Skip to content

Commit

Permalink
increment calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jbolda committed Aug 28, 2024
1 parent 48bf362 commit 2f42d89
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/foundation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function createFoundationSimulationServer<
let simulationStore = createSimulationStore(extendStore);

app.use((req, res, next) => {
// add each response to the internal log
simulationStore.store.dispatch(
simulationStore.actions.simulationLog({
method: req.method,
Expand Down Expand Up @@ -278,6 +279,7 @@ export function createFoundationSimulationServer<
}
}

// return simulation helper page
app.get("/", (req, res) => {
let routes = simulationStore.schema.simulationRoutes.selectTableAsList(
simulationStore.store.getState()
Expand Down
14 changes: 13 additions & 1 deletion packages/foundation/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateSchemaWithInputSlices } from "./schema";
import type { ExtendSimulationSchemaInput } from "./schema";
import type { AnyState, StoreUpdater, Callable } from "starfx";
import { parallel, take, createStore, createSelector } from "starfx";
import { parallel, take, select, createStore, createSelector } from "starfx";
import { updateStore, createThunks, mdw } from "starfx";

type StoreThunks = ReturnType<typeof createThunks>;
Expand Down Expand Up @@ -82,6 +82,7 @@ export function createSimulationStore<
}>("simulationLog", function* (ctx, next) {
const { method, url, query, body } = ctx.payload;
const timestamp = Date.now();

yield* schema.update(
schema.simulationLogs.add({
[timestamp]: {
Expand All @@ -92,6 +93,17 @@ export function createSimulationStore<
},
})
);

// attempt to increment `route.calls`
const id = `${method.toLowerCase()}:${url}`;
const route = yield* select(schema.simulationRoutes.selectById, {
id,
});
if (route.url !== "")
yield* schema.update(
schema.simulationRoutes.merge({ [id]: { calls: route.calls + 1 } })
);

yield* next();
});

Expand Down

0 comments on commit 2f42d89

Please sign in to comment.