Skip to content

Commit fa91ec7

Browse files
committed
[persisters] Load and autoLoad with content function
1 parent e7592e5 commit fa91ec7

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

src/@types/persisters/docs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,8 @@
900900
* fails (for example when the Persister is remote and the environment is
901901
* offline). This allows you to fallback or instantiate a Store whether it's
902902
* loading from previously persisted storage or being run for the first time.
903+
* Since v5.4.2, this parameter can also be a function that returns the
904+
* content.
903905
*
904906
* This method is asynchronous because the persisted data may be on a remote
905907
* machine or a filesystem. Even for those storage types that are synchronous
@@ -966,6 +968,8 @@
966968
* fails (for example when the Persister is remote and the environment is
967969
* offline). This allows you to fallback or instantiate a Store whether it's
968970
* loading from previously persisted storage or being run for the first time.
971+
* Since v5.4.2, this parameter can also be a function that returns the
972+
* content.
969973
*
970974
* This method first runs a single call to the load method to ensure the data
971975
* is in sync with the persisted storage. It then continues to watch for

src/@types/persisters/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ export type DpcTabularValues = {
150150
export interface Persister<Persist extends Persists = Persists.StoreOnly> {
151151
//
152152
/// Persister.load
153-
load(initialContent?: Content): Promise<this>;
153+
load(initialContent?: Content | (() => Content)): Promise<this>;
154154

155155
/// Persister.startAutoLoad
156-
startAutoLoad(initialContent?: Content): Promise<this>;
156+
startAutoLoad(initialContent?: Content | (() => Content)): Promise<this>;
157157

158158
/// Persister.stopAutoLoad
159159
stopAutoLoad(): this;

src/@types/persisters/with-schemas/index.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,14 @@ export interface Persister<
168168
Persist extends Persists = Persists.StoreOnly,
169169
> {
170170
/// Persister.load
171-
load(initialContent?: Content<Schemas, true>): Promise<this>;
171+
load(
172+
initialContent?: Content<Schemas, true> | (() => Content<Schemas, true>),
173+
): Promise<this>;
172174

173175
/// Persister.startAutoLoad
174-
startAutoLoad(initialContent?: Content<Schemas, true>): Promise<this>;
176+
startAutoLoad(
177+
initialContent?: Content<Schemas, true> | (() => Content<Schemas, true>),
178+
): Promise<this>;
175179

176180
/// Persister.stopAutoLoad
177181
stopAutoLoad(): this;

src/persisters/common/create.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const getStoreFunctions = (
6161
getContent: () => Content,
6262
getChanges: () => Changes,
6363
hasChanges: (changes: Changes) => boolean,
64-
setDefaultContent: (content: Content) => Store,
64+
setDefaultContent: (content: Content | (() => Content)) => Store,
6565
]
6666
| [
6767
isMergeableStore: 1,
@@ -70,7 +70,7 @@ const getStoreFunctions = (
7070
typeof isSynchronizer extends 1 ? false : true
7171
>,
7272
hasChanges: (changes: MergeableChanges) => boolean,
73-
setDefaultContent: (content: Content) => MergeableStore,
73+
setDefaultContent: (content: Content | (() => Content)) => MergeableStore,
7474
] =>
7575
persist != PersistsValues.StoreOnly && store.isMergeable()
7676
? [
@@ -195,7 +195,9 @@ export const createCustomPersister = <
195195
);
196196
};
197197

198-
const load = async (initialContent?: Content): Promise<Persister> => {
198+
const load = async (
199+
initialContent?: Content | (() => Content),
200+
): Promise<Persister> => {
199201
/*! istanbul ignore else */
200202
if (status != StatusValues.Saving) {
201203
setStatus(StatusValues.Loading);
@@ -206,14 +208,14 @@ export const createCustomPersister = <
206208
if (isArray(content)) {
207209
setContentOrChanges(content);
208210
} else if (initialContent) {
209-
setDefaultContent(initialContent as Content);
211+
setDefaultContent(initialContent);
210212
} else {
211213
errorNew(`Content is not an array: ${content}`);
212214
}
213215
} catch (error) {
214216
onIgnoredError?.(error);
215217
if (initialContent) {
216-
setDefaultContent(initialContent as Content);
218+
setDefaultContent(initialContent);
217219
}
218220
}
219221
setStatus(StatusValues.Idle);
@@ -223,7 +225,7 @@ export const createCustomPersister = <
223225
};
224226

225227
const startAutoLoad = async (
226-
initialContent?: Content,
228+
initialContent?: Content | (() => Content),
227229
): Promise<Persister<Persist>> => {
228230
stopAutoLoad();
229231
await load(initialContent);

0 commit comments

Comments
 (0)