Skip to content

Commit f0e00f2

Browse files
committed
feat: docs completed
1 parent 3bd0677 commit f0e00f2

File tree

1 file changed

+149
-6
lines changed

1 file changed

+149
-6
lines changed

README.md

Lines changed: 149 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,122 @@
11
# vue-subscription
22

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.
44

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.
66

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).
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+
---
8102

9103
## Usage
10104

105+
### Basic Example
106+
11107
```typescript
12108
const $mySubscription = useSubscription('hello'); // Type will be string
13109

14110
// Get the current value
15111
console.log($mySubscription.$value); // 'hello'
16112

17-
function mySubscriber(value: string) {
18-
console.log(`The value is now: ${value}`);
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+
});
19120
}
20121

21122
// Add a subscriber
@@ -36,7 +137,9 @@ const myReadonlyValue = $mySubscription.$read;
36137
console.log(myReadonlyValue.value); // 'world'
37138
```
38139

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.
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.
40143

41144
```typescript
42145
const $mySubscription = useSubscription(
@@ -70,6 +173,8 @@ function tester() {
70173
tester();
71174
```
72175

176+
### Destructured
177+
73178
You can also destructure the properties to have a seperate getter and setter.
74179

75180
```typescript
@@ -96,3 +201,41 @@ $set(val => `Hello ${val}`);
96201
// Use the readonly version of the value
97202
console.log($read.value); // 'Hello world'
98203
```
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

Comments
 (0)