varstor 0.6.6 → 0.6.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,126 +18,179 @@ import Varstor from 'varstor/webextension'
18
18
  ```
19
19
  in your script file.
20
20
 
21
- ## Usage
21
+ ## Contents
22
+ 1. [Usage](#overview)
23
+ [1.1 Creation ```.add()```/```.addPersistent()```](#creation)
24
+ [1.2 Dynamic reevaluation (```ReactiveFunction```)](#reactivefunction)
25
+ [1.3 Accessing ```.get()```](#accessing)
26
+ [1.4 Mutating ```.set()```/```.reset()```](#mutating)
27
+ [1.5 Listening ```.onChange()```/```.removeListener()```](#listening)
28
+ [1.6 Namespacing ```.get(Namespace)```](#namespacing)
29
+ [1.7 Method chaining](#chaining)
30
+ 2. [Shortcuts](#shortcuts)
31
+ 3. [Example](#example)
32
+
33
+ ## Usage <a name="overview"></a>
22
34
  The library object features the following methods:
23
35
  ```js
24
36
  .add ()
25
37
  .addPersistent ()
26
38
  .get ()
27
39
  .set ()
28
- .resetAll ()
40
+ .reset ()
29
41
  .onChange ()
30
42
  .removeListener ()
31
43
  ```
32
44
 
33
- Values are added to the store with ```add``` or ```addPersistent``` methods. They perform the same functionality, except ```addPersistent``` allows state to be saved in storage and reused between browser sessions.
34
- Signature of those methods is:
45
+ ## Creation ```.add()```/```.addPersistent()``` <a name="creation"></a>
46
+ Values are added to the store with add or addPersistent methods. They perform the same functionality, except addPersistent saves state to the storage and lets you reuse it between browser sessions.
47
+
35
48
  ```js
36
49
  async Varstor.add(
37
50
  KeysValues {
38
51
  key1: value1,
39
52
  key2: value2,
40
- key3: ComputeFunction(...dependencies) => computedValue
41
- ...
42
- }
43
- ) =>
44
- StateAccessors {
45
- key: StateAccessor
53
+ key3: ReactiveFunction(...valueNames[]) => computedValue
46
54
  ...
47
55
  }
56
+ ) => Varstor
48
57
  ```
49
58
  Where:
50
- ```KeysValues``` - a standard object with keys and values, where ```key``` is used to identify a piece of state, and ```value``` to give it a default value or ```ComputeFunction```
51
- ```ComputeFunction``` - reevalutes and updates its value automatically each time ```dependencies``` parameters change. ```dependencies``` parameters are any state values defined before the ```ComputeFunction```. ```computedValue```s are not saved in storage.
52
- ```StateAccessors``` - a map linking state ```key```s to special ```StateAccessor``` objects which will perform data manipulations and listener management.
59
+ ```KeysValues {}``` - a standard object with keys and values, where ```key``` is a name of piece of state, and ```value``` is a default value or ```ReactiveFunction```
60
+ ```ReactiveFunction``` - re-evalutes and updates its value automatically each time arguments in ```valueNames``` list change. ```valueNames``` are any state values defined before the ```ReactiveFunction```. ```computedValue```s are not saved in storage.
53
61
 
54
62
  **Important: ```add``` and ```addPersistent``` are asynchronous operations; you must `await` or use ```Promise.then``` to ensure all the data is ready to work with!**
55
63
 
56
-
64
+ ## Dynamic reevaluation (```ReactiveFunction```) <a name="reactivefunction"></a>
65
+ Values can change automatically with the help of ```ReactiveFunction```s when one or more of the other values in the namespace change.
57
66
 
58
- Values can be accessed with:
59
67
  ```js
60
- Varstor.get () => StateValues
68
+ ReactiveFunction (...valueNames[]) => computedValue
69
+ ```
70
+ Put the names of dependencies in the ```valueNames``` arguments list of the ```ReactiveFunction```, and describe the calculation in the body of the function.
71
+ ```js
72
+ Varstor.add({
73
+ a: 1
74
+ b: 2,
75
+ c: (a, b) => a + b // 3
76
+ })
61
77
  ```
62
- Where:
63
- ```StateValues``` - an object representing all the values in the store in the current namespace at the current moment
64
78
 
65
79
 
66
- or
80
+ ## Accessing ```.get()``` <a name="accessing"></a>
81
+ Values are accessed with:
67
82
  ```js
68
- Varstor.get (AccessorsCallback (StateAccessors {}, Varstor) => value) => value
83
+ Varstor.get() => NamespaceValues {}
69
84
  ```
70
85
  Where:
71
- ```AccessorsCallback``` - a function that takes all ```StateAccessors``` and a ```Varstor``` instance from the current namespace, manipulates them, and can return any ```value```, which in turn will be returned from the whole ```get(Callback)``` call.
86
+ ```NamespaceValues {}``` - an object representing all the values in the current namespace at the current moment
72
87
 
73
-
74
- Values can be mutated with:
88
+
89
+
90
+ ## Mutating ```.set()```/```.reset()``` <a name="mutating"></a>
91
+ Values are mutated with:
75
92
  ```js
76
- async Varstor.set (
93
+ async Varstor.set(
77
94
  KeysValues {
78
95
  key: value
79
96
  ...
80
97
  }
81
98
  ) => Varstor
82
99
  ```
83
- **Important: values are updated asynchronously; don't assume the script will recognize the change immediately. Instead, make use of ```onChange``` listeners!**
100
+ **Important: values are updated asynchronously; don't assume the script will recognize the change immediately on the next line. Instead, ```await``` or make use of ```onChange``` listeners!**
84
101
 
85
- To reset all stored values back to defaults:
102
+ To reset values back to defaults:
86
103
  ```js
87
- async Varstor.resetAll () => Varstor
104
+ async Varstor.reset(Keys []) => Varstor
88
105
  ```
106
+ Where:
107
+ ```Keys[]``` (optional) - array of keys to return to default values. If omitted all values will be returned to defaults.
108
+
109
+
110
+ ## Listening ```.onChange()```/```.removeListener()``` <a name="listening"></a>
89
111
 
90
112
  To listen and react to state changes:
91
113
  ```js
92
- Varstor.onChange (Keys[], ChangeCallback(newValue, allValues, previousValue) => void) => Varstor
114
+ Varstor.onChange(
115
+ Keys[],
116
+ ChangeCallback(ChangedKeys [], allValues {}, PreviousValues {}) => void
117
+ ) => Varstor
93
118
  ```
94
119
  Where:
95
- ```Keys``` - array of keys of the state in the current namespace that you want to listen to
120
+ ```Keys[]``` (optional) - array of keys of the state in the current namespace that you want to listen to. If omitted, ```ChangeCallback``` will run on any value change in the namespace.
96
121
  ```ChangeCallback``` - function to run when a change happens
97
122
 
98
123
  To remove the listener:
99
124
  ```js
100
- Varstor.removeListener(Keys, ChangeCallback) => Varstor
125
+ Varstor.removeListener(
126
+ Keys[],
127
+ ChangeCallback(ChangedKeys [], allValues {}, PreviousValues {}) => void
128
+ ) => Varstor
101
129
  ```
102
130
  the same parameter usage.
103
131
 
104
-
105
-
106
-
107
-
108
- Methods ```.set()```, ```.resetAll()```, ```onChange```, and ```removeListener``` all return a new instance of ```Varstor``` with the same namespace, so command chaining is possible.
109
- Method ```.get(() => {})``` may return a new ```Varstor``` instance.
110
-
111
- ## Namespacing
132
+ ## Namespacing ```.get(Namespace)``` <a name="namespacing"></a>
112
133
  To avoid name collisions, put keys with the same name in different namespaces. You can create a new or get an existing namespace with
113
134
  ```js
114
- Varstor.get(String Namespace) => Varstor
135
+ Varstor.get(Namespace string) => Varstor
115
136
  ```
116
- which will return a new instance of ```Varstor``` with the specified ```Namespace```.
137
+ which will return a new instance of ```Varstor``` with the specified ```Namespace```.
138
+
139
+
140
+ ## Method chaining <a name="chaining"></a>
141
+ Methods ```.add```, ```.addPersistent```, ```.set()```, ```.reset()```, ```onChange```, and ```removeListener``` all return a new instance of ```Varstor``` with the same namespace, so method chaining is possible.
142
+
117
143
 
118
- ## Shortcuts
119
- The library object itself can be called with different types of arguments, which will mirror almost all of its API.
144
+ ## Shortcuts <a name="shortcuts"></a>
145
+ The library/namespace object itself can be called with different types of arguments, which will mirror almost all of its API.
120
146
  ```js
121
147
  Varstor() -> Varstor.get()
122
148
  Varstor(String namespace) -> Varstor.get(namespace)
123
- Varstor(() => {}) -> Varstor.get(() => {})
124
149
  Varstor({ key: value }) -> Varstor.set({ key: value })
125
- Varstor({}) -> Varstor.resetAll()
126
150
  Varstor([], () => {}) -> Varstor.onChange([], () => {})
127
151
  ```
128
152
 
129
- ## StateAccessor
130
- ```StateAccessor``` is a way to manipulate each piece of state individually. It's a function that can be called itself, but also an object with a set of methods:
153
+ ## Example <a name="example"></a>
131
154
  ```js
132
- .()
133
- .set(value)
134
- .reset()
135
- .onChange(Callback)
136
- .removeListener(Callback)
137
- ```
138
- Where:
139
- ```()``` (call the object itself) - returns the value
140
- ```.set(value)``` - sets the value
141
- ```.reset()``` - resets the value
142
- ```.onChange(Callback)``` - adds the listener
143
- ```.removeListener(Callback)``` - removes the listener
155
+ function onChange(changes, values, data) {
156
+ console.log("onChange", changes, values, data);
157
+ }
158
+
159
+ Varstor.add({
160
+ a: 10,
161
+ b: 20,
162
+ c: (a, b) => a + b,
163
+ });
164
+
165
+ await Varstor.addPersistent({
166
+ d: 40,
167
+ });
168
+
169
+ console.log(Varstor.get()); // {a: 10, b: 20, c: 30, d: 40}
170
+
171
+ Varstor.onChange(["a", "b", "c", "d"], onChange);
172
+ Varstor.onChange(onChange);
173
+
174
+ await Varstor.set({ a: 20, b: 88, d: 60 });
175
+ console.log(Varstor.get()); // {a: 20, b: 88, c: 108, d: 60}
176
+
177
+ await Varstor.reset(["b"]);
178
+ console.log(Varstor.get()); // {a: 20, b: 20, c: 40, d: 60}
179
+
180
+ await Varstor.reset();
181
+ console.log(Varstor.get()); // {a: 10, b: 20, c: 30, d: 40}
182
+
183
+ Varstor.removeListener(["a", "b", "c", "d"], onChange);
184
+ Varstor.removeListener(onChange);
185
+
186
+ const newNamespace = Varstor("new namespace");
187
+
188
+ newNamespace.add({
189
+ yyy: 1,
190
+ zzz: 2,
191
+ });
192
+
193
+ newNamespace({ zzz: 3 });
194
+
195
+ console.log(Varstor("new namespace").get()); // {yyy: 1, zzz: 3}
196
+ ```