repos / starfx

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

commit
cfcc818
parent
89763fc
author
Eric Bower
date
2023-12-04 14:54:17 +0000 UTC
fix: `slice.table` types
2 files changed,  +48, -23
M store/slice/table.test.ts
+23, -7
 1@@ -1,8 +1,7 @@
 2 import { asserts, describe, it } from "../../test.ts";
 3-import { configureStore, updateStore } from "../../store/mod.ts";
 4-import { createQueryState } from "../../action.ts";
 5-
 6-import { createTable } from "./table.ts";
 7+import { configureStore } from "../store.ts";
 8+import { updateStore } from "../fx.ts";
 9+import { createTable, table } from "./table.ts";
10 
11 const tests = describe("createTable()");
12 
13@@ -19,7 +18,6 @@ const slice = createTable<TUser>({
14 });
15 
16 const initialState = {
17-  ...createQueryState(),
18   [NAME]: slice.initialState,
19 };
20 
21@@ -113,8 +111,26 @@ it(tests, "gets all rows", async () => {
22   asserts.assertEquals(store.getState()[NAME], data);
23 });
24 
25-it(tests, "optional empty", async () => {
26-  const tbl = createTable<TUser>({ name: "table" });
27+// checking types of `result` here
28+it(tests, "with empty", async () => {
29+  const tbl = table<TUser>({ empty: first })("users");
30+  const store = configureStore({
31+    initialState,
32+  });
33+
34+  await store.run(function* () {
35+    yield* updateStore(tbl.set({ [first.id]: first }));
36+  });
37+  asserts.assertEquals(tbl.selectTable(store.getState()), {
38+    [first.id]: first,
39+  });
40+  const result = tbl.selectById(store.getState(), { id: 1 });
41+  asserts.assertEquals(result, first);
42+});
43+
44+// checking types of `result` here
45+it(tests, "with no empty", async () => {
46+  const tbl = table<TUser>()("users");
47   const store = configureStore({
48     initialState,
49   });
M store/slice/table.ts
+25, -16
 1@@ -109,11 +109,7 @@ export interface TableOutput<
 2 export function createTable<
 3   Entity extends AnyState = AnyState,
 4   S extends AnyState = AnyState,
 5->({
 6-  name,
 7-  empty,
 8-  initialState = {},
 9-}: {
10+>(p: {
11   name: keyof S;
12   initialState?: Record<IdProp, Entity>;
13   empty: Entity | (() => Entity);
14@@ -121,11 +117,7 @@ export function createTable<
15 export function createTable<
16   Entity extends AnyState = AnyState,
17   S extends AnyState = AnyState,
18->({
19-  name,
20-  empty,
21-  initialState = {},
22-}: {
23+>(p: {
24   name: keyof S;
25   initialState?: Record<IdProp, Entity>;
26   empty?: Entity | (() => Entity);
27@@ -197,10 +189,27 @@ export function createTable<
28 }
29 
30 export function table<
31-  V extends AnyState = AnyState,
32->({ initialState, empty }: {
33-  initialState?: Record<IdProp, V>;
34-  empty?: V | (() => V);
35-}) {
36-  return (name: string) => createTable<V>({ name, empty, initialState });
37+  Entity extends AnyState = AnyState,
38+  S extends AnyState = AnyState,
39+>(p: {
40+  initialState?: Record<IdProp, Entity>;
41+  empty: Entity | (() => Entity);
42+}): (n: string) => TableOutput<Entity, S, Entity>;
43+export function table<
44+  Entity extends AnyState = AnyState,
45+  S extends AnyState = AnyState,
46+>(p?: {
47+  initialState?: Record<IdProp, Entity>;
48+  empty?: Entity | (() => Entity);
49+}): (n: string) => TableOutput<Entity, S, Entity | undefined>;
50+export function table<
51+  Entity extends AnyState = AnyState,
52+  S extends AnyState = AnyState,
53+>(
54+  { initialState, empty }: {
55+    initialState?: Record<IdProp, Entity>;
56+    empty?: Entity | (() => Entity);
57+  } = {},
58+): (n: string) => TableOutput<Entity, S, Entity | undefined> {
59+  return (name: string) => createTable<Entity>({ name, empty, initialState });
60 }