-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wire up simulation store with non-optimal TS
- Loading branch information
Showing
8 changed files
with
495 additions
and
83 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import "./thunks"; | ||
import { setupStore } from "./setup"; | ||
import { thunks } from "./thunks"; | ||
import type { StoreUpdater } from "starfx"; | ||
import { updateStore } from "starfx"; | ||
import type { SimulationInputSchema } from "./schema"; | ||
|
||
let updater = thunks.create<StoreUpdater<any>[]>( | ||
"update", | ||
function* (ctx, next) { | ||
yield* updateStore(ctx.payload); | ||
yield* next(); | ||
} | ||
); | ||
|
||
type CreateSimulationStore = typeof createSimulationStore; | ||
export type SimulationStore = ReturnType<CreateSimulationStore>; | ||
export function createSimulationStore( | ||
{ | ||
actions: inputActions, | ||
schema: inputSchema, | ||
}: { actions: any; schema: SimulationInputSchema } = { | ||
actions: () => ({}), | ||
schema: () => ({}), | ||
} | ||
) { | ||
let additionalTasks = [thunks.bootup]; | ||
let { store, schema } = setupStore({ | ||
logs: false, | ||
additionalTasks, | ||
inputSchema, | ||
}); | ||
|
||
let actions = { | ||
updater, | ||
...inputActions({ thunks, store, schema }), | ||
}; | ||
|
||
return { | ||
store, | ||
schema, | ||
actions, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createSchema, slice as immerSlice } from "starfx"; | ||
|
||
export type SimulationSlice = typeof immerSlice; | ||
export type SimulationInputSchema = Parameters< | ||
typeof generateSchemaWithInputSlices | ||
>[0]; | ||
type SimulationSchemaSlices = Omit< | ||
Parameters<typeof createSchema>[0], | ||
"loaders" | "cache" | ||
>; | ||
|
||
export function generateSchemaWithInputSlices( | ||
inputSchema: (args: { slice: SimulationSlice }) => SimulationSchemaSlices | ||
) { | ||
let slices = inputSchema({ slice: immerSlice }); | ||
|
||
return createSchema({ | ||
cache: immerSlice.table({ empty: {} }), | ||
loaders: immerSlice.loaders(), | ||
boop: immerSlice.num(), | ||
...slices, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { Callable } from "starfx"; | ||
import { parallel, take, createStore } from "starfx"; | ||
import type { SimulationInputSchema } from "./schema"; | ||
import { generateSchemaWithInputSlices } from "./schema"; | ||
|
||
export function setupStore({ | ||
logs = true, | ||
additionalTasks = [], | ||
initialState, | ||
inputSchema, | ||
}: { | ||
logs: boolean; | ||
initialState?: Record<string, any>; | ||
additionalTasks?: Callable<unknown>[]; | ||
inputSchema: SimulationInputSchema; | ||
}) { | ||
let [schema, schemaInitialState] = generateSchemaWithInputSlices(inputSchema); | ||
let store = createStore({ | ||
initialState: { | ||
...schemaInitialState, | ||
...initialState, | ||
}, | ||
}); | ||
|
||
let tsks: Callable<unknown>[] = [...additionalTasks]; | ||
if (logs) { | ||
// log all actions dispatched | ||
tsks.push(function* logActions() { | ||
while (true) { | ||
let action = yield* take("*"); | ||
console.dir(action, { depth: 5 }); | ||
} | ||
}); | ||
} | ||
|
||
store.run(function* () { | ||
let group = yield* parallel(tsks); | ||
yield* group; | ||
}); | ||
|
||
return { store, schema }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createThunks, mdw } from 'starfx'; | ||
|
||
const thunks = createThunks(); | ||
// catch errors from task and logs them with extra info | ||
thunks.use(mdw.err); | ||
// where all the thunks get called in the middleware stack | ||
thunks.use(thunks.routes()); | ||
|
||
export { thunks }; |