react-native-onyx 2.0.81 → 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/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.
@@ -278,4 +278,5 @@ declare const OnyxUtils: {
278
278
  unsubscribeFromKey: typeof unsubscribeFromKey;
279
279
  getEvictionBlocklist: typeof getEvictionBlocklist;
280
280
  };
281
+ export type { OnyxMethod };
281
282
  export default OnyxUtils;
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.81",
3
+ "version": "2.0.82",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",