Skip to content

Commit

Permalink
Fix TS issues
Browse files Browse the repository at this point in the history
  • Loading branch information
paultsimura committed Apr 18, 2024
1 parent 3381ff8 commit 31fd63e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import utils from './utils';
import DevTools from './DevTools';
import type {
Collection,
CollectionKey,
CollectionKeyBase,
ConnectOptions,
InitOptions,
KeyValueMapping,
Mapping,
MixedOperationsQueue,
NullableKeyValueMapping,
NullishDeep,
OnyxCollection,
Expand Down Expand Up @@ -584,12 +586,12 @@ function update(data: OnyxUpdate[]): Promise<void> {
enqueueMergeOperation(key, value);
break;
case OnyxUtils.METHOD.MERGE_COLLECTION:
if (OnyxUtils.isValidMergeCollection(key, value)) {
Object.entries(value).forEach(([_key, _value]) => enqueueMergeOperation(_key, _value));
if (OnyxUtils.isValidMergeCollection(key, value as Collection<CollectionKey, unknown, unknown>)) {
Object.entries(value).forEach(([entryKey, entryValue]) => enqueueMergeOperation(entryKey, entryValue));
}
break;
case OnyxUtils.METHOD.MULTI_SET:
Object.entries(value).forEach(([_key, _value]) => enqueueSetOperation(_key, _value));
Object.entries(value).forEach(([entryKey, entryValue]) => enqueueSetOperation(entryKey, entryValue));
break;
case OnyxUtils.METHOD.CLEAR:
clearPromise = clear();
Expand All @@ -609,7 +611,7 @@ function update(data: OnyxUpdate[]): Promise<void> {
}

const batchedCollectionUpdates = collectionItemKeys.reduce(
(acc: {merge: NullableKeyValueMapping; set: NullableKeyValueMapping}, key: string) => {
(acc: MixedOperationsQueue, key: string) => {
const operations = updateQueue[key];

// Remove the collection-related key from the updateQueue so that it won't be processed individually later.
Expand All @@ -630,8 +632,7 @@ function update(data: OnyxUpdate[]): Promise<void> {
);

if (!utils.isEmptyObject(batchedCollectionUpdates.merge)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
promises.push(() => mergeCollection(collectionKey, batchedCollectionUpdates.merge as any)); // todo: fix this any
promises.push(() => mergeCollection(collectionKey, batchedCollectionUpdates.merge as Collection<CollectionKey, unknown, unknown>));
}
if (!utils.isEmptyObject(batchedCollectionUpdates.set)) {
promises.push(() => multiSet(batchedCollectionUpdates.set));
Expand Down
4 changes: 3 additions & 1 deletion lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import type {
OnyxEntry,
KeyValueMapping,
DefaultConnectCallback,
Collection,
NullishDeep,
} from './types';
import type Onyx from './Onyx';

Expand Down Expand Up @@ -1051,7 +1053,7 @@ function initializeWithDefaultKeyStates(): Promise<void> {
/**
* Verify if the collection is valid for merging into the collection key using mergeCollection()
*/
function isValidMergeCollection<TKey extends CollectionKeyBase>(collectionKey: TKey, collection: Record<string, unknown>): boolean {
function isValidMergeCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey, collection: Collection<TKey, TMap, NullishDeep<KeyValueMapping[TKey]>>): boolean {
if (typeof collection !== 'object' || Array.isArray(collection) || utils.isEmptyObject(collection)) {
Logger.logInfo('mergeCollection() called with invalid or empty value. Skipping this update.');
return false;
Expand Down
9 changes: 9 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ type InitOptions = {
debugSetState?: boolean;
};

/**
* Represents a combination of Merge and Set operations that should be executed in Onyx
*/
type MixedOperationsQueue = {
merge: NullableKeyValueMapping;
set: NullableKeyValueMapping;
};

export type {
CollectionKey,
CollectionKeyBase,
Expand Down Expand Up @@ -418,4 +426,5 @@ export type {
Mapping,
OnyxUpdate,
InitOptions,
MixedOperationsQueue,
};

0 comments on commit 31fd63e

Please sign in to comment.