varstor 0.0.4 → 0.0.5
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 +120 -2
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -14,6 +14,124 @@ import varstor from 'varstor'
|
|
|
14
14
|
or, if you are developing a webextension (except content script)
|
|
15
15
|
|
|
16
16
|
```js
|
|
17
|
-
import
|
|
17
|
+
import Varstor from 'varstor/webextension'
|
|
18
18
|
```
|
|
19
|
-
in your script file.
|
|
19
|
+
in your script file.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
The library object features the following methods:
|
|
23
|
+
```js
|
|
24
|
+
.add ()
|
|
25
|
+
.addPersistent ()
|
|
26
|
+
.get ()
|
|
27
|
+
.set ()
|
|
28
|
+
.resetAll ()
|
|
29
|
+
.onChange ()
|
|
30
|
+
.removeListener ()
|
|
31
|
+
```
|
|
32
|
+
|
|
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:
|
|
35
|
+
```js
|
|
36
|
+
async Varstor.add(
|
|
37
|
+
KeysValues {
|
|
38
|
+
key1: value1,
|
|
39
|
+
key2: value2,
|
|
40
|
+
key3: ComputeFunction(...dependencies) => computedValue
|
|
41
|
+
...
|
|
42
|
+
}
|
|
43
|
+
) =>
|
|
44
|
+
StateAccessors {
|
|
45
|
+
key: StateAccessor
|
|
46
|
+
...
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
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.
|
|
53
|
+
|
|
54
|
+
**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
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
Values can be accessed with:
|
|
59
|
+
```js
|
|
60
|
+
Varstor.get () => StateValues
|
|
61
|
+
```
|
|
62
|
+
Where:
|
|
63
|
+
```StateValues``` - an object representing all the values in the store in the current namespace at the current moment
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
or
|
|
67
|
+
```js
|
|
68
|
+
Varstor.get (AccessorsCallback (StateAccessors {}) => value) => value
|
|
69
|
+
```
|
|
70
|
+
Where:
|
|
71
|
+
```AccessorsCallback``` - a function that takes all ```StateAccessors``` in the current namespace, manipulates them, and can return any ```value```, which in turn will be returned from the whole ```get(Callback)``` call.
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
Values can be mutated with:
|
|
75
|
+
```js
|
|
76
|
+
async Varstor.set (
|
|
77
|
+
KeysValues {
|
|
78
|
+
key: value
|
|
79
|
+
...
|
|
80
|
+
}
|
|
81
|
+
)
|
|
82
|
+
```
|
|
83
|
+
**Important: values are updated asynchronously; don't assume the script will recognize the change immediately. Instead, make use of ```onChange``` listeners!**
|
|
84
|
+
|
|
85
|
+
To reset all stored values back to defaults:
|
|
86
|
+
```js
|
|
87
|
+
async Varstor.resetAll ()
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
To listen and react to state changes:
|
|
91
|
+
```js
|
|
92
|
+
Varstor.onChange (Keys[], ChangeCallback(newValue, allValues, previousValue) => void)
|
|
93
|
+
```
|
|
94
|
+
Where:
|
|
95
|
+
```Keys``` (optional) - array of keys of state that you want to listen to
|
|
96
|
+
```ChangeCallback``` - function to run when a change happens
|
|
97
|
+
|
|
98
|
+
To remove the listener:
|
|
99
|
+
```js
|
|
100
|
+
Varstor.removeListener(Keys, ChangeCallback)
|
|
101
|
+
```
|
|
102
|
+
the same parameter usage
|
|
103
|
+
|
|
104
|
+
## Namespacing
|
|
105
|
+
To avoid name collisions, put keys with the same name in different namespaces. You can do that with
|
|
106
|
+
```js
|
|
107
|
+
Varstor.get(String Namespace) => Varstor
|
|
108
|
+
```
|
|
109
|
+
which will return a new instance of ```Varstor``` with the specified ```Namespace```.
|
|
110
|
+
|
|
111
|
+
## Shortcuts
|
|
112
|
+
The library object itself can be called with different types of arguments, which will mirror almost all of its API.
|
|
113
|
+
```js
|
|
114
|
+
Varstor() -> Varstor.get()
|
|
115
|
+
Varstor(String namespace) -> Varstor.get(namespace)
|
|
116
|
+
Varstor(() => {}) -> Varstor.get(() => {})
|
|
117
|
+
Varstor({ key: value }) -> Varstor.set({ key: value })
|
|
118
|
+
Varstor({}) -> Varstor.resetAll()
|
|
119
|
+
Varstor([], () => {}) -> Varstor.onChange([], () => {})
|
|
120
|
+
```
|
|
121
|
+
Each call returns a new instance of ```Varstor```, so the chaining is possible.
|
|
122
|
+
|
|
123
|
+
## StateAccessor
|
|
124
|
+
```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:
|
|
125
|
+
```js
|
|
126
|
+
.()
|
|
127
|
+
.set(value)
|
|
128
|
+
.reset()
|
|
129
|
+
.onChange(Callback)
|
|
130
|
+
.removeListener(Callback)
|
|
131
|
+
```
|
|
132
|
+
Where:
|
|
133
|
+
```()``` (call the object itself) - returns the value
|
|
134
|
+
```.set(value)``` - sets the value
|
|
135
|
+
```.reset()``` - resets the value
|
|
136
|
+
```.onChange(Callback)``` - adds the listener
|
|
137
|
+
```.removeListener(Callback)``` - removes the listener
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "varstor",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "State manager for web applications and webextensions, employing reactivity, wrapping, and uniting the usage of different storages' values and common variables into a simple universal interface",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Igor Morozov <ismorozs@gmail.com>",
|
|
7
7
|
"main": "./dist/varstor.js",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./dist/varstor.js",
|
|
10
|
-
"./
|
|
10
|
+
"./min": "./dist/varstor.min.js",
|
|
11
|
+
"./webextension": "./dist/varstor-webextension.js",
|
|
12
|
+
"./webextension/min": "./dist/varstor-webextension.min.js"
|
|
11
13
|
},
|
|
12
14
|
"scripts": {
|
|
13
15
|
"start": "webpack",
|