You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+149-6Lines changed: 149 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,122 @@
1
1
# vue-subscription
2
2
3
-
This is a TypeScript module for Vue.js, which provides a function `useSubscription`that returns an object with a reactive value, a subscriber, and a few extra methods.
3
+
This Vue package provides a simple way to create reactive subscriptions that can be used to observe changes to a value and execute a list of subscribers when the value changes. It also includes methods to mutate the value and trigger subscribers manually.
4
4
5
-
The `useSubscription` function takes an initial value and returns an object with a reactive value of the initial value passed in, and Subscriber functions can be added to a Set of subscribers to be executed when the value is changed.
5
+
The `useSubscription` function takes an initial value and returns an object with a reactive value of the initial value passed in, and a subscriber can be added to be executed when the value is changed.
6
6
7
-
The module also includes methods to add, delete and trigger the subscribers as well as a method to mutate the value if it is a more complex datatype(typeof object).
This method adds a subscriber to the subscription. A subscriber is a function that takes the new value as an argument and is executed whenever the value changes. The subscriber can be `async`
64
+
65
+
```typescript
66
+
function logValue(value) {
67
+
console.log(`New value: ${value}`);
68
+
}
69
+
70
+
$mySubscription.$addSub(logValue);
71
+
$mySubscription.$deleteSub(subscriber);
72
+
```
73
+
74
+
### $deleteSub
75
+
76
+
This method removes a subscriber from the subscription.
77
+
78
+
```typescript
79
+
subscription.$deleteSub(logValue);
80
+
```
81
+
82
+
### $triggerSubs
83
+
84
+
This method manually triggers all subscribers to the subscription.
85
+
86
+
```typescript
87
+
subscription.$triggerSubs();
88
+
```
89
+
90
+
### $mutate
91
+
92
+
This method mutates the subscription value with a mutator function. The mutator function takes the current value as an argument and returns the new value.
93
+
94
+
```typescript
95
+
subscription.$mutate(value=> {
96
+
value.name='John';
97
+
returnvalue;
98
+
});
99
+
```
100
+
101
+
---
8
102
9
103
## Usage
10
104
105
+
### Basic Example
106
+
11
107
```typescript
12
108
const $mySubscription =useSubscription('hello'); // Type will be string
Example when using complex objects which won't be tracked deeply by default. Unless the subscriber is used in templates, watch, watchEffect and template you don't need to add the deep flag.
140
+
### Complex state
141
+
142
+
Example uses a complex objects which won't be tracked deeply by default. Unless the subscriber is used in templates, watch, watchEffect and computed you don't need to add the deep flag.
40
143
41
144
```typescript
42
145
const $mySubscription =useSubscription(
@@ -70,6 +173,8 @@ function tester() {
70
173
tester();
71
174
```
72
175
176
+
### Destructured
177
+
73
178
You can also destructure the properties to have a seperate getter and setter.
deep (optional) - Whether to create a shallow or deep reactive subscription. Defaults to false.
231
+
Return Value
232
+
An object with the following properties:
233
+
234
+
- $value - The current value of the subscription.
235
+
- $get() - A function that returns the current value of the subscription.
236
+
- $set(value: T | ((value: T) => T)) - A function that sets the value of the subscription. If a function is passed, it will receive the current value of the subscription as its argument and should return the new value.
237
+
- $read - A readonly reactive reference to the current value of the subscription.
238
+
- $addSub(subscriber: (value: T) => Promise<void> | void)) - A method for adding a subscriber to the subscription. It can be `async`. The subscriber is a function that will be executed whenever the value of the subscription changes. It can take the new value of the subscription as its argument.
239
+
- $deleteSub(subscriber: (value: T) => Promise<void> | void)) - A method for removing a subscriber from the subscription.
240
+
- $triggerSubs() - A method for manually triggering all subscribers. This should rarely be necessary.
241
+
- $mutate(mutator: (value: T) => T) - A method for updating the value of the subscription with a function that takes the current value as its argument and returns the new value. This should only be used for updating complex objects.
0 commit comments