Skip to content

Commit

Permalink
fix: slice.table types
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Dec 4, 2023
1 parent 89763fc commit cfcc818
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
30 changes: 23 additions & 7 deletions store/slice/table.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { asserts, describe, it } from "../../test.ts";
import { configureStore, updateStore } from "../../store/mod.ts";
import { createQueryState } from "../../action.ts";

import { createTable } from "./table.ts";
import { configureStore } from "../store.ts";
import { updateStore } from "../fx.ts";
import { createTable, table } from "./table.ts";

const tests = describe("createTable()");

Expand All @@ -19,7 +18,6 @@ const slice = createTable<TUser>({
});

const initialState = {
...createQueryState(),
[NAME]: slice.initialState,
};

Expand Down Expand Up @@ -113,8 +111,26 @@ it(tests, "gets all rows", async () => {
asserts.assertEquals(store.getState()[NAME], data);
});

it(tests, "optional empty", async () => {
const tbl = createTable<TUser>({ name: "table" });
// checking types of `result` here
it(tests, "with empty", async () => {
const tbl = table<TUser>({ empty: first })("users");
const store = configureStore({
initialState,
});

await store.run(function* () {
yield* updateStore(tbl.set({ [first.id]: first }));
});
asserts.assertEquals(tbl.selectTable(store.getState()), {
[first.id]: first,
});
const result = tbl.selectById(store.getState(), { id: 1 });
asserts.assertEquals(result, first);
});

// checking types of `result` here
it(tests, "with no empty", async () => {
const tbl = table<TUser>()("users");
const store = configureStore({
initialState,
});
Expand Down
41 changes: 25 additions & 16 deletions store/slice/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,15 @@ export interface TableOutput<
export function createTable<
Entity extends AnyState = AnyState,
S extends AnyState = AnyState,
>({
name,
empty,
initialState = {},
}: {
>(p: {
name: keyof S;
initialState?: Record<IdProp, Entity>;
empty: Entity | (() => Entity);
}): TableOutput<Entity, S, Entity>;
export function createTable<
Entity extends AnyState = AnyState,
S extends AnyState = AnyState,
>({
name,
empty,
initialState = {},
}: {
>(p: {
name: keyof S;
initialState?: Record<IdProp, Entity>;
empty?: Entity | (() => Entity);
Expand Down Expand Up @@ -197,10 +189,27 @@ export function createTable<
}

export function table<
V extends AnyState = AnyState,
>({ initialState, empty }: {
initialState?: Record<IdProp, V>;
empty?: V | (() => V);
}) {
return (name: string) => createTable<V>({ name, empty, initialState });
Entity extends AnyState = AnyState,
S extends AnyState = AnyState,
>(p: {
initialState?: Record<IdProp, Entity>;
empty: Entity | (() => Entity);
}): (n: string) => TableOutput<Entity, S, Entity>;
export function table<
Entity extends AnyState = AnyState,
S extends AnyState = AnyState,
>(p?: {
initialState?: Record<IdProp, Entity>;
empty?: Entity | (() => Entity);
}): (n: string) => TableOutput<Entity, S, Entity | undefined>;
export function table<
Entity extends AnyState = AnyState,
S extends AnyState = AnyState,
>(
{ initialState, empty }: {
initialState?: Record<IdProp, Entity>;
empty?: Entity | (() => Entity);
} = {},
): (n: string) => TableOutput<Entity, S, Entity | undefined> {
return (name: string) => createTable<Entity>({ name, empty, initialState });
}

0 comments on commit cfcc818

Please sign in to comment.