repos / starfx

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

commit
44ddfc9
parent
e319eb3
author
Eric Bower
date
2023-12-01 03:28:44 +0000 UTC
refactor: require data and loaders slices (#24)

6 files changed,  +25, -14
M query/api.test.ts
+2, -2
 1@@ -29,7 +29,7 @@ const testStore = () => {
 2   const schema = createSchema({
 3     users: slice.table<User>({ empty: emptyUser }),
 4     loaders: slice.loader(),
 5-    data: slice.table({ empty: {} }),
 6+    cache: slice.table({ empty: {} }),
 7   });
 8   const store = configureStore(schema);
 9   return { schema, store };
10@@ -298,7 +298,7 @@ it(tests, "createApi with hash key on a large post", async () => {
11   });
12 
13   expect([8, 9].includes(expectedKey.split("|")[1].length)).toBeTruthy();
14-  expect(s.data[expectedKey]).toEqual({
15+  expect(s.cache[expectedKey]).toEqual({
16     "1": { id: "1", name: "test", email: email, largetext: largetext },
17   });
18 });
M query/fetch.test.ts
+5, -5
 1@@ -23,7 +23,7 @@ const delay = (n = 200) =>
 2 const testStore = () => {
 3   const schema = createSchema({
 4     loaders: slice.loader(),
 5-    data: slice.table({ empty: {} }),
 6+    cache: slice.table({ empty: {} }),
 7   });
 8   const store = configureStore(schema);
 9   return { schema, store };
10@@ -68,7 +68,7 @@ it(
11     await delay();
12 
13     const state = store.getState();
14-    expect(state.data[action.payload.key]).toEqual(mockUser);
15+    expect(state.cache[action.payload.key]).toEqual(mockUser);
16 
17     expect(actual).toEqual([{
18       url: `${baseUrl}/users`,
19@@ -151,7 +151,7 @@ it(tests, "error handling", async () => {
20   await delay();
21 
22   const state = store.getState();
23-  expect(state.data[action.payload.key]).toEqual(errMsg);
24+  expect(state.cache[action.payload.key]).toEqual(errMsg);
25   expect(actual).toEqual({ ok: false, data: errMsg, error: errMsg });
26 });
27 
28@@ -191,7 +191,7 @@ it(tests, "status 204", async () => {
29   await delay();
30 
31   const state = store.getState();
32-  expect(state.data[action.payload.key]).toEqual({});
33+  expect(state.cache[action.payload.key]).toEqual({});
34   expect(actual).toEqual({ ok: true, data: {}, value: {} });
35 });
36 
37@@ -429,7 +429,7 @@ it(
38     await delay();
39 
40     const state = store.getState();
41-    expect(state.data[action.payload.key]).toEqual(mockUser);
42+    expect(state.cache[action.payload.key]).toEqual(mockUser);
43     expect(actual).toEqual({ ok: true, data: mockUser, value: mockUser });
44   },
45 );
M query/mdw.test.ts
+3, -3
 1@@ -34,7 +34,7 @@ const testStore = () => {
 2   const schema = createSchema({
 3     users: slice.table<User>({ empty: emptyUser }),
 4     loaders: slice.loader(),
 5-    data: slice.table({ empty: {} }),
 6+    cache: slice.table({ empty: {} }),
 7   });
 8   const store = configureStore(schema);
 9   return { schema, store };
10@@ -410,7 +410,7 @@ it(tests, "createApi with own key", async () => {
11     : createKey("/users [POST]", { email: newUEmail });
12 
13   const s = store.getState();
14-  asserts.assertEquals(schema.db.data.selectById(s, { id: expectedKey }), {
15+  asserts.assertEquals(schema.db.cache.selectById(s, { id: expectedKey }), {
16     "1": { id: "1", name: "test", email: newUEmail },
17   });
18 
19@@ -480,7 +480,7 @@ it(tests, "createApi with custom key but no payload", async () => {
20     : createKey("/users [GET]", null);
21 
22   const s = store.getState();
23-  asserts.assertEquals(schema.db.data.selectById(s, { id: expectedKey }), {
24+  asserts.assertEquals(schema.db.cache.selectById(s, { id: expectedKey }), {
25     "1": mockUser,
26   });
27 
M store/query.ts
+3, -3
 1@@ -9,15 +9,15 @@ import { TableOutput } from "./slice/table.ts";
 2 export function storeMdw<
 3   Ctx extends ApiCtx = ApiCtx,
 4   M extends AnyState = AnyState,
 5->({ data, loaders, errorFn }: {
 6+>({ cache, loaders, errorFn }: {
 7   loaders: LoaderOutput<M, AnyState>;
 8-  data: TableOutput<any, AnyState>;
 9+  cache: TableOutput<any, AnyState>;
10   errorFn?: (ctx: Ctx) => string;
11 }) {
12   return compose<Ctx>([
13     dispatchActions,
14     loadingMonitor(loaders, errorFn),
15-    simpleCache(data),
16+    simpleCache(cache),
17   ]);
18 }
19 
M store/schema.test.ts
+4, -0
 1@@ -25,6 +25,7 @@ it(tests, "general types and functionality", async () => {
 2     counter: slice.num(),
 3     dev: slice.any<boolean>(false),
 4     currentUser: slice.obj<User>(emptyUser),
 5+    cache: slice.table({ empty: {} }),
 6     loaders: slice.loader(),
 7   });
 8   const db = schema.db;
 9@@ -36,6 +37,7 @@ it(tests, "general types and functionality", async () => {
10     counter: 0,
11     dev: false,
12     currentUser: { id: "", name: "" },
13+    cache: {},
14     loaders: {},
15   });
16   const userMap = schema.db.users.selectTable(store.getState());
17@@ -74,6 +76,8 @@ it(tests, "general types and functionality", async () => {
18 it(tests, "can work with a nested object", async () => {
19   const schema = createSchema({
20     currentUser: slice.obj<UserWithRoles>({ id: "", name: "", roles: [] }),
21+    cache: slice.table({ empty: {} }),
22+    loaders: slice.loader(),
23   });
24   const db = schema.db;
25   const store = configureStore(schema);
M store/schema.ts
+8, -1
 1@@ -1,8 +1,15 @@
 2+import { AnyState } from "../types.ts";
 3 import { updateStore } from "./fx.ts";
 4+import { LoaderOutput } from "./slice/loader.ts";
 5+import { TableOutput } from "./slice/table.ts";
 6 import { BaseSchema, FxStore, StoreUpdater } from "./types.ts";
 7 
 8 export function createSchema<
 9-  O extends { [key: string]: (name: string) => BaseSchema<unknown> },
10+  O extends {
11+    loaders: <M extends AnyState>(s: string) => LoaderOutput<M, AnyState>;
12+    cache: (s: string) => TableOutput<any, AnyState>;
13+    [key: string]: (name: string) => BaseSchema<unknown>;
14+  },
15   S extends { [key in keyof O]: ReturnType<O[key]>["initialState"] },
16 >(
17   slices: O,