diff --git a/packages/foundation/index.ts b/packages/foundation/index.ts index 66b60fa5..1acaad6d 100644 --- a/packages/foundation/index.ts +++ b/packages/foundation/index.ts @@ -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; -export async function startServerStandalone< - ExtendedStoreSchema extends SimulationInputSchema, - ExtendedStoreActions extends (arg: { - thunks: StoreThunks; - store: SimulationStore["store"]; - schema: SimulationStore["schema"] & - ReturnTypes>; - }) => { [Key: string]: ThunksCreated }, - SimulationStoreContext extends { - store: SimulationStore["store"]; - schema: ExtendedStoreSchema; - actions: ExtendedStoreActions; - } ->({ +export async function startServerStandalone({ openapi, port = 9000, extendStore, @@ -32,15 +19,15 @@ export async function startServerStandalone< openapi?: { document: Document | [Document, RecursivePartial]; handlers: ( - simulationStore: SimulationStoreContext + simulationStore: { store: FxStore; schema: SimulationSchema; actions: Actions } ) => Record>; apiRoot?: string; }; port: number; - extendStore?: { schema: ExtendedStoreSchema; actions: ExtendedStoreActions }; + extendStore?: { schema: SimulationInputSchema; actions: Actions }; extend?( router: express.Router, - simulationStore: SimulationStoreContext + simulationStore: { store: FxStore; schema: SimulationSchema; actions: Actions } ): void; }) { let app = express(); @@ -48,6 +35,7 @@ export async function startServerStandalone< let simulationStore = createSimulationStore(extendStore); if (extend) { + // TODO Add `updater` action extend(app, simulationStore); } diff --git a/packages/foundation/store/index.ts b/packages/foundation/store/index.ts index d927d6e4..1fdf4edd 100644 --- a/packages/foundation/store/index.ts +++ b/packages/foundation/store/index.ts @@ -1,3 +1,4 @@ +<<<<<<< Updated upstream import type { SimulationInputSchema } from "./schema"; import type { SimulationStore } from "./setup"; import { setupStore } from "./setup"; @@ -5,6 +6,13 @@ 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[]>( "update", @@ -15,6 +23,7 @@ let updater = thunks.create[]>( ); export type StoreThunks = typeof thunks; +<<<<<<< Updated upstream export function createSimulationStore< ExtendedStoreSchema extends SimulationInputSchema, ExtendedStoreActions extends (arg: { @@ -24,16 +33,31 @@ export function createSimulationStore< ReturnTypes>; }) => { [Key: string]: ReturnType } >( +======= +export type Actions> = (arg: { + thunks: StoreThunks; + store: FxStore; + schema: SimulationSchema; +}) => Record> + +export function createSimulationStore>( +>>>>>>> Stashed changes { actions: inputActions, schema: inputSchema, }: { +<<<<<<< Updated upstream actions: ExtendedStoreActions | undefined; schema: ExtendedStoreSchema | undefined; } = { actions: undefined, schema: undefined, } +======= + actions?: Actions; + schema: SimulationInputSchema; + } = { schema: (() => ({})) as unknown as SimulationInputSchema } +>>>>>>> Stashed changes ) { let additionalTasks = [thunks.bootup]; let { store, schema } = setupStore({ diff --git a/packages/foundation/store/schema.ts b/packages/foundation/store/schema.ts index e051bb38..ebba6b56 100644 --- a/packages/foundation/store/schema.ts +++ b/packages/foundation/store/schema.ts @@ -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[0], - "loaders" | "cache" ->; +export type SimulationInputSchema> = (args: { slice: SimulationSlice }) => ToMap; + +export type Loaders = Schema extends FxSchema ? L : never; + +export type ToMap = { [key in keyof T]: (name: string) => BaseSchema }; + +export type SimulationSchema> = FxSchema>; -export function generateSchemaWithInputSlices< - ExtendedStoreSchema extends SimulationInputSchema ->(inputSchema?: ExtendedStoreSchema) { - let slices = inputSchema ? inputSchema({ slice: immerSlice }) : {}; +export function generateSchemaWithInputSlices>(inputSchema: SimulationInputSchema) { + let slices = inputSchema({ slice: immerSlice }); return createSchema({ cache: immerSlice.table({ empty: {} }), diff --git a/packages/foundation/store/setup.ts b/packages/foundation/store/setup.ts index 5bcbb320..bc0575da 100644 --- a/packages/foundation/store/setup.ts +++ b/packages/foundation/store/setup.ts @@ -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; export function setupStore({ +======= +export function setupStore({ +>>>>>>> Stashed changes logs = true, additionalTasks = [], initialState, @@ -13,8 +17,13 @@ export function setupStore({ logs: boolean; initialState?: Record; additionalTasks?: Callable[]; +<<<<<<< Updated upstream inputSchema?: ExtendedStoreSchema; }) { +======= + inputSchema: SimulationInputSchema; +}): { store: FxStore; schema: SimulationSchema } { +>>>>>>> Stashed changes let [schema, schemaInitialState] = generateSchemaWithInputSlices(inputSchema); let store = createStore({ initialState: { @@ -39,5 +48,5 @@ export function setupStore({ yield* group; }); - return { store, schema }; + return { store, schema } as any; }