Skip to content

Commit 541d23e

Browse files
committed
Add OnyxSetCollectionInput type and update related functions
1 parent 2979ece commit 541d23e

7 files changed

Lines changed: 40 additions & 34 deletions

File tree

lib/Onyx.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import Storage from './storage';
44
import utils from './utils';
55
import DevTools, {initDevTools} from './DevTools';
66
import type {
7-
Collection,
8-
CollectionKey,
97
CollectionKeyBase,
108
ConnectOptions,
119
InitOptions,
@@ -15,6 +13,7 @@ import type {
1513
MixedOperationsQueue,
1614
OnyxKey,
1715
OnyxMergeCollectionInput,
16+
OnyxSetCollectionInput,
1817
OnyxMergeInput,
1918
OnyxMultiSetInput,
2019
OnyxSetInput,
@@ -545,7 +544,7 @@ function update(data: OnyxUpdate[]): Promise<void> {
545544
[OnyxUtils.METHOD.SET]: enqueueSetOperation,
546545
[OnyxUtils.METHOD.MERGE]: enqueueMergeOperation,
547546
[OnyxUtils.METHOD.MERGE_COLLECTION]: () => {
548-
const collection = value as Collection<CollectionKey, unknown>;
547+
const collection = value as OnyxMergeCollectionInput<OnyxKey>;
549548
if (!OnyxUtils.isValidNonEmptyCollectionForMerge(collection)) {
550549
Logger.logInfo('mergeCollection enqueued within update() with invalid or empty value. Skipping this operation.');
551550
return;
@@ -655,7 +654,7 @@ function update(data: OnyxUpdate[]): Promise<void> {
655654
* @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT`
656655
* @param collection Object collection keyed by individual collection member keys and values
657656
*/
658-
function setCollection<TKey extends CollectionKeyBase>(collectionKey: TKey, collection: OnyxMergeCollectionInput<TKey>): Promise<void> {
657+
function setCollection<TKey extends CollectionKeyBase>(collectionKey: TKey, collection: OnyxSetCollectionInput<TKey>): Promise<void> {
659658
let resultCollection: OnyxInputKeyValueMapping = collection;
660659
let resultCollectionKeys = Object.keys(resultCollection);
661660

lib/OnyxUtils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import type {
2929
OnyxUpdate,
3030
OnyxValue,
3131
Selector,
32-
Collection,
32+
OnyxSetCollectionInput,
3333
} from './types';
3434
import type {FastMergeOptions, FastMergeResult} from './utils';
3535
import utils from './utils';
@@ -1036,7 +1036,7 @@ function initializeWithDefaultKeyStates(): Promise<void> {
10361036
/**
10371037
* Validate the collection is not empty and has a correct type before applying mergeCollection()
10381038
*/
1039-
function isValidNonEmptyCollectionForMerge<TKey extends CollectionKeyBase>(collection: Collection<TKey, unknown>): boolean {
1039+
function isValidNonEmptyCollectionForMerge<TKey extends CollectionKeyBase>(collection: OnyxMergeCollectionInput<TKey>): boolean {
10401040
return typeof collection === 'object' && !Array.isArray(collection) && !utils.isEmptyObject(collection);
10411041
}
10421042

@@ -1367,7 +1367,7 @@ function mergeCollectionWithPatches<TKey extends CollectionKeyBase>(
13671367
* @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT`
13681368
* @param collection Object collection keyed by individual collection member keys and values
13691369
*/
1370-
function partialSetCollection<TKey extends CollectionKeyBase>(collectionKey: TKey, collection: OnyxMergeCollectionInput<TKey>): Promise<void> {
1370+
function partialSetCollection<TKey extends CollectionKeyBase>(collectionKey: TKey, collection: OnyxSetCollectionInput<TKey>): Promise<void> {
13711371
let resultCollection: OnyxInputKeyValueMapping = collection;
13721372
let resultCollectionKeys = Object.keys(resultCollection);
13731373

lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
OnyxMultiSetInput,
1717
OnyxMergeInput,
1818
OnyxMergeCollectionInput,
19+
OnyxSetCollectionInput,
1920
} from './types';
2021
import type {FetchStatus, ResultMetadata, UseOnyxResult, UseOnyxOptions} from './useOnyx';
2122
import type {Connection} from './OnyxConnectionManager';
@@ -40,6 +41,7 @@ export type {
4041
OnyxMultiSetInput,
4142
OnyxMergeInput,
4243
OnyxMergeCollectionInput,
44+
OnyxSetCollectionInput,
4345
OnyxUpdate,
4446
OnyxValue,
4547
ResultMetadata,

lib/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ type OnyxMergeInput<TKey extends OnyxKey> = OnyxInput<TKey>;
321321
*/
322322
type OnyxMergeCollectionInput<TKey extends OnyxKey> = Collection<TKey, NonNullable<OnyxInput<TKey>>>;
323323

324+
/**
325+
* This represents the value that can be passed to `Onyx.setCollection` and to `Onyx.update` with the method "SET_COLLECTION"
326+
*/
327+
type OnyxSetCollectionInput<TKey extends OnyxKey> = Collection<TKey, OnyxInput<TKey>>;
328+
324329
type OnyxMethodMap = typeof OnyxUtils.METHOD;
325330

326331
/**
@@ -363,7 +368,7 @@ type OnyxUpdate =
363368
| {
364369
onyxMethod: typeof OnyxUtils.METHOD.SET_COLLECTION;
365370
key: TKey;
366-
value: OnyxMergeCollectionInput<TKey>;
371+
value: OnyxSetCollectionInput<TKey>;
367372
};
368373
}[CollectionKeyBase];
369374

@@ -476,6 +481,7 @@ export type {
476481
OnyxMultiSetInput,
477482
OnyxMergeInput,
478483
OnyxMergeCollectionInput,
484+
OnyxSetCollectionInput,
479485
OnyxMethod,
480486
OnyxMethodMap,
481487
OnyxUpdate,

tests/unit/onyxMultiMergeWebStorageTest.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
33
import Storage from '../../lib/storage';
44
import type MockedStorage from '../../lib/storage/__mocks__';
55
import type OnyxInstance from '../../lib/Onyx';
6-
import type {OnyxKey, OnyxMergeCollectionInput} from '../../lib';
6+
import type GenericCollection from '../utils/GenericCollection';
77

88
const StorageMock = Storage as unknown as typeof MockedStorage;
99

@@ -55,15 +55,15 @@ describe('Onyx.mergeCollection() and WebStorage', () => {
5555
test_1: additionalDataOne,
5656
test_2: additionalDataOne,
5757
test_3: additionalDataOne,
58-
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
58+
} as GenericCollection);
5959

6060
// And call again consecutively with different data
6161
const additionalDataTwo = {d: 'd', e: [2]};
6262
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
6363
test_1: additionalDataTwo,
6464
test_2: additionalDataTwo,
6565
test_3: additionalDataTwo,
66-
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
66+
} as GenericCollection);
6767

6868
return waitForPromisesToResolve().then(() => {
6969
const finalObject = {
@@ -103,7 +103,7 @@ describe('Onyx.mergeCollection() and WebStorage', () => {
103103
test_1: data,
104104
test_2: data,
105105
test_3: data,
106-
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
106+
} as GenericCollection);
107107

108108
return waitForPromisesToResolve()
109109
.then(() => {
@@ -125,7 +125,7 @@ describe('Onyx.mergeCollection() and WebStorage', () => {
125125
test_1: additionalData,
126126
test_2: additionalData,
127127
test_3: additionalData,
128-
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
128+
} as GenericCollection);
129129

130130
return waitForPromisesToResolve();
131131
})
@@ -164,7 +164,7 @@ describe('Onyx.mergeCollection() and WebStorage', () => {
164164
// 2nd call
165165
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
166166
test_1: {d: 'd', e: 'e'},
167-
} as unknown as OnyxMergeCollectionInput<OnyxKey>);
167+
} as GenericCollection);
168168

169169
// Last call
170170
Onyx.merge('test_1', {f: 'f'});

tests/unit/onyxTest.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
55
import OnyxUtils from '../../lib/OnyxUtils';
66
import type OnyxCache from '../../lib/OnyxCache';
77
import StorageMock from '../../lib/storage';
8-
import type {OnyxCollection, OnyxKey, OnyxMergeCollectionInput, OnyxUpdate} from '../../lib/types';
8+
import type {OnyxCollection, OnyxUpdate} from '../../lib/types';
99
import type {GenericDeepRecord} from '../types';
1010
import type GenericCollection from '../utils/GenericCollection';
1111
import type {Connection} from '../../lib/OnyxConnectionManager';
@@ -624,7 +624,7 @@ describe('Onyx', () => {
624624
ID: 345,
625625
value: 'three',
626626
},
627-
} as unknown as OnyxMergeCollectionInput<OnyxKey>)
627+
} as GenericCollection)
628628
.then(() =>
629629
// 2 key values to update and 2 new keys to add.
630630
// MergeCollection will perform a mix of multiSet and multiMerge
@@ -646,7 +646,7 @@ describe('Onyx', () => {
646646
ID: 567,
647647
value: 'one',
648648
},
649-
} as unknown as OnyxMergeCollectionInput<OnyxKey>),
649+
} as GenericCollection),
650650
)
651651
.then(() => {
652652
// 3 items on the first mergeCollection + 4 items the next mergeCollection
@@ -674,7 +674,7 @@ describe('Onyx', () => {
674674
callback: (data, key) => (valuesReceived[key] = data),
675675
});
676676

677-
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {test_1: {ID: 123}, notMyTest: {beep: 'boop'}} as unknown as OnyxMergeCollectionInput<OnyxKey>).then(() => {
677+
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {test_1: {ID: 123}, notMyTest: {beep: 'boop'}} as GenericCollection).then(() => {
678678
expect(valuesReceived).toEqual({});
679679
});
680680
});
@@ -705,7 +705,7 @@ describe('Onyx', () => {
705705
ID: 234,
706706
value: 'two',
707707
},
708-
} as unknown as OnyxMergeCollectionInput<OnyxKey>),
708+
} as GenericCollection),
709709
)
710710
.then(() => {
711711
expect(valuesReceived).toEqual({
@@ -899,7 +899,7 @@ describe('Onyx', () => {
899899
ID: 345,
900900
value: 'three',
901901
},
902-
} as unknown as OnyxMergeCollectionInput<OnyxKey>,
902+
} as GenericCollection,
903903
},
904904
]);
905905
})
@@ -1058,7 +1058,7 @@ describe('Onyx', () => {
10581058
},
10591059
};
10601060

1061-
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, initialCollectionData as unknown as OnyxMergeCollectionInput<OnyxKey>)
1061+
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, initialCollectionData as GenericCollection)
10621062
.then(() => {
10631063
// When we connect to that collection with waitForCollectionCallback = true
10641064
connection = Onyx.connect({
@@ -1091,7 +1091,7 @@ describe('Onyx', () => {
10911091
return (
10921092
waitForPromisesToResolve()
10931093
// When mergeCollection is called with an updated collection
1094-
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate as unknown as OnyxMergeCollectionInput<OnyxKey>))
1094+
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate as GenericCollection))
10951095
.then(() => {
10961096
// Then we expect the callback to have called twice, once for the initial connect call + once for the collection update
10971097
expect(mockCallback).toHaveBeenCalledTimes(2);
@@ -1120,7 +1120,7 @@ describe('Onyx', () => {
11201120
return (
11211121
waitForPromisesToResolve()
11221122
// When mergeCollection is called with an updated collection
1123-
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate as unknown as OnyxMergeCollectionInput<OnyxKey>))
1123+
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate as GenericCollection))
11241124
.then(() => {
11251125
// Then we expect the callback to have called twice, once for the initial connect call + once for the collection update
11261126
expect(mockCallback).toHaveBeenCalledTimes(2);
@@ -1268,7 +1268,7 @@ describe('Onyx', () => {
12681268
Onyx.update([
12691269
{onyxMethod: Onyx.METHOD.SET, key: ONYX_KEYS.TEST_KEY, value: 'taco'},
12701270
{onyxMethod: Onyx.METHOD.MERGE, key: ONYX_KEYS.OTHER_TEST, value: 'pizza'},
1271-
{onyxMethod: Onyx.METHOD.MERGE_COLLECTION, key: ONYX_KEYS.COLLECTION.TEST_UPDATE, value: {[itemKey]: {a: 'a'}} as OnyxMergeCollectionInput<OnyxKey>},
1271+
{onyxMethod: Onyx.METHOD.MERGE_COLLECTION, key: ONYX_KEYS.COLLECTION.TEST_UPDATE, value: {[itemKey]: {a: 'a'}} as GenericCollection},
12721272
]).then(() => {
12731273
expect(collectionCallback).toHaveBeenCalledTimes(2);
12741274
expect(collectionCallback).toHaveBeenNthCalledWith(1, undefined, undefined, undefined);
@@ -1525,10 +1525,10 @@ describe('Onyx', () => {
15251525

15261526
const initialValue = {name: 'Fluffy'};
15271527

1528-
const collectionDiff = {
1528+
const collectionDiff: GenericCollection = {
15291529
[cat]: initialValue,
15301530
[dog]: {name: 'Rex'},
1531-
} as OnyxMergeCollectionInput<OnyxKey>;
1531+
};
15321532

15331533
return Onyx.set(cat, initialValue)
15341534
.then(() => {
@@ -1630,7 +1630,7 @@ describe('Onyx', () => {
16301630
0: 'Bed',
16311631
},
16321632
},
1633-
} as OnyxMergeCollectionInput<OnyxKey>,
1633+
} as GenericCollection,
16341634
},
16351635
{
16361636
onyxMethod: Onyx.METHOD.MERGE,
@@ -1729,7 +1729,7 @@ describe('Onyx', () => {
17291729
value: {
17301730
[cat]: {age: 5, size: 'S'},
17311731
[dog]: {size: 'M'},
1732-
} as OnyxMergeCollectionInput<OnyxKey>,
1732+
} as GenericCollection,
17331733
},
17341734
{onyxMethod: Onyx.METHOD.SET, key: cat, value: {age: 3}},
17351735
{onyxMethod: Onyx.METHOD.MERGE, key: cat, value: {sound: 'meow'}},
@@ -2214,7 +2214,7 @@ describe('Onyx', () => {
22142214
value: {
22152215
[routeA]: {name: 'New Route A'},
22162216
[routeB]: {name: 'New Route B'},
2217-
} as OnyxMergeCollectionInput<OnyxKey>,
2217+
} as GenericCollection,
22182218
},
22192219
]);
22202220
})
@@ -2265,7 +2265,7 @@ describe('Onyx', () => {
22652265
key: ONYX_KEYS.COLLECTION.ROUTES,
22662266
value: {
22672267
[routeA]: {name: 'Final Route A'},
2268-
} as OnyxMergeCollectionInput<OnyxKey>,
2268+
} as GenericCollection,
22692269
},
22702270
{
22712271
onyxMethod: Onyx.METHOD.MERGE,
@@ -2319,7 +2319,7 @@ describe('Onyx', () => {
23192319
value: {
23202320
[key1]: {id: '1', name: 'Updated Item 1'},
23212321
[key2]: {id: '2', name: 'Updated Item 2'},
2322-
} as OnyxMergeCollectionInput<OnyxKey>,
2322+
} as GenericCollection,
23232323
},
23242324
]);
23252325

tests/utils/GenericCollection.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type {OnyxKey, OnyxMergeCollectionInput} from '../../lib/types';
2-
3-
type GenericCollection = OnyxMergeCollectionInput<OnyxKey>;
1+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2+
type GenericCollection = Record<string, any>;
43

54
export default GenericCollection;

0 commit comments

Comments
 (0)