|
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 shallow reactive value, a subscriber, and a few extra methods. |
| 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. |
4 | 4 |
|
5 | | -The `useSubscription` function takes an initial value and returns an object with a reactive value property that is a shallowRef 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 Subscriber functions can be added to a Set of subscribers 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 of the object. The `useSubscription` also returns an internal object named `$sub` with all the methods except readonly without the prefix `$`. |
| 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). |
8 | 8 |
|
9 | 9 | ## Usage |
10 | 10 |
|
11 | 11 | ```typescript |
| 12 | +const $mySubscription = useSubscription('hello'); // Type will be string |
| 13 | + |
| 14 | +// Get the current value |
| 15 | +console.log($mySubscription.$value); // 'hello' |
| 16 | + |
12 | 17 | function mySubscriber(value: string) { |
13 | 18 | console.log(`The value is now: ${value}`); |
14 | 19 | } |
15 | 20 |
|
16 | | -function example() { |
17 | | - const $mySubscription = useSubscription('hello'); |
| 21 | +// Add a subscriber |
| 22 | +$mySubscription.$addSub(mySubscriber); |
| 23 | +// Manually trigger the subscribers if needed(rarely) |
| 24 | +$mySubscription.$triggerSubs(); // 'The value is now: hello' |
18 | 25 |
|
19 | | - // Get the current value |
20 | | - console.log($mySubscription.value); // 'hello' |
| 26 | +// Set the value |
| 27 | +$mySubscription.$value = 'world'; |
21 | 28 |
|
22 | | - // Add a subscriber |
23 | | - $mySubscription.$subscriber = mySubscriber; |
| 29 | +// Subscriber runs here - 'The value is now: world' |
24 | 30 |
|
25 | | - // Set the value |
26 | | - $mySubscription.value = 'world'; |
| 31 | +// Remove a subscriber (can be used in Unmount, beforeRouteLeave etc) |
| 32 | +$mySubscription.$deleteSub(mySubscriber); |
27 | 33 |
|
28 | | - // Subscriber runs here - 'The value is now: world' |
| 34 | +// Use the readonly version of the value |
| 35 | +const myReadonlyValue = $mySubscription.$read; |
| 36 | +console.log(myReadonlyValue.value); // 'world' |
| 37 | +``` |
29 | 38 |
|
30 | | - // Manually trigger the subscribers if needed(rarely) |
31 | | - $mySubscription.$triggerSubs(); // 'The value is now: world' |
| 39 | +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. |
32 | 40 |
|
33 | | - // Remove a subscriber (can be used in Unmount, beforeRouteLeave etc) |
34 | | - $mySubscription.$deleteSub(mySubscriber); |
| 41 | +```typescript |
| 42 | +const $mySubscription = useSubscription( |
| 43 | + { |
| 44 | + user: { |
| 45 | + name: 'John', |
| 46 | + isActive: false |
| 47 | + } |
| 48 | + }, |
| 49 | + // You can pass `true` as the deep flag to make the subscription deeply reactive if used in templates |
| 50 | + true |
| 51 | +); |
| 52 | +// Add a subscriber |
| 53 | +$mySubscription.$addSub(data => { |
| 54 | + console.log(`The data is now: ${JSON.stringify(data)}`); |
| 55 | +}); |
| 56 | + |
| 57 | +function myMutator(data: typeof $mySubscription.$value) { |
| 58 | + data.user.isActive = true; |
| 59 | + return data; |
| 60 | +} |
| 61 | + |
| 62 | +// Trigger the subscribers |
| 63 | +$mySubscription.$triggerSubs(); // 'The data is now: { user: { name: 'John', isActive: false }}' |
35 | 64 |
|
36 | | - // Use the readonly version of the value |
37 | | - const myReadonlyValue = $mySubscription.$ref; |
38 | | - console.log(myReadonlyValue.value); // { name: 'world', age: 30 } |
| 65 | +function tester() { |
| 66 | + // Mutate the value (only works if the value is an object) |
| 67 | + $mySubscription.$mutate(myMutator); |
| 68 | + // Subscriber runs here - 'The data is now: { user: { name: 'John', isActive: true }}' |
39 | 69 | } |
| 70 | +tester(); |
40 | 71 | ``` |
41 | 72 |
|
42 | | -Example when using complex objects which won't be tracked deeply by default |
| 73 | +You can also destructure the properties to have a seperate getter and setter. |
43 | 74 |
|
44 | 75 | ```typescript |
45 | | -function mySubscriber(data: object) { |
46 | | - console.log(`The data is now: ${data}`); |
47 | | -} |
| 76 | +const { $get, $set, $read, $addSub } = useSubscription('hello'); |
48 | 77 |
|
49 | | -function myMutator(data: object) { |
50 | | - data.isActive = true; |
51 | | - return data; |
52 | | -} |
53 | | - |
54 | | -function example() { |
55 | | - const $mySubscription = useSubscription({ |
56 | | - user: { |
57 | | - name: Sravan, |
58 | | - isActive: false |
59 | | - } |
60 | | - }); |
| 78 | +// Get the current value |
| 79 | +console.log($get()); // 'hello' |
61 | 80 |
|
62 | | - // Get the current value |
63 | | - console.log($mySubscription.value); // '{ user: { name: Sravan, isActive: false }}' |
| 81 | +function mySubscriber(value: string) { |
| 82 | + console.log(`The value is now: ${value}`); |
| 83 | +} |
64 | 84 |
|
65 | | - // Add a subscriber |
66 | | - $mySubscription.$subscriber = mySubscriber; |
| 85 | +// Add a subscriber |
| 86 | +$addSub(mySubscriber); |
67 | 87 |
|
68 | | - // Trigger the subscribers |
69 | | - $mySubscription.$triggerSubs(); // 'The data is now: { user: { name: Sravan, isActive: false }}' |
| 88 | +// Set the value |
| 89 | +$set('world'); |
70 | 90 |
|
71 | | - // Mutate the value (only works if the value is an object) |
72 | | - $mySubscription.$mutate(myMutator); |
| 91 | +// Subscriber runs here - 'The value is now: world' |
73 | 92 |
|
74 | | - // Subscriber runs here - 'The data is now: { user: { name: Sravan, isActive: true }}' |
| 93 | +$set(val => `Hello ${val}`); |
| 94 | +// Subscriber runs here - 'The value is now: Hello world' |
75 | 95 |
|
76 | | - console.log($mySubscription.value); // '{ user: { name: Sravan, isActive: true }}' |
77 | | -} |
| 96 | +// Use the readonly version of the value |
| 97 | +console.log($read.value); // 'Hello world' |
78 | 98 | ``` |
0 commit comments