react-native-onyx 1.0.30 → 1.0.31

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 CHANGED
@@ -27,6 +27,11 @@ For this reason, Onyx works more similar to what you might expect from a native
27
27
  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
28
28
  behavior just yet.</p>
29
29
  </dd>
30
+ <dt><a href="#notifyCollectionSubscribersOnNextTick">notifyCollectionSubscribersOnNextTick(key, value)</a></dt>
31
+ <dd><p>This method is similar to notifySubscribersOnNextTick but it is built for working specifically with collections
32
+ so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
33
+ subscriber callbacks receive the data in a different format than they normally expect and it breaks code.</p>
34
+ </dd>
30
35
  <dt><a href="#set">set(key, value)</a> ⇒ <code>Promise</code></dt>
31
36
  <dd><p>Write a value to our store with the given key</p>
32
37
  </dd>
@@ -43,7 +48,7 @@ concatenate. See here: <a href="https://github.com/lodash/lodash/issues/2872">ht
43
48
  applied in the order they were called. Note: <code>Onyx.set()</code> calls do not work this way so use caution when mixing
44
49
  <code>Onyx.merge()</code> and <code>Onyx.set()</code>.</p>
45
50
  </dd>
46
- <dt><a href="#clear">clear()</a> ⇒ <code>Promise.&lt;void&gt;</code></dt>
51
+ <dt><a href="#clear">clear(keysToPreserve)</a> ⇒ <code>Promise.&lt;void&gt;</code></dt>
47
52
  <dd><p>Clear out all the data in the store</p>
48
53
  <p>Note that calling Onyx.clear() and then Onyx.set() on a key with a default
49
54
  key state may store an unexpected value in Storage.</p>
@@ -62,7 +67,7 @@ value will be saved to storage after the default value.</p>
62
67
  <dt><a href="#mergeCollection">mergeCollection(collectionKey, collection)</a> ⇒ <code>Promise</code></dt>
63
68
  <dd><p>Merges a collection based on their keys</p>
64
69
  </dd>
65
- <dt><a href="#update">update(data)</a></dt>
70
+ <dt><a href="#update">update(data)</a> ⇒ <code>Promise</code></dt>
66
71
  <dd><p>Insert API responses and lifecycle data into Onyx</p>
67
72
  </dd>
68
73
  <dt><a href="#init">init([options])</a></dt>
@@ -123,7 +128,7 @@ Subscribes a react component's state directly to a store key
123
128
  | [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 |
124
129
  | [mapping.initWithStoredValues] | <code>Boolean</code> | If set to false, then no data will be prefilled into the component |
125
130
  | [mapping.waitForCollectionCallback] | <code>Boolean</code> | If set to true, it will return the entire collection to the callback as a single object |
126
- | [mapping.selector] | <code>String</code> \| <code>function</code> | THIS PARAM IS ONLY USED WITH withOnyx(). If included, this will be used to subscribe to a subset of an Onyx key's data. If the selector is a string, the selector is passed to lodashGet on the sourceData. If the selector is a function, the sourceData is passed to the selector and should return the simplified data. Using this setting on `withOnyx` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
131
+ | [mapping.selector] | <code>String</code> \| <code>function</code> | THIS PARAM IS ONLY USED WITH withOnyx(). If included, this will be used to subscribe to a subset of an Onyx key's data. If the selector is a string, the selector is passed to lodashGet on the sourceData. If the selector is a function, the sourceData is passed to the selector and should return the simplified data. Using this setting on `withOnyx` can have very positive performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can be expensive from a performance standpoint). |
127
132
 
128
133
  **Example**
129
134
  ```js
@@ -168,6 +173,20 @@ behavior just yet.
168
173
  ```js
169
174
  notifySubscribersOnNextTick(key, value, subscriber => subscriber.initWithStoredValues === false)
170
175
  ```
176
+ <a name="notifyCollectionSubscribersOnNextTick"></a>
177
+
178
+ ## notifyCollectionSubscribersOnNextTick(key, value)
179
+ This method is similar to notifySubscribersOnNextTick but it is built for working specifically with collections
180
+ so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
181
+ subscriber callbacks receive the data in a different format than they normally expect and it breaks code.
182
+
183
+ **Kind**: global function
184
+
185
+ | Param | Type |
186
+ | --- | --- |
187
+ | key | <code>String</code> |
188
+ | value | <code>\*</code> |
189
+
171
190
  <a name="set"></a>
172
191
 
173
192
  ## set(key, value) ⇒ <code>Promise</code>
@@ -225,7 +244,7 @@ Onyx.merge(ONYXKEYS.POLICY, {name: 'My Workspace'}); // -> {id: 1, name: 'My Wor
225
244
  ```
226
245
  <a name="clear"></a>
227
246
 
228
- ## clear() ⇒ <code>Promise.&lt;void&gt;</code>
247
+ ## clear(keysToPreserve) ⇒ <code>Promise.&lt;void&gt;</code>
229
248
  Clear out all the data in the store
230
249
 
231
250
  Note that calling Onyx.clear() and then Onyx.set() on a key with a default
@@ -245,6 +264,11 @@ Storage.setItem() from Onyx.clear() will have already finished and the merged
245
264
  value will be saved to storage after the default value.
246
265
 
247
266
  **Kind**: global function
267
+
268
+ | Param | Type | Description |
269
+ | --- | --- | --- |
270
+ | keysToPreserve | <code>Array</code> | is a list of ONYXKEYS that should not be cleared with the rest of the data |
271
+
248
272
  <a name="mergeCollection"></a>
249
273
 
250
274
  ## mergeCollection(collectionKey, collection) ⇒ <code>Promise</code>
@@ -266,10 +290,11 @@ Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, {
266
290
  ```
267
291
  <a name="update"></a>
268
292
 
269
- ## update(data)
293
+ ## update(data) ⇒ <code>Promise</code>
270
294
  Insert API responses and lifecycle data into Onyx
271
295
 
272
296
  **Kind**: global function
297
+ **Returns**: <code>Promise</code> - resolves when all operations are complete
273
298
 
274
299
  | Param | Type | Description |
275
300
  | --- | --- | --- |
@@ -1269,6 +1269,19 @@ function notifySubscribersOnNextTick(key, value, canUpdateSubscriber) {
1269
1269
  Promise.resolve().then(function () {return keyChanged(key, value, canUpdateSubscriber);});
1270
1270
  }
1271
1271
 
1272
+ /**
1273
+ * This method is similar to notifySubscribersOnNextTick but it is built for working specifically with collections
1274
+ * so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the
1275
+ * subscriber callbacks receive the data in a different format than they normally expect and it breaks code.
1276
+ *
1277
+ * @param {String} key
1278
+ * @param {*} value
1279
+ */
1280
+ // eslint-disable-next-line rulesdir/no-negated-variables
1281
+ function notifyCollectionSubscribersOnNextTick(key, value) {
1282
+ Promise.resolve().then(function () {return keysChanged(key, value);});
1283
+ }
1284
+
1272
1285
  /**
1273
1286
  * Remove a key from Onyx and update the subscribers
1274
1287
  *
@@ -1529,24 +1542,55 @@ function clear() {var keysToPreserve = arguments.length > 0 && arguments[0] !==
1529
1542
  var defaultKeys = _underscore.default.keys(defaultKeyStates);
1530
1543
 
1531
1544
  // The only keys that should not be cleared are:
1532
- // 1. Anything specifically passed in keysToPreserve (because some keys like language preferences, offline status, or activeClients need to remain in Onyx even when signed out)
1533
- // 2. Any keys with a default state (because they need to remain in Onyx as their default, and setting them to null would cause unknown behavior)
1545
+ // 1. Anything specifically passed in keysToPreserve (because some keys like language preferences, offline
1546
+ // status, or activeClients need to remain in Onyx even when signed out)
1547
+ // 2. Any keys with a default state (because they need to remain in Onyx as their default, and setting them
1548
+ // to null would cause unknown behavior)
1534
1549
  var keysToClear = _underscore.default.difference(keys, keysToPreserve, defaultKeys);
1535
1550
  keyValuesToReset.push.apply(keyValuesToReset, (0, _toConsumableArray2.default)(_underscore.default.map(keysToClear, function (key) {return [key, null];})));
1536
1551
 
1537
- // Remove any keysToPreserve from the defaultKeyStates because if they are passed in it has been explicitly called out to preserve those values instead of resetting them back
1552
+ // Remove any keysToPreserve from the defaultKeyStates because if they are passed in it has been explicitly
1553
+ // called out to preserve those values instead of resetting them back
1538
1554
  // to the default.
1539
1555
  var defaultKeyValuePairs = _underscore.default.pairs(_underscore.default.omit.apply(_underscore.default, [defaultKeyStates].concat((0, _toConsumableArray2.default)(keysToPreserve))));
1540
1556
 
1541
- // Add the default key value pairs to the keyValuesToReset so that they get set back to their default values when we clear Onyx
1557
+ // Add the default key value pairs to the keyValuesToReset so that they get set back to their default values
1558
+ // when we clear Onyx
1542
1559
  keyValuesToReset.push.apply(keyValuesToReset, (0, _toConsumableArray2.default)(defaultKeyValuePairs));
1543
1560
 
1561
+ // We now have all the key/values that need to be reset, but we're not done yet!
1562
+ // There will be two groups of key/values and they each need to be updated a little bit differently.
1563
+ // Collection keys need to be notified differently than non collection keys
1564
+ var keyValuesToResetAsCollection = {};
1565
+ var keyValuesToResetIndividually = {};
1566
+
1544
1567
  // Make sure that we also reset the cache values before clearing the values from storage.
1545
- // We do this before clearing Storage so that any call to clear() followed by merge() on a key with a default state results in the merged value getting saved, since the update
1546
- // from the merge() call would happen on the tick after the update from this clear()
1568
+ // We do this before clearing Storage so that any call to clear() followed by merge() on a key with a
1569
+ // default state results in the merged value getting saved, since the update from the merge() call would
1570
+ // happen on the tick after the update from this clear()
1547
1571
  _underscore.default.each(keyValuesToReset, function (keyValue) {
1548
- _OnyxCache.default.set(keyValue[0], keyValue[1]);
1549
- notifySubscribersOnNextTick(keyValue[0], keyValue[1]);
1572
+ var key = keyValue[0];
1573
+ var value = keyValue[1];
1574
+ _OnyxCache.default.set(key, value);
1575
+
1576
+ var collectionKey = key.substring(0, key.indexOf('_') + 1);
1577
+ if (collectionKey) {
1578
+ if (!keyValuesToResetAsCollection[collectionKey]) {
1579
+ keyValuesToResetAsCollection[collectionKey] = {};
1580
+ }
1581
+ keyValuesToResetAsCollection[collectionKey][key] = value;
1582
+ return;
1583
+ }
1584
+
1585
+ keyValuesToResetIndividually[key] = value;
1586
+ });
1587
+
1588
+ // Notify the subscribers for each key/value group so they can receive the new values
1589
+ _underscore.default.each(keyValuesToResetIndividually, function (value, key) {
1590
+ notifySubscribersOnNextTick(key, value);
1591
+ });
1592
+ _underscore.default.each(keyValuesToResetAsCollection, function (value, key) {
1593
+ notifyCollectionSubscribersOnNextTick(key, value);
1550
1594
  });
1551
1595
 
1552
1596
  return _storage.default.multiSet(keyValuesToReset);