react-native-onyx 1.0.4 → 1.0.7
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/API.md +66 -16
- package/README.md +4 -4
- package/dist/web.development.js +19 -12
- package/dist/web.development.js.map +1 -1
- package/dist/web.min.js +1 -1
- package/dist/web.min.js.map +1 -1
- package/lib/Onyx.js +19 -12
- package/package.json +1 -1
package/API.md
CHANGED
|
@@ -11,6 +11,12 @@
|
|
|
11
11
|
<dt><a href="#disconnect">disconnect(connectionID, [keyToRemoveFromEvictionBlocklist])</a></dt>
|
|
12
12
|
<dd><p>Remove the listener for a react component</p>
|
|
13
13
|
</dd>
|
|
14
|
+
<dt><a href="#notifySubscribersOnNextTick">notifySubscribersOnNextTick(key, value)</a></dt>
|
|
15
|
+
<dd><p>This method mostly exists for historical reasons as this library was initially designed without a memory cache and one was added later.
|
|
16
|
+
For this reason, Onyx works more similar to what you might expect from a native AsyncStorage with reads, writes, etc all becoming
|
|
17
|
+
available async. Since we have code in our main applications that might expect things to work this way it's not safe to change this
|
|
18
|
+
behavior just yet.</p>
|
|
19
|
+
</dd>
|
|
14
20
|
<dt><a href="#set">set(key, value)</a> ⇒ <code>Promise</code></dt>
|
|
15
21
|
<dd><p>Write a value to our store with the given key</p>
|
|
16
22
|
</dd>
|
|
@@ -29,6 +35,19 @@ applied in the order they were called. Note: <code>Onyx.set()</code> calls do no
|
|
|
29
35
|
</dd>
|
|
30
36
|
<dt><a href="#clear">clear()</a> ⇒ <code>Promise.<void></code></dt>
|
|
31
37
|
<dd><p>Clear out all the data in the store</p>
|
|
38
|
+
<p>Note that calling Onyx.clear() and then Onyx.set() on a key with a default
|
|
39
|
+
key state may store an unexpected value in Storage.</p>
|
|
40
|
+
<p>E.g.
|
|
41
|
+
Onyx.clear();
|
|
42
|
+
Onyx.set(ONYXKEYS.DEFAULT_KEY, 'default');
|
|
43
|
+
Storage.getItem(ONYXKEYS.DEFAULT_KEY)
|
|
44
|
+
.then((storedValue) => console.log(storedValue));
|
|
45
|
+
null is logged instead of the expected 'default'</p>
|
|
46
|
+
<p>Onyx.set() might call Storage.setItem() before Onyx.clear() calls
|
|
47
|
+
Storage.setItem(). Use Onyx.merge() instead if possible. Onyx.merge() calls
|
|
48
|
+
Onyx.get(key) before calling Storage.setItem() via Onyx.set().
|
|
49
|
+
Storage.setItem() from Onyx.clear() will have already finished and the merged
|
|
50
|
+
value will be saved to storage after the default value.</p>
|
|
32
51
|
</dd>
|
|
33
52
|
<dt><a href="#mergeCollection">mergeCollection(collectionKey, collection)</a> ⇒ <code>Promise</code></dt>
|
|
34
53
|
<dd><p>Merges a collection based on their keys</p>
|
|
@@ -46,8 +65,8 @@ applied in the order they were called. Note: <code>Onyx.set()</code> calls do no
|
|
|
46
65
|
## connect(mapping) ⇒ <code>Number</code>
|
|
47
66
|
Subscribes a react component's state directly to a store key
|
|
48
67
|
|
|
49
|
-
**Kind**: global function
|
|
50
|
-
**Returns**: <code>Number</code> - an ID to use when calling disconnect
|
|
68
|
+
**Kind**: global function
|
|
69
|
+
**Returns**: <code>Number</code> - an ID to use when calling disconnect
|
|
51
70
|
|
|
52
71
|
| Param | Type | Description |
|
|
53
72
|
| --- | --- | --- |
|
|
@@ -58,7 +77,7 @@ Subscribes a react component's state directly to a store key
|
|
|
58
77
|
| [mapping.callback] | <code>function</code> | a method that will be called with changed data This is used by any non-React code to connect to Onyx |
|
|
59
78
|
| [mapping.initWithStoredValues] | <code>Boolean</code> | If set to false, then no data will be prefilled into the component |
|
|
60
79
|
|
|
61
|
-
**Example**
|
|
80
|
+
**Example**
|
|
62
81
|
```js
|
|
63
82
|
const connectionID = Onyx.connect({
|
|
64
83
|
key: ONYXKEYS.SESSION,
|
|
@@ -70,23 +89,38 @@ const connectionID = Onyx.connect({
|
|
|
70
89
|
## disconnect(connectionID, [keyToRemoveFromEvictionBlocklist])
|
|
71
90
|
Remove the listener for a react component
|
|
72
91
|
|
|
73
|
-
**Kind**: global function
|
|
92
|
+
**Kind**: global function
|
|
74
93
|
|
|
75
94
|
| Param | Type | Description |
|
|
76
95
|
| --- | --- | --- |
|
|
77
96
|
| connectionID | <code>Number</code> | unique id returned by call to Onyx.connect() |
|
|
78
97
|
| [keyToRemoveFromEvictionBlocklist] | <code>String</code> | |
|
|
79
98
|
|
|
80
|
-
**Example**
|
|
99
|
+
**Example**
|
|
81
100
|
```js
|
|
82
101
|
Onyx.disconnect(connectionID);
|
|
83
102
|
```
|
|
103
|
+
<a name="notifySubscribersOnNextTick"></a>
|
|
104
|
+
|
|
105
|
+
## notifySubscribersOnNextTick(key, value)
|
|
106
|
+
This method mostly exists for historical reasons as this library was initially designed without a memory cache and one was added later.
|
|
107
|
+
For this reason, Onyx works more similar to what you might expect from a native AsyncStorage with reads, writes, etc all becoming
|
|
108
|
+
available async. Since we have code in our main applications that might expect things to work this way it's not safe to change this
|
|
109
|
+
behavior just yet.
|
|
110
|
+
|
|
111
|
+
**Kind**: global function
|
|
112
|
+
|
|
113
|
+
| Param | Type |
|
|
114
|
+
| --- | --- |
|
|
115
|
+
| key | <code>String</code> |
|
|
116
|
+
| value | <code>\*</code> |
|
|
117
|
+
|
|
84
118
|
<a name="set"></a>
|
|
85
119
|
|
|
86
120
|
## set(key, value) ⇒ <code>Promise</code>
|
|
87
121
|
Write a value to our store with the given key
|
|
88
122
|
|
|
89
|
-
**Kind**: global function
|
|
123
|
+
**Kind**: global function
|
|
90
124
|
|
|
91
125
|
| Param | Type | Description |
|
|
92
126
|
| --- | --- | --- |
|
|
@@ -98,13 +132,13 @@ Write a value to our store with the given key
|
|
|
98
132
|
## multiSet(data) ⇒ <code>Promise</code>
|
|
99
133
|
Sets multiple keys and values
|
|
100
134
|
|
|
101
|
-
**Kind**: global function
|
|
135
|
+
**Kind**: global function
|
|
102
136
|
|
|
103
137
|
| Param | Type | Description |
|
|
104
138
|
| --- | --- | --- |
|
|
105
139
|
| data | <code>Object</code> | object keyed by ONYXKEYS and the values to set |
|
|
106
140
|
|
|
107
|
-
**Example**
|
|
141
|
+
**Example**
|
|
108
142
|
```js
|
|
109
143
|
Onyx.multiSet({'key1': 'a', 'key2': 'b'});
|
|
110
144
|
```
|
|
@@ -122,14 +156,14 @@ Calls to `Onyx.merge()` are batched so that any calls performed in a single tick
|
|
|
122
156
|
applied in the order they were called. Note: `Onyx.set()` calls do not work this way so use caution when mixing
|
|
123
157
|
`Onyx.merge()` and `Onyx.set()`.
|
|
124
158
|
|
|
125
|
-
**Kind**: global function
|
|
159
|
+
**Kind**: global function
|
|
126
160
|
|
|
127
161
|
| Param | Type | Description |
|
|
128
162
|
| --- | --- | --- |
|
|
129
163
|
| key | <code>String</code> | ONYXKEYS key |
|
|
130
164
|
| value | <code>Object</code> \| <code>Array</code> | Object or Array value to merge |
|
|
131
165
|
|
|
132
|
-
**Example**
|
|
166
|
+
**Example**
|
|
133
167
|
```js
|
|
134
168
|
Onyx.merge(ONYXKEYS.EMPLOYEE_LIST, ['Joe']); // -> ['Joe']
|
|
135
169
|
Onyx.merge(ONYXKEYS.EMPLOYEE_LIST, ['Jack']); // -> ['Joe', 'Jack']
|
|
@@ -141,20 +175,36 @@ Onyx.merge(ONYXKEYS.POLICY, {name: 'My Workspace'}); // -> {id: 1, name: 'My Wor
|
|
|
141
175
|
## clear() ⇒ <code>Promise.<void></code>
|
|
142
176
|
Clear out all the data in the store
|
|
143
177
|
|
|
144
|
-
|
|
178
|
+
Note that calling Onyx.clear() and then Onyx.set() on a key with a default
|
|
179
|
+
key state may store an unexpected value in Storage.
|
|
180
|
+
|
|
181
|
+
E.g.
|
|
182
|
+
Onyx.clear();
|
|
183
|
+
Onyx.set(ONYXKEYS.DEFAULT_KEY, 'default');
|
|
184
|
+
Storage.getItem(ONYXKEYS.DEFAULT_KEY)
|
|
185
|
+
.then((storedValue) => console.log(storedValue));
|
|
186
|
+
null is logged instead of the expected 'default'
|
|
187
|
+
|
|
188
|
+
Onyx.set() might call Storage.setItem() before Onyx.clear() calls
|
|
189
|
+
Storage.setItem(). Use Onyx.merge() instead if possible. Onyx.merge() calls
|
|
190
|
+
Onyx.get(key) before calling Storage.setItem() via Onyx.set().
|
|
191
|
+
Storage.setItem() from Onyx.clear() will have already finished and the merged
|
|
192
|
+
value will be saved to storage after the default value.
|
|
193
|
+
|
|
194
|
+
**Kind**: global function
|
|
145
195
|
<a name="mergeCollection"></a>
|
|
146
196
|
|
|
147
197
|
## mergeCollection(collectionKey, collection) ⇒ <code>Promise</code>
|
|
148
198
|
Merges a collection based on their keys
|
|
149
199
|
|
|
150
|
-
**Kind**: global function
|
|
200
|
+
**Kind**: global function
|
|
151
201
|
|
|
152
202
|
| Param | Type | Description |
|
|
153
203
|
| --- | --- | --- |
|
|
154
204
|
| collectionKey | <code>String</code> | e.g. `ONYXKEYS.COLLECTION.REPORT` |
|
|
155
205
|
| collection | <code>Object</code> | Object collection keyed by individual collection member keys and values |
|
|
156
206
|
|
|
157
|
-
**Example**
|
|
207
|
+
**Example**
|
|
158
208
|
```js
|
|
159
209
|
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, {
|
|
160
210
|
[`${ONYXKEYS.COLLECTION.REPORT}1`]: report1,
|
|
@@ -166,7 +216,7 @@ Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, {
|
|
|
166
216
|
## update(data)
|
|
167
217
|
Insert API responses and lifecycle data into Onyx
|
|
168
218
|
|
|
169
|
-
**Kind**: global function
|
|
219
|
+
**Kind**: global function
|
|
170
220
|
|
|
171
221
|
| Param | Type | Description |
|
|
172
222
|
| --- | --- | --- |
|
|
@@ -177,7 +227,7 @@ Insert API responses and lifecycle data into Onyx
|
|
|
177
227
|
## init([options])
|
|
178
228
|
Initialize the store with actions and listening for storage events
|
|
179
229
|
|
|
180
|
-
**Kind**: global function
|
|
230
|
+
**Kind**: global function
|
|
181
231
|
|
|
182
232
|
| Param | Type | Default | Description |
|
|
183
233
|
| --- | --- | --- | --- |
|
|
@@ -190,7 +240,7 @@ Initialize the store with actions and listening for storage events
|
|
|
190
240
|
| [options.shouldSyncMultipleInstances] | <code>Boolean</code> | | Auto synchronize storage events between multiple instances of Onyx running in different tabs/windows. Defaults to true for platforms that support local storage (web/desktop) |
|
|
191
241
|
| [option.keysToDisableSyncEvents] | <code>Array.<String></code> | <code>[]</code> | Contains keys for which we want to disable sync event across tabs. |
|
|
192
242
|
|
|
193
|
-
**Example**
|
|
243
|
+
**Example**
|
|
194
244
|
```js
|
|
195
245
|
Onyx.init({
|
|
196
246
|
keys: ONYXKEYS,
|
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ Persistent storage solution wrapped in a Pub/Sub library.
|
|
|
20
20
|
Onyx is published to [`npm`](https://www.npmjs.com/package/react-native-onyx)
|
|
21
21
|
|
|
22
22
|
```shell
|
|
23
|
-
npm install react-native-onyx --save
|
|
23
|
+
npm install react-native-onyx --save
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
## Initialization
|
|
@@ -42,8 +42,8 @@ Onyx.init(config);
|
|
|
42
42
|
```
|
|
43
43
|
|
|
44
44
|
### Usage in non react-native projects
|
|
45
|
-
Onyx can be used in non react-native projects, by leveraging the `browser` field in `package.json`
|
|
46
|
-
Bundlers like Webpack respect that field and import code from the specified path
|
|
45
|
+
Onyx can be used in non react-native projects, by leveraging the `browser` field in `package.json`
|
|
46
|
+
Bundlers like Webpack respect that field and import code from the specified path
|
|
47
47
|
We import Onyx the same way shown above - `import Onyx from 'react-native-onyx'`
|
|
48
48
|
|
|
49
49
|
## Setting data
|
|
@@ -148,7 +148,7 @@ Under the hood storage access calls are delegated to a [`StorageProvider`](lib/s
|
|
|
148
148
|
Some platforms (like web and desktop) might use the same storage provider
|
|
149
149
|
|
|
150
150
|
If a platform needs to use a separate library (like using MMVK for react-native) it should be added in the following way:
|
|
151
|
-
1. Create a `StorageProvider.js` at [lib/storage/providers](lib/storage/providers)
|
|
151
|
+
1. Create a `StorageProvider.js` at [lib/storage/providers](lib/storage/providers)
|
|
152
152
|
Reference an existing [StorageProvider](lib/storage/providers/AsyncStorage.js) for the interface that has to be implemented
|
|
153
153
|
2. Update the factory at [lib/storage/index.web.js](lib/storage/index.web.js) and [lib/storage/index.native.js](lib/storage/index.native.js) to return the newly created Provider for the desired Platform(s)
|
|
154
154
|
|
package/dist/web.development.js
CHANGED
|
@@ -971,6 +971,20 @@ function disconnect(connectionID, keyToRemoveFromEvictionBlocklist) {
|
|
|
971
971
|
delete callbackToStateMapping[connectionID];
|
|
972
972
|
}
|
|
973
973
|
|
|
974
|
+
/**
|
|
975
|
+
* This method mostly exists for historical reasons as this library was initially designed without a memory cache and one was added later.
|
|
976
|
+
* For this reason, Onyx works more similar to what you might expect from a native AsyncStorage with reads, writes, etc all becoming
|
|
977
|
+
* available async. Since we have code in our main applications that might expect things to work this way it's not safe to change this
|
|
978
|
+
* behavior just yet.
|
|
979
|
+
*
|
|
980
|
+
* @param {String} key
|
|
981
|
+
* @param {*} value
|
|
982
|
+
*/
|
|
983
|
+
// eslint-disable-next-line rulesdir/no-negated-variables
|
|
984
|
+
function notifySubscribersOnNextTick(key, value) {
|
|
985
|
+
Promise.resolve().then(function () {return keyChanged(key, value);});
|
|
986
|
+
}
|
|
987
|
+
|
|
974
988
|
/**
|
|
975
989
|
* Remove a key from Onyx and update the subscribers
|
|
976
990
|
*
|
|
@@ -981,10 +995,7 @@ function disconnect(connectionID, keyToRemoveFromEvictionBlocklist) {
|
|
|
981
995
|
function remove(key) {
|
|
982
996
|
// Cache the fact that the value was removed
|
|
983
997
|
_OnyxCache.default.set(key, null);
|
|
984
|
-
|
|
985
|
-
// Optimistically inform subscribers on the next tick
|
|
986
|
-
Promise.resolve().then(function () {return keyChanged(key, null);});
|
|
987
|
-
|
|
998
|
+
notifySubscribersOnNextTick(key, null);
|
|
988
999
|
return _storage.default.removeItem(key);
|
|
989
1000
|
}
|
|
990
1001
|
|
|
@@ -1040,9 +1051,7 @@ function set(key, value) {
|
|
|
1040
1051
|
|
|
1041
1052
|
// Adds the key to cache when it's not available
|
|
1042
1053
|
_OnyxCache.default.set(key, value);
|
|
1043
|
-
|
|
1044
|
-
// Optimistically inform subscribers on the next tick
|
|
1045
|
-
Promise.resolve().then(function () {return keyChanged(key, value);});
|
|
1054
|
+
notifySubscribersOnNextTick(key, value);
|
|
1046
1055
|
|
|
1047
1056
|
// Write the thing to persistent storage, which will trigger a storage event for any other tabs open on this domain
|
|
1048
1057
|
return _storage.default.setItem(key, value).
|
|
@@ -1075,7 +1084,7 @@ function multiSet(data) {
|
|
|
1075
1084
|
_underscore.default.each(data, function (val, key) {
|
|
1076
1085
|
// Update cache and optimistically inform subscribers on the next tick
|
|
1077
1086
|
_OnyxCache.default.set(key, val);
|
|
1078
|
-
|
|
1087
|
+
notifySubscribersOnNextTick(key, val);
|
|
1079
1088
|
});
|
|
1080
1089
|
|
|
1081
1090
|
return _storage.default.multiSet(keyValuePairs).
|
|
@@ -1224,11 +1233,9 @@ function clear() {
|
|
|
1224
1233
|
_underscore.default.each(keys, function (key) {
|
|
1225
1234
|
var resetValue = (0, _get.default)(defaultKeyStates, key, null);
|
|
1226
1235
|
_OnyxCache.default.set(key, resetValue);
|
|
1227
|
-
|
|
1228
|
-
// Optimistically inform subscribers on the next tick
|
|
1229
|
-
Promise.resolve().then(function () {return keyChanged(key, resetValue);});
|
|
1236
|
+
notifySubscribersOnNextTick(key, resetValue);
|
|
1230
1237
|
});
|
|
1231
|
-
_storage.default.clear();
|
|
1238
|
+
return _storage.default.clear();
|
|
1232
1239
|
});
|
|
1233
1240
|
}
|
|
1234
1241
|
|