Skip to content

Commit 6dd764f

Browse files
committed
[persister] Common try
1 parent be97261 commit 6dd764f

2 files changed

Lines changed: 60 additions & 52 deletions

File tree

‎src/common/other.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ export const promiseAll = async (promises: Promise<any>[]) =>
8484
export const errorNew = (message: string) => {
8585
throw new Error(message);
8686
};
87+
88+
export const tryCatch = async (
89+
action: () => unknown | Promise<unknown>,
90+
onIgnoredError?: (error: any) => void,
91+
then?: () => unknown,
92+
): Promise<void> => {
93+
try {
94+
await action();
95+
} catch (error) {
96+
/*! istanbul ignore next */
97+
onIgnoredError?.(error);
98+
then?.();
99+
}
100+
};

‎src/persisters/common/create.ts‎

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {arrayClear, arrayPush, arrayShift} from '../../common/array.ts';
1919
import {getListenerFunctions} from '../../common/listeners.ts';
2020
import {mapEnsure, mapGet, mapNew, mapSet} from '../../common/map.ts';
2121
import {objFreeze, objIsEmpty} from '../../common/obj.ts';
22-
import {errorNew, isArray, isUndefined} from '../../common/other.ts';
22+
import {errorNew, isArray, isUndefined, tryCatch} from '../../common/other.ts';
2323
import {IdSet2} from '../../common/set.ts';
2424

2525
const enum StatusValues {
@@ -164,12 +164,7 @@ export const createCustomPersister = <
164164
)),
165165
)
166166
) {
167-
try {
168-
await action();
169-
} catch (error) {
170-
/*! istanbul ignore next */
171-
onIgnoredError?.(error);
172-
}
167+
await tryCatch(action, onIgnoredError);
173168
}
174169
mapSet(scheduleRunning, scheduleId, 0);
175170
}
@@ -205,21 +200,23 @@ export const createCustomPersister = <
205200
setStatus(StatusValues.Loading);
206201
loads++;
207202
await schedule(async () => {
208-
try {
209-
const content = await getPersisted();
210-
if (isArray(content)) {
211-
setContentOrChanges(content);
212-
} else if (initialContent) {
213-
setDefaultContent(initialContent);
214-
} else {
215-
errorNew(`Content is not an array: ${content}`);
216-
}
217-
} catch (error) {
218-
onIgnoredError?.(error);
219-
if (initialContent) {
220-
setDefaultContent(initialContent);
221-
}
222-
}
203+
await tryCatch(
204+
async () => {
205+
const content = await getPersisted();
206+
if (isArray(content)) {
207+
setContentOrChanges(content);
208+
} else if (initialContent) {
209+
setDefaultContent(initialContent);
210+
} else {
211+
errorNew(`Content is not an array: ${content}`);
212+
}
213+
},
214+
() => {
215+
if (initialContent) {
216+
setDefaultContent(initialContent);
217+
}
218+
},
219+
);
223220
setStatus(StatusValues.Idle);
224221
});
225222
}
@@ -231,35 +228,34 @@ export const createCustomPersister = <
231228
): Promise<Persister<Persist>> => {
232229
stopAutoLoad();
233230
await load(initialContent);
234-
try {
235-
autoLoadHandle = await addPersisterListener(async (content, changes) => {
236-
if (changes || content) {
237-
/*! istanbul ignore else */
238-
if (status != StatusValues.Saving) {
239-
setStatus(StatusValues.Loading);
240-
loads++;
241-
setContentOrChanges(changes ?? content);
242-
setStatus(StatusValues.Idle);
243-
}
244-
} else {
245-
await load();
246-
}
247-
});
248-
} catch (error) {
249-
/*! istanbul ignore next */
250-
onIgnoredError?.(error);
251-
}
231+
await tryCatch(
232+
async () =>
233+
(autoLoadHandle = await addPersisterListener(
234+
async (content, changes) => {
235+
if (changes || content) {
236+
/*! istanbul ignore else */
237+
if (status != StatusValues.Saving) {
238+
setStatus(StatusValues.Loading);
239+
loads++;
240+
setContentOrChanges(changes ?? content);
241+
setStatus(StatusValues.Idle);
242+
}
243+
} else {
244+
await load();
245+
}
246+
},
247+
)),
248+
onIgnoredError,
249+
);
252250
return persister;
253251
};
254252

255253
const stopAutoLoad = async (): Promise<Persister<Persist>> => {
256254
if (autoLoadHandle) {
257-
try {
258-
await delPersisterListener(autoLoadHandle);
259-
} catch (error) {
260-
/*! istanbul ignore next */
261-
onIgnoredError?.(error);
262-
}
255+
await tryCatch(
256+
() => delPersisterListener(autoLoadHandle!),
257+
onIgnoredError,
258+
);
263259
autoLoadHandle = undefined;
264260
}
265261
return persister;
@@ -278,12 +274,10 @@ export const createCustomPersister = <
278274
setStatus(StatusValues.Saving);
279275
saves++;
280276
await schedule(async () => {
281-
try {
282-
await setPersisted(getContent as any, changes);
283-
} catch (error) {
284-
/*! istanbul ignore next */
285-
onIgnoredError?.(error);
286-
}
277+
await tryCatch(
278+
() => setPersisted(getContent as any, changes),
279+
onIgnoredError,
280+
);
287281
setStatus(StatusValues.Idle);
288282
});
289283
}

0 commit comments

Comments
 (0)