Skip to content

Commit 9f4d9eb

Browse files
committed
[persister] Added tolerance for loading invalid content
1 parent fbafcf1 commit 9f4d9eb

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/persisters/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ export const createCustomPersister = <
162162
loads++;
163163
await schedule(async () => {
164164
try {
165-
setContentOrChanges(await getPersisted());
165+
const content = await getPersisted();
166+
isArray(content)
167+
? setContentOrChanges(content)
168+
: errorNew(`Content is not an array ${content}`);
166169
} catch (error) {
167170
onIgnoredError?.(error);
168171
if (initialContent) {

src/store/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ import {
116116
} from '../common/cell.ts';
117117
import {
118118
ifNotUndefined,
119+
isArray,
119120
isFunction,
120121
isTypeStringOrBoolean,
121122
isUndefined,
@@ -245,6 +246,8 @@ export const createStore: typeof createStoreDecl = (): Store => {
245246
return true;
246247
};
247248

249+
const validateContent = isArray;
250+
248251
const validateTables = (tables: Tables): boolean =>
249252
objValidate(tables, validateTable, cellInvalid);
250253

@@ -411,6 +414,11 @@ export const createStore: typeof createStoreDecl = (): Store => {
411414
const setOrDelTables = (tables: Tables) =>
412415
objIsEmpty(tables) ? delTables() : setTables(tables);
413416

417+
const setValidContent = ([tables, values]: Content): void => {
418+
(objIsEmpty(tables) ? delTables : setTables)(tables);
419+
(objIsEmpty(values) ? delValues : setValues)(values);
420+
};
421+
414422
const setValidTables = (tables: Tables): TablesMap =>
415423
mapMatch(
416424
tablesMap,
@@ -1069,11 +1077,10 @@ export const createStore: typeof createStoreDecl = (): Store => {
10691077
const getSchemaJson = (): Json =>
10701078
jsonStringWithMap([tablesSchemaMap, valuesSchemaMap]);
10711079

1072-
const setContent = ([tables, values]: Content): Store =>
1073-
fluentTransaction(() => {
1074-
(objIsEmpty(tables) ? delTables : setTables)(tables);
1075-
(objIsEmpty(values) ? delValues : setValues)(values);
1076-
});
1080+
const setContent = (content: Content): Store =>
1081+
fluentTransaction(() =>
1082+
validateContent(content) ? setValidContent(content) : 0,
1083+
);
10771084

10781085
const setTables = (tables: Tables): Store =>
10791086
fluentTransaction(() =>

0 commit comments

Comments
 (0)