varstor 0.6.61 → 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 +111 -58
- package/dist/varstor-webextension.js +441 -506
- package/dist/varstor-webextension.min.js +1 -1
- package/dist/varstor.js +380 -430
- package/dist/varstor.min.js +1 -1
- package/package.json +5 -1
- package/src/constants.ts +1 -0
- package/src/helpers.ts +88 -0
- package/src/index.ts +311 -0
- package/src/listeners.ts +74 -0
- package/src/storage.ts +37 -0
- package/src/types.d.ts +39 -0
- package/src/{validation.js → validation.ts} +11 -8
- package/src/{webextension-storage.js → webextension-storage.ts} +6 -6
- package/src/{webextension.js → webextension.ts} +5 -2
- package/tsconfig.json +28 -0
- package/webpack.config.js +16 -2
- package/src/helpers.js +0 -40
- package/src/index.js +0 -283
- package/src/namespace.js +0 -35
- package/src/storage.js +0 -31
package/README.md
CHANGED
|
@@ -18,126 +18,179 @@ import Varstor from 'varstor/webextension'
|
|
|
18
18
|
```
|
|
19
19
|
in your script file.
|
|
20
20
|
|
|
21
|
-
##
|
|
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
|
-
.
|
|
40
|
+
.reset ()
|
|
29
41
|
.onChange ()
|
|
30
42
|
.removeListener ()
|
|
31
43
|
```
|
|
32
44
|
|
|
33
|
-
|
|
34
|
-
|
|
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:
|
|
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
|
|
51
|
-
```
|
|
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
|
-
|
|
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
|
-
|
|
80
|
+
## Accessing ```.get()``` <a name="accessing"></a>
|
|
81
|
+
Values are accessed with:
|
|
67
82
|
```js
|
|
68
|
-
Varstor.get
|
|
83
|
+
Varstor.get() => NamespaceValues {}
|
|
69
84
|
```
|
|
70
85
|
Where:
|
|
71
|
-
```
|
|
86
|
+
```NamespaceValues {}``` - an object representing all the values in the current namespace at the current moment
|
|
72
87
|
|
|
73
|
-
|
|
74
|
-
|
|
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
|
|
102
|
+
To reset values back to defaults:
|
|
86
103
|
```js
|
|
87
|
-
async 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
|
|
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(
|
|
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(
|
|
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
|
-
##
|
|
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
|
-
.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
.
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
+
```
|