Skip to content

Commit

Permalink
[persisters] Changes/Content refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgpearce committed Apr 3, 2024
1 parent 22c3681 commit aaf0985
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
55 changes: 25 additions & 30 deletions src/persisters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ type Action = () => Promise<any>;
const scheduleRunning: Map<any, 0 | 1> = mapNew();
const scheduleActions: Map<any, Action[]> = mapNew();

const isMergeable = (
contentOrChanges:
| Content
| MergeableContent
| Changes
| MergeableChanges
| undefined,
) => isString(contentOrChanges?.[0]);

const getStoreFunctions = (
supportsMergeableStore: boolean | undefined,
store: MergeableStore,
Expand Down Expand Up @@ -140,12 +131,22 @@ export const createCustomPersister = <
};

const setContentOrChanges = (
content: Content | MergeableContent | undefined | 1,
contentOrChanges:
| Content
| Changes
| MergeableContent
| MergeableChanges
| undefined
| 1,
): void => {
if (content !== 1) {
(isMergeableStore && isMergeable(content)
? (store as MergeableStore).setMergeableContent
: store.setContent)(content as Content & MergeableContent);
if (contentOrChanges !== 1) {
(isMergeableStore && isString(contentOrChanges?.[0])
? contentOrChanges?.[1][2] === 1
? (store as MergeableStore).applyMergeableChanges
: (store as MergeableStore).setMergeableContent
: contentOrChanges?.[2] === 1
? store.applyChanges
: store.setContent)(contentOrChanges as Changes & MergeableChanges);
}
};

Expand All @@ -169,22 +170,16 @@ export const createCustomPersister = <
await persister.stopAutoLoad().load(initialTables, initialValues);
listening = 1;
listeningHandle = addPersisterListener(async (getContent, getChanges) => {
if (getChanges) {
const changes = getChanges();
await loadLock(async () =>
(isMergeableStore && isMergeable(changes)
? (store as MergeableStore).applyMergeableChanges
: store.applyChanges)(changes as Changes & MergeableChanges),
);
} else {
await loadLock(async () => {
try {
setContentOrChanges(getContent?.() ?? (await getPersisted()));
} catch (error) {
onIgnoredError?.(error);
}
});
}
const changes = getChanges?.();
await loadLock(async () => {
try {
setContentOrChanges(
changes ?? getContent?.() ?? (await getPersisted()),
);
} catch (error) {
onIgnoredError?.(error);
}
});
});
return persister;
},
Expand Down
13 changes: 8 additions & 5 deletions test/unit/persisters/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export const mockChangesListener: Persistable<string> = getMockedCustom(
content = [] as any;
}
customPersisterListener?.(
() => content, // content
undefined,
() => [...content, 1], // changes
);
},
Expand All @@ -209,7 +209,7 @@ export const mockMergeableNoContentListener: Persistable<string> =
export const mockMergeableContentListener: Persistable<string> =
getMockedCustom(async (_location: string, rawContent: any): Promise<void> => {
customPersister = rawContent;
let content: Content;
let content: MergeableContent;
try {
content = JSON.parse(rawContent);
} catch (e) {
Expand All @@ -221,15 +221,18 @@ export const mockMergeableContentListener: Persistable<string> =
export const mockMergeableChangesListener: Persistable<string> =
getMockedCustom(async (_location: string, rawContent: any): Promise<void> => {
customPersister = rawContent;
let content: Content;
let content: MergeableContent;
try {
content = JSON.parse(rawContent);
} catch (e) {
content = [] as any;
}
customPersisterListener?.(
() => content, // content
() => [...content, 1], // changes
undefined,
() =>
(typeof content[0] == 'string'
? [content[0], [...(content[1] ?? []), 1]]
: [...content, 1]) as any, // changes
);
}, true);

Expand Down

0 comments on commit aaf0985

Please sign in to comment.