varstor 0.0.4 → 0.0.6

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.
Files changed (2) hide show
  1. package/README.md +126 -2
  2. package/package.json +4 -2
package/README.md CHANGED
@@ -14,6 +14,130 @@ import varstor from 'varstor'
14
14
  or, if you are developing a webextension (except content script)
15
15
 
16
16
  ```js
17
- import varstor from 'varstor/webextension'
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 {}, Varstor) => value) => value
69
+ ```
70
+ 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.
72
+
73
+
74
+ Values can be mutated with:
75
+ ```js
76
+ async Varstor.set (
77
+ KeysValues {
78
+ key: value
79
+ ...
80
+ }
81
+ ) => Varstor
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 () => Varstor
88
+ ```
89
+
90
+ To listen and react to state changes:
91
+ ```js
92
+ Varstor.onChange (Keys[], ChangeCallback(newValue, allValues, previousValue) => void) => Varstor
93
+ ```
94
+ Where:
95
+ ```Keys``` - array of keys of the state in the current namespace 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) => Varstor
101
+ ```
102
+ the same parameter usage.
103
+
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
112
+ 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
+ ```js
114
+ Varstor.get(String Namespace) => Varstor
115
+ ```
116
+ which will return a new instance of ```Varstor``` with the specified ```Namespace```.
117
+
118
+ ## Shortcuts
119
+ The library object itself can be called with different types of arguments, which will mirror almost all of its API.
120
+ ```js
121
+ Varstor() -> Varstor.get()
122
+ Varstor(String namespace) -> Varstor.get(namespace)
123
+ Varstor(() => {}) -> Varstor.get(() => {})
124
+ Varstor({ key: value }) -> Varstor.set({ key: value })
125
+ Varstor({}) -> Varstor.resetAll()
126
+ Varstor([], () => {}) -> Varstor.onChange([], () => {})
127
+ ```
128
+
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:
131
+ ```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
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "varstor",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
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
- "./webextension": "./dist/varstor-webextension.js"
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",