repos / starfx

supercharged async flow control library.
git clone https://github.com/neurosnap/starfx.git

commit
be72c77
parent
aea5407
author
Eric Bower
date
2023-12-17 03:38:07 +0000 UTC
fix(store): persistor rehydrates properly now
2 files changed,  +47, -5
M store/persist.test.ts
+41, -3
 1@@ -1,7 +1,7 @@
 2 import { Ok, Operation } from "../deps.ts";
 3 import { parallel } from "../fx/mod.ts";
 4 import { asserts, describe, it } from "../test.ts";
 5-import { createTracker, put, take } from "./fx.ts";
 6+import { put, take } from "./fx.ts";
 7 import { configureStore } from "./store.ts";
 8 import {
 9   createPersistor,
10@@ -43,8 +43,7 @@ it(tests, "can persist to storage adapters", async () => {
11   });
12 
13   await store.run(function* (): Operation<void> {
14-    const tracker = createTracker(db.loaders)(PERSIST_LOADER_ID);
15-    yield* tracker(persistor.rehydrate);
16+    yield* persistor.rehydrate();
17 
18     const group = yield* parallel([
19       function* (): Operation<void> {
20@@ -63,3 +62,42 @@ it(tests, "can persist to storage adapters", async () => {
21     '{"token":"1234"}',
22   );
23 });
24+
25+it(tests, "rehydrates state", async () => {
26+  const schema = createSchema({
27+    token: slice.str(),
28+    loaders: slice.loader(),
29+    cache: slice.table({ empty: {} }),
30+  });
31+  const db = schema.db;
32+  type State = typeof schema.initialState;
33+  let ls = JSON.stringify({ token: "123" });
34+  const adapter: PersistAdapter<State> = {
35+    getItem: function* (_: string) {
36+      return Ok(JSON.parse(ls));
37+    },
38+    setItem: function* (_: string, s: Partial<State>) {
39+      ls = JSON.stringify(s);
40+      return Ok(undefined);
41+    },
42+    removeItem: function* (_: string) {
43+      return Ok(undefined);
44+    },
45+  };
46+  const persistor = createPersistor<State>({ adapter, allowlist: ["token"] });
47+  const mdw = persistStoreMdw(persistor);
48+  const store = configureStore({
49+    initialState: schema.initialState,
50+    middleware: [mdw],
51+  });
52+
53+  await store.run(function* (): Operation<void> {
54+    yield* persistor.rehydrate();
55+    yield* schema.update(db.loaders.success({ id: PERSIST_LOADER_ID }));
56+  });
57+
58+  asserts.assertEquals(
59+    store.getState().token,
60+    "123",
61+  );
62+});
M store/persist.ts
+6, -2
 1@@ -4,7 +4,7 @@ import { AnyState } from "../types.ts";
 2 import { select, updateStore } from "./fx.ts";
 3 import { UpdaterCtx } from "./types.ts";
 4 
 5-export const PERSIST_LOADER_ID = "persist";
 6+export const PERSIST_LOADER_ID = "@@starfx/persist";
 7 
 8 export interface PersistAdapter<S extends AnyState> {
 9   getItem(key: string): Operation<Result<Partial<S>>>;
10@@ -38,8 +38,12 @@ export function createPersistor<S extends AnyState>(
11       return Err(persistedState.error);
12     }
13 
14+    const state = yield* select((s) => s);
15+    const nextState = reconciler(state as S, persistedState.value);
16     yield* updateStore<S>(function (state) {
17-      state = reconciler(state, persistedState.value);
18+      Object.keys(nextState).forEach((key: keyof S) => {
19+        state[key] = nextState[key];
20+      });
21     });
22 
23     return Ok(undefined);