Skip to content

Commit

Permalink
Try to fix types
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Lazarev <[email protected]>
  • Loading branch information
wKich committed Jun 3, 2024
1 parent 367b08a commit 1f602fa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
28 changes: 8 additions & 20 deletions packages/foundation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,15 @@ import express from "express";
import { merge } from "lodash";
import type { Handler, Request, Document } from "openapi-backend";
import OpenAPIBackend from "openapi-backend";
import type { StoreThunks } from "./store";
import { FxStore, QueryState } from "starfx";
import type { Actions, StoreThunks } from "./store";
import { createSimulationStore } from "./store";
import type { SimulationInputSchema } from "./store/schema";
import type { SimulationStore } from "./store/setup";
import type { SimulationInputSchema, SimulationSchema } from "./store/schema";
import type { RecursivePartial, ReturnTypes } from "./store/types";

export type ThunksCreated = ReturnType<StoreThunks["create"]>;

export async function startServerStandalone<
ExtendedStoreSchema extends SimulationInputSchema,
ExtendedStoreActions extends (arg: {
thunks: StoreThunks;
store: SimulationStore["store"];
schema: SimulationStore["schema"] &
ReturnTypes<ReturnType<ExtendedStoreSchema>>;
}) => { [Key: string]: ThunksCreated },
SimulationStoreContext extends {
store: SimulationStore["store"];
schema: ExtendedStoreSchema;
actions: ExtendedStoreActions;
}
>({
export async function startServerStandalone<Input>({
openapi,
port = 9000,
extendStore,
Expand All @@ -32,22 +19,23 @@ export async function startServerStandalone<
openapi?: {
document: Document | [Document, RecursivePartial<Document>];
handlers: (
simulationStore: SimulationStoreContext
simulationStore: { store: FxStore<QueryState & Input>; schema: SimulationSchema<Input>; actions: Actions<Input> }
) => Record<string, Handler | Record<string, Handler>>;
apiRoot?: string;
};
port: number;
extendStore?: { schema: ExtendedStoreSchema; actions: ExtendedStoreActions };
extendStore?: { schema: SimulationInputSchema<Input>; actions: Actions<Input> };
extend?(
router: express.Router,
simulationStore: SimulationStoreContext
simulationStore: { store: FxStore<QueryState & Input>; schema: SimulationSchema<Input>; actions: Actions<Input> }
): void;
}) {
let app = express();
app.use(express.json());
let simulationStore = createSimulationStore(extendStore);

if (extend) {
// TODO Add `updater` action
extend(app, simulationStore);
}

Expand Down
24 changes: 24 additions & 0 deletions packages/foundation/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<<<<<<< Updated upstream
import type { SimulationInputSchema } from "./schema";
import type { SimulationStore } from "./setup";
import { setupStore } from "./setup";
import { thunks } from "./thunks";
import type { AnyState, StoreUpdater } from "starfx";
import { updateStore } from "starfx";
import type { ReturnTypes } from "./types";
=======
import type { DefaultSchema, SimulationInputSchema, SimulationSchema } from "./schema";
import { setupStore } from "./setup";
import { thunks } from "./thunks";
import type { AnyState, FxSchema, FxStore, QueryState, StoreUpdater } from "starfx";
import { updateStore } from "starfx";
>>>>>>> Stashed changes

let updater = thunks.create<StoreUpdater<AnyState>[]>(
"update",
Expand All @@ -15,6 +23,7 @@ let updater = thunks.create<StoreUpdater<AnyState>[]>(
);
export type StoreThunks = typeof thunks;

<<<<<<< Updated upstream
export function createSimulationStore<
ExtendedStoreSchema extends SimulationInputSchema,
ExtendedStoreActions extends (arg: {
Expand All @@ -24,16 +33,31 @@ export function createSimulationStore<
ReturnTypes<ReturnType<ExtendedStoreSchema>>;
}) => { [Key: string]: ReturnType<StoreThunks["create"]> }
>(
=======
export type Actions<Input extends Record<string, any>> = (arg: {
thunks: StoreThunks;
store: FxStore<QueryState & Input>;
schema: SimulationSchema<Input>;
}) => Record<string, ReturnType<StoreThunks["create"]>>

export function createSimulationStore<Input extends Record<string, any>>(
>>>>>>> Stashed changes
{
actions: inputActions,
schema: inputSchema,
}: {
<<<<<<< Updated upstream
actions: ExtendedStoreActions | undefined;
schema: ExtendedStoreSchema | undefined;
} = {
actions: undefined,
schema: undefined,
}
=======
actions?: Actions<Input>;
schema: SimulationInputSchema<Input>;
} = { schema: (() => ({})) as unknown as SimulationInputSchema<Input> }
>>>>>>> Stashed changes
) {
let additionalTasks = [thunks.bootup];
let { store, schema } = setupStore({
Expand Down
21 changes: 10 additions & 11 deletions packages/foundation/store/schema.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { createSchema, slice as immerSlice } from "starfx";
import type { FxSchema, BaseSchema, FxMap, QueryState } from "starfx";

export type SimulationSlice = typeof immerSlice;
export type SimulationInputSchema = (args: {
slice: SimulationSlice;
}) => SimulationSchemaSlicesSansBase;
export type SimulationSchemaSlicesSansBase = Omit<
Parameters<typeof createSchema>[0],
"loaders" | "cache"
>;
export type SimulationInputSchema<T extends Record<string, any>> = (args: { slice: SimulationSlice }) => ToMap<T>;

export type Loaders<Schema> = Schema extends FxSchema<any, infer L> ? L : never;

export type ToMap<T> = { [key in keyof T]: (name: string) => BaseSchema<T[key]> };

export type SimulationSchema<Input extends Record<string, any>> = FxSchema<QueryState & Input, FxMap & ToMap<Input>>;

export function generateSchemaWithInputSlices<
ExtendedStoreSchema extends SimulationInputSchema
>(inputSchema?: ExtendedStoreSchema) {
let slices = inputSchema ? inputSchema({ slice: immerSlice }) : {};
export function generateSchemaWithInputSlices<T extends Record<string, any>>(inputSchema: SimulationInputSchema<T>) {
let slices = inputSchema({ slice: immerSlice });

return createSchema({
cache: immerSlice.table({ empty: {} }),
Expand Down
15 changes: 12 additions & 3 deletions packages/foundation/store/setup.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { Callable } from "starfx";
import type { Callable, FxStore, QueryState } from "starfx";
import { parallel, take, createStore } from "starfx";
import type { SimulationInputSchema } from "./schema";
import type { SimulationInputSchema, SimulationSchema } from "./schema";
import { generateSchemaWithInputSlices } from "./schema";

<<<<<<< Updated upstream
export type SimulationStore = ReturnType<typeof setupStore>;
export function setupStore<ExtendedStoreSchema extends SimulationInputSchema>({
=======
export function setupStore<Input>({
>>>>>>> Stashed changes
logs = true,
additionalTasks = [],
initialState,
Expand All @@ -13,8 +17,13 @@ export function setupStore<ExtendedStoreSchema extends SimulationInputSchema>({
logs: boolean;
initialState?: Record<string, any>;
additionalTasks?: Callable<unknown>[];
<<<<<<< Updated upstream
inputSchema?: ExtendedStoreSchema;
}) {
=======
inputSchema: SimulationInputSchema<Input>;
}): { store: FxStore<QueryState & Input>; schema: SimulationSchema<Input> } {
>>>>>>> Stashed changes
let [schema, schemaInitialState] = generateSchemaWithInputSlices(inputSchema);
let store = createStore({
initialState: {
Expand All @@ -39,5 +48,5 @@ export function setupStore<ExtendedStoreSchema extends SimulationInputSchema>({
yield* group;
});

return { store, schema };
return { store, schema } as any;
}

0 comments on commit 1f602fa

Please sign in to comment.