|
1 | 1 | # vue-subscription
|
2 |
| -A subscriber to replace the EventBus in Vue. |
| 2 | + |
| 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 | + |
| 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 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Installation and Import |
| 10 | + |
| 11 | +To use this package, you can install it via npm: |
| 12 | + |
| 13 | +```sh |
| 14 | +// In your console |
| 15 | +npm install @vue-subscription |
| 16 | +``` |
| 17 | + |
| 18 | +```typescript |
| 19 | +// In your file |
| 20 | +import { useSubscription } from '@vue-subscription'; |
| 21 | +const $mySubscription = useSubscription('hello'); // Type will be string |
| 22 | +``` |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## API |
| 27 | + |
| 28 | +### $value / $get() |
| 29 | + |
| 30 | +This property/method returns the current value of the subscription. |
| 31 | + |
| 32 | +```typescript |
| 33 | +const value = $mySubscription.$value; |
| 34 | +const value = $mySubscription.$get(); |
| 35 | +``` |
| 36 | + |
| 37 | +### $value = val / $set(val) |
| 38 | + |
| 39 | +This property/method sets the current value of the subscription. |
| 40 | + |
| 41 | +```typescript |
| 42 | +$mySubscription.$value = 42; |
| 43 | +$mySubscription.$set(42); |
| 44 | +``` |
| 45 | + |
| 46 | +The $set method can also accept a mutator function that takes the current value as an argument and returns the new value: |
| 47 | + |
| 48 | +```typescript |
| 49 | +$mySubscription.$set(value => value + 1); |
| 50 | +``` |
| 51 | + |
| 52 | +### $read |
| 53 | + |
| 54 | +This is a read-only version of the subscription value. It wraps the subscription in a readonly ref. |
| 55 | + |
| 56 | +```typescript |
| 57 | +const readonlySubscription = $mySubscription.$read; |
| 58 | +console.log(readonlySubscription.value); |
| 59 | +``` |
| 60 | + |
| 61 | +### $addSub |
| 62 | + |
| 63 | +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 | + return value; |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Usage |
| 104 | + |
| 105 | +### Basic Example |
| 106 | + |
| 107 | +```typescript |
| 108 | +const $mySubscription = useSubscription('hello'); // Type will be string |
| 109 | + |
| 110 | +// Get the current value |
| 111 | +console.log($mySubscription.$value); // 'hello' |
| 112 | + |
| 113 | +// Subscribers can `async` |
| 114 | +async function mySubscriber(value: string) { |
| 115 | + return new Promise((resolve, reject) => { |
| 116 | + setTimeout(() => { |
| 117 | + console.log(`The value is now: ${value}`); |
| 118 | + }, 1); |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +// Add a subscriber |
| 123 | +$mySubscription.$addSub(mySubscriber); |
| 124 | +// Manually trigger the subscribers if needed(rarely) |
| 125 | +$mySubscription.$triggerSubs(); // 'The value is now: hello' |
| 126 | + |
| 127 | +// Set the value |
| 128 | +$mySubscription.$value = 'world'; |
| 129 | + |
| 130 | +// Subscriber runs here - 'The value is now: world' |
| 131 | + |
| 132 | +// Remove a subscriber (can be used in Unmount, beforeRouteLeave etc) |
| 133 | +$mySubscription.$deleteSub(mySubscriber); |
| 134 | + |
| 135 | +// Use the readonly version of the value |
| 136 | +const myReadonlyValue = $mySubscription.$read; |
| 137 | +console.log(myReadonlyValue.value); // 'world' |
| 138 | +``` |
| 139 | + |
| 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. |
| 143 | + |
| 144 | +```typescript |
| 145 | +const $mySubscription = useSubscription( |
| 146 | + { |
| 147 | + user: { |
| 148 | + name: 'John', |
| 149 | + isActive: false |
| 150 | + } |
| 151 | + }, |
| 152 | + // You can pass `true` as the deep flag to make the subscription deeply reactive if used in templates |
| 153 | + true |
| 154 | +); |
| 155 | +// Add a subscriber |
| 156 | +$mySubscription.$addSub(data => { |
| 157 | + console.log(`The data is now: ${JSON.stringify(data)}`); |
| 158 | +}); |
| 159 | + |
| 160 | +function myMutator(data: typeof $mySubscription.$value) { |
| 161 | + data.user.isActive = true; |
| 162 | + return data; |
| 163 | +} |
| 164 | + |
| 165 | +// Trigger the subscribers |
| 166 | +$mySubscription.$triggerSubs(); // 'The data is now: { user: { name: 'John', isActive: false }}' |
| 167 | + |
| 168 | +function tester() { |
| 169 | + // Mutate the value (only works if the value is an object) |
| 170 | + $mySubscription.$mutate(myMutator); |
| 171 | + // Subscriber runs here - 'The data is now: { user: { name: 'John', isActive: true }}' |
| 172 | +} |
| 173 | +tester(); |
| 174 | +``` |
| 175 | + |
| 176 | +### Destructured |
| 177 | + |
| 178 | +You can also destructure the properties to have a seperate getter and setter. |
| 179 | + |
| 180 | +```typescript |
| 181 | +const { $get, $set, $read, $addSub } = useSubscription('hello'); |
| 182 | + |
| 183 | +// Get the current value |
| 184 | +console.log($get()); // 'hello' |
| 185 | + |
| 186 | +function mySubscriber(value: string) { |
| 187 | + console.log(`The value is now: ${value}`); |
| 188 | +} |
| 189 | + |
| 190 | +// Add a subscriber |
| 191 | +$addSub(mySubscriber); |
| 192 | + |
| 193 | +// Set the value |
| 194 | +$set('world'); |
| 195 | + |
| 196 | +// Subscriber runs here - 'The value is now: world' |
| 197 | + |
| 198 | +$set(val => `Hello ${val}`); |
| 199 | +// Subscriber runs here - 'The value is now: Hello world' |
| 200 | + |
| 201 | +// Use the readonly version of the value |
| 202 | +console.log($read.value); // 'Hello world' |
| 203 | +``` |
| 204 | + |
| 205 | +--- |
| 206 | + |
| 207 | +## Type definition |
| 208 | + |
| 209 | +### Function Signature |
| 210 | + |
| 211 | +```typescript |
| 212 | +function useSubscription<T>( |
| 213 | + value: T, |
| 214 | + deep?: boolean |
| 215 | +): { |
| 216 | + $value: T; |
| 217 | + $get: () => T; |
| 218 | + $set: (value: T | ((value: T) => T)) => void; |
| 219 | + $read: Readonly<Ref<T>>; |
| 220 | + $addSub: (subscriber: (value: T) => Promise<void> | void) => void; |
| 221 | + $deleteSub: (subscriber: (value: T) => Promise<void> | void) => void; |
| 222 | + $triggerSubs: () => void; |
| 223 | + $mutate: (mutator: (value: T) => T) => void; |
| 224 | +}; |
| 225 | +``` |
| 226 | + |
| 227 | +### Arguments |
| 228 | + |
| 229 | +value - The initial value of the subscription. |
| 230 | +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