react-native-onyx 2.0.80 → 2.0.82

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
@@ -50,6 +50,10 @@ value will be saved to storage after the default value.</p>
50
50
  <dt><a href="#update">update(data)</a> ⇒</dt>
51
51
  <dd><p>Insert API responses and lifecycle data into Onyx</p>
52
52
  </dd>
53
+ <dt><a href="#setCollection">setCollection(collectionKey, collection)</a></dt>
54
+ <dd><p>Sets a collection by replacing all existing collection members with new values.
55
+ Any existing collection members not included in the new data will be removed.</p>
56
+ </dd>
53
57
  </dl>
54
58
 
55
59
  <a name="init"></a>
@@ -209,3 +213,23 @@ Insert API responses and lifecycle data into Onyx
209
213
  | --- | --- |
210
214
  | data | An array of objects with update expressions |
211
215
 
216
+ <a name="setCollection"></a>
217
+
218
+ ## setCollection(collectionKey, collection)
219
+ Sets a collection by replacing all existing collection members with new values.
220
+ Any existing collection members not included in the new data will be removed.
221
+
222
+ **Kind**: global function
223
+
224
+ | Param | Description |
225
+ | --- | --- |
226
+ | collectionKey | e.g. `ONYXKEYS.COLLECTION.REPORT` |
227
+ | collection | Object collection keyed by individual collection member keys and values |
228
+
229
+ **Example**
230
+ ```js
231
+ Onyx.setCollection(ONYXKEYS.COLLECTION.REPORT, {
232
+ [`${ONYXKEYS.COLLECTION.REPORT}1`]: report1,
233
+ [`${ONYXKEYS.COLLECTION.REPORT}2`]: report2,
234
+ });
235
+ ```
package/dist/Onyx.d.ts CHANGED
@@ -119,11 +119,26 @@ declare function clear(keysToPreserve?: OnyxKey[]): Promise<void>;
119
119
  * @returns resolves when all operations are complete
120
120
  */
121
121
  declare function update(data: OnyxUpdate[]): Promise<void>;
122
+ /**
123
+ * Sets a collection by replacing all existing collection members with new values.
124
+ * Any existing collection members not included in the new data will be removed.
125
+ *
126
+ * @example
127
+ * Onyx.setCollection(ONYXKEYS.COLLECTION.REPORT, {
128
+ * [`${ONYXKEYS.COLLECTION.REPORT}1`]: report1,
129
+ * [`${ONYXKEYS.COLLECTION.REPORT}2`]: report2,
130
+ * });
131
+ *
132
+ * @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT`
133
+ * @param collection Object collection keyed by individual collection member keys and values
134
+ */
135
+ declare function setCollection<TKey extends CollectionKeyBase, TMap extends string>(collectionKey: TKey, collection: OnyxMergeCollectionInput<TMap>): Promise<void>;
122
136
  declare const Onyx: {
123
137
  readonly METHOD: {
124
138
  readonly SET: "set";
125
139
  readonly MERGE: "merge";
126
140
  readonly MERGE_COLLECTION: "mergecollection";
141
+ readonly SET_COLLECTION: "setcollection";
127
142
  readonly MULTI_SET: "multiset";
128
143
  readonly CLEAR: "clear";
129
144
  };
@@ -133,6 +148,7 @@ declare const Onyx: {
133
148
  readonly multiSet: typeof multiSet;
134
149
  readonly merge: typeof merge;
135
150
  readonly mergeCollection: typeof mergeCollection;
151
+ readonly setCollection: typeof setCollection;
136
152
  readonly update: typeof update;
137
153
  readonly clear: typeof clear;
138
154
  readonly init: typeof init;
package/dist/Onyx.js CHANGED
@@ -515,7 +515,7 @@ function updateSnapshots(data) {
515
515
  function update(data) {
516
516
  // First, validate the Onyx object is in the format we expect
517
517
  data.forEach(({ onyxMethod, key, value }) => {
518
- if (![OnyxUtils_1.default.METHOD.CLEAR, OnyxUtils_1.default.METHOD.SET, OnyxUtils_1.default.METHOD.MERGE, OnyxUtils_1.default.METHOD.MERGE_COLLECTION, OnyxUtils_1.default.METHOD.MULTI_SET].includes(onyxMethod)) {
518
+ if (!Object.values(OnyxUtils_1.default.METHOD).includes(onyxMethod)) {
519
519
  throw new Error(`Invalid onyxMethod ${onyxMethod} in Onyx update.`);
520
520
  }
521
521
  if (onyxMethod === OnyxUtils_1.default.METHOD.MULTI_SET) {
@@ -552,18 +552,14 @@ function update(data) {
552
552
  const promises = [];
553
553
  let clearPromise = Promise.resolve();
554
554
  data.forEach(({ onyxMethod, key, value }) => {
555
- switch (onyxMethod) {
556
- case OnyxUtils_1.default.METHOD.SET:
557
- enqueueSetOperation(key, value);
558
- break;
559
- case OnyxUtils_1.default.METHOD.MERGE:
560
- enqueueMergeOperation(key, value);
561
- break;
562
- case OnyxUtils_1.default.METHOD.MERGE_COLLECTION: {
555
+ const handlers = {
556
+ [OnyxUtils_1.default.METHOD.SET]: enqueueSetOperation,
557
+ [OnyxUtils_1.default.METHOD.MERGE]: enqueueMergeOperation,
558
+ [OnyxUtils_1.default.METHOD.MERGE_COLLECTION]: () => {
563
559
  const collection = value;
564
560
  if (!OnyxUtils_1.default.isValidNonEmptyCollectionForMerge(collection)) {
565
561
  Logger.logInfo('mergeCollection enqueued within update() with invalid or empty value. Skipping this operation.');
566
- break;
562
+ return;
567
563
  }
568
564
  // Confirm all the collection keys belong to the same parent
569
565
  const collectionKeys = Object.keys(collection);
@@ -571,17 +567,14 @@ function update(data) {
571
567
  const mergedCollection = collection;
572
568
  collectionKeys.forEach((collectionKey) => enqueueMergeOperation(collectionKey, mergedCollection[collectionKey]));
573
569
  }
574
- break;
575
- }
576
- case OnyxUtils_1.default.METHOD.MULTI_SET:
577
- Object.entries(value).forEach(([entryKey, entryValue]) => enqueueSetOperation(entryKey, entryValue));
578
- break;
579
- case OnyxUtils_1.default.METHOD.CLEAR:
570
+ },
571
+ [OnyxUtils_1.default.METHOD.SET_COLLECTION]: (k, v) => promises.push(() => setCollection(k, v)),
572
+ [OnyxUtils_1.default.METHOD.MULTI_SET]: (k, v) => Object.entries(v).forEach(([entryKey, entryValue]) => enqueueSetOperation(entryKey, entryValue)),
573
+ [OnyxUtils_1.default.METHOD.CLEAR]: () => {
580
574
  clearPromise = clear();
581
- break;
582
- default:
583
- break;
584
- }
575
+ },
576
+ };
577
+ handlers[onyxMethod](key, value);
585
578
  });
586
579
  // Group all the collection-related keys and update each collection in a single `mergeCollection` call.
587
580
  // This is needed to prevent multiple `mergeCollection` calls for the same collection and `merge` calls for the individual items of the said collection.
@@ -632,6 +625,39 @@ function update(data) {
632
625
  .then(() => updateSnapshots(data))
633
626
  .then(() => undefined);
634
627
  }
628
+ /**
629
+ * Sets a collection by replacing all existing collection members with new values.
630
+ * Any existing collection members not included in the new data will be removed.
631
+ *
632
+ * @example
633
+ * Onyx.setCollection(ONYXKEYS.COLLECTION.REPORT, {
634
+ * [`${ONYXKEYS.COLLECTION.REPORT}1`]: report1,
635
+ * [`${ONYXKEYS.COLLECTION.REPORT}2`]: report2,
636
+ * });
637
+ *
638
+ * @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT`
639
+ * @param collection Object collection keyed by individual collection member keys and values
640
+ */
641
+ function setCollection(collectionKey, collection) {
642
+ const newCollectionKeys = Object.keys(collection);
643
+ if (!OnyxUtils_1.default.doAllCollectionItemsBelongToSameParent(collectionKey, newCollectionKeys)) {
644
+ Logger.logAlert(`setCollection called with keys that do not belong to the same parent ${collectionKey}. Skipping this update.`);
645
+ return Promise.resolve();
646
+ }
647
+ return OnyxUtils_1.default.getAllKeys().then((persistedKeys) => {
648
+ const mutableCollection = Object.assign({}, collection);
649
+ persistedKeys.forEach((key) => {
650
+ if (!key.startsWith(collectionKey)) {
651
+ return;
652
+ }
653
+ if (newCollectionKeys.includes(key)) {
654
+ return;
655
+ }
656
+ mutableCollection[key] = null;
657
+ });
658
+ return multiSet(mutableCollection);
659
+ });
660
+ }
635
661
  const Onyx = {
636
662
  METHOD: OnyxUtils_1.default.METHOD,
637
663
  connect,
@@ -640,6 +666,7 @@ const Onyx = {
640
666
  multiSet,
641
667
  merge,
642
668
  mergeCollection,
669
+ setCollection,
643
670
  update,
644
671
  clear,
645
672
  init,
@@ -6,6 +6,7 @@ declare const METHOD: {
6
6
  readonly SET: "set";
7
7
  readonly MERGE: "merge";
8
8
  readonly MERGE_COLLECTION: "mergecollection";
9
+ readonly SET_COLLECTION: "setcollection";
9
10
  readonly MULTI_SET: "multiset";
10
11
  readonly CLEAR: "clear";
11
12
  };
@@ -228,6 +229,7 @@ declare const OnyxUtils: {
228
229
  readonly SET: "set";
229
230
  readonly MERGE: "merge";
230
231
  readonly MERGE_COLLECTION: "mergecollection";
232
+ readonly SET_COLLECTION: "setcollection";
231
233
  readonly MULTI_SET: "multiset";
232
234
  readonly CLEAR: "clear";
233
235
  };
@@ -276,4 +278,5 @@ declare const OnyxUtils: {
276
278
  unsubscribeFromKey: typeof unsubscribeFromKey;
277
279
  getEvictionBlocklist: typeof getEvictionBlocklist;
278
280
  };
281
+ export type { OnyxMethod };
279
282
  export default OnyxUtils;
package/dist/OnyxUtils.js CHANGED
@@ -44,6 +44,7 @@ const METHOD = {
44
44
  SET: 'set',
45
45
  MERGE: 'merge',
46
46
  MERGE_COLLECTION: 'mergecollection',
47
+ SET_COLLECTION: 'setcollection',
47
48
  MULTI_SET: 'multiset',
48
49
  CLEAR: 'clear',
49
50
  };
package/dist/types.d.ts CHANGED
@@ -2,6 +2,7 @@ import type { Merge } from 'type-fest';
2
2
  import type { BuiltIns } from 'type-fest/source/internal';
3
3
  import type OnyxUtils from './OnyxUtils';
4
4
  import type { WithOnyxInstance, WithOnyxState } from './withOnyx/types';
5
+ import type { OnyxMethod } from './OnyxUtils';
5
6
  /**
6
7
  * Utility type that excludes `null` from the type `TValue`.
7
8
  */
@@ -329,35 +330,43 @@ type OnyxMergeInput<TKey extends OnyxKey> = OnyxInput<TKey>;
329
330
  * This represents the value that can be passed to `Onyx.merge` and to `Onyx.update` with the method "MERGE"
330
331
  */
331
332
  type OnyxMergeCollectionInput<TKey extends OnyxKey, TMap = object> = Collection<TKey, NonNullable<OnyxInput<TKey>>, TMap>;
332
- /**
333
- * Represents different kinds of updates that can be passed to `Onyx.update()` method. It is a discriminated union of
334
- * different update methods (`SET`, `MERGE`, `MERGE_COLLECTION`), each with their own key and value structure.
335
- */
336
- type OnyxUpdate = {
337
- [TKey in OnyxKey]: {
338
- onyxMethod: typeof OnyxUtils.METHOD.SET;
339
- key: TKey;
340
- value: OnyxSetInput<TKey>;
341
- } | {
342
- onyxMethod: typeof OnyxUtils.METHOD.MULTI_SET;
343
- key: TKey;
333
+ type OnyxMethodMap = typeof OnyxUtils.METHOD;
334
+ type OnyxMethodValueMap = {
335
+ [OnyxUtils.METHOD.SET]: {
336
+ key: OnyxKey;
337
+ value: OnyxSetInput<OnyxKey>;
338
+ };
339
+ [OnyxUtils.METHOD.MULTI_SET]: {
340
+ key: OnyxKey;
344
341
  value: OnyxMultiSetInput;
345
- } | {
346
- onyxMethod: typeof OnyxUtils.METHOD.MERGE;
347
- key: TKey;
348
- value: OnyxMergeInput<TKey>;
349
- } | {
350
- onyxMethod: typeof OnyxUtils.METHOD.CLEAR;
351
- key: TKey;
342
+ };
343
+ [OnyxUtils.METHOD.MERGE]: {
344
+ key: OnyxKey;
345
+ value: OnyxMergeInput<OnyxKey>;
346
+ };
347
+ [OnyxUtils.METHOD.CLEAR]: {
348
+ key: OnyxKey;
352
349
  value?: undefined;
353
350
  };
354
- }[OnyxKey] | {
355
- [TKey in CollectionKeyBase]: {
356
- onyxMethod: typeof OnyxUtils.METHOD.MERGE_COLLECTION;
357
- key: TKey;
358
- value: OnyxMergeCollectionInput<TKey>;
351
+ [OnyxUtils.METHOD.MERGE_COLLECTION]: {
352
+ key: CollectionKeyBase;
353
+ value: OnyxMergeCollectionInput<CollectionKeyBase>;
359
354
  };
360
- }[CollectionKeyBase];
355
+ [OnyxUtils.METHOD.SET_COLLECTION]: {
356
+ key: CollectionKeyBase;
357
+ value: OnyxMergeCollectionInput<CollectionKeyBase>;
358
+ };
359
+ };
360
+ /**
361
+ * OnyxUpdate type includes all onyx methods used in OnyxMethodValueMap.
362
+ * If a new method is added to OnyxUtils.METHOD constant, it must be added to OnyxMethodValueMap type.
363
+ * Otherwise it will show static type errors.
364
+ */
365
+ type OnyxUpdate = {
366
+ [Method in OnyxMethod]: {
367
+ onyxMethod: Method;
368
+ } & OnyxMethodValueMap[Method];
369
+ }[OnyxMethod];
361
370
  /**
362
371
  * Represents the options used in `Onyx.init()` method.
363
372
  */
@@ -393,4 +402,4 @@ type MixedOperationsQueue = {
393
402
  merge: OnyxInputKeyValueMapping;
394
403
  set: OnyxInputKeyValueMapping;
395
404
  };
396
- export type { BaseConnectOptions, Collection, CollectionConnectCallback, CollectionConnectOptions, CollectionKey, CollectionKeyBase, ConnectOptions, CustomTypeOptions, DeepRecord, DefaultConnectCallback, DefaultConnectOptions, ExtractOnyxCollectionValue, GenericFunction, InitOptions, Key, KeyValueMapping, Mapping, NonNull, NonUndefined, OnyxInputKeyValueMapping, NullishDeep, OnyxCollection, OnyxEntry, OnyxKey, OnyxInputValue, OnyxCollectionInputValue, OnyxInput, OnyxSetInput, OnyxMultiSetInput, OnyxMergeInput, OnyxMergeCollectionInput, OnyxUpdate, OnyxValue, Selector, WithOnyxConnectOptions, MixedOperationsQueue, };
405
+ export type { BaseConnectOptions, Collection, CollectionConnectCallback, CollectionConnectOptions, CollectionKey, CollectionKeyBase, ConnectOptions, CustomTypeOptions, DeepRecord, DefaultConnectCallback, DefaultConnectOptions, ExtractOnyxCollectionValue, GenericFunction, InitOptions, Key, KeyValueMapping, Mapping, NonNull, NonUndefined, OnyxInputKeyValueMapping, NullishDeep, OnyxCollection, OnyxEntry, OnyxKey, OnyxInputValue, OnyxCollectionInputValue, OnyxInput, OnyxSetInput, OnyxMultiSetInput, OnyxMergeInput, OnyxMergeCollectionInput, OnyxMethod, OnyxMethodMap, OnyxUpdate, OnyxValue, Selector, WithOnyxConnectOptions, MixedOperationsQueue, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "2.0.80",
3
+ "version": "2.0.82",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",