repos / starfx

supercharged async flow control library.
git clone https://github.com/neurosnap/starfx.git

commit
3db088e
parent
d05fa1c
author
Eric Bower
date
2023-07-15 14:30:37 +0000 UTC
fix: dispatch type sig
2 files changed,  +17, -2
M redux/query.ts
+16, -1
 1@@ -1,7 +1,7 @@
 2 import { createLoaderTable, createReducerMap, createTable } from "../deps.ts";
 3 import { compose } from "../compose.ts";
 4 export { defaultLoader } from "../store/mod.ts";
 5-import { dispatchActions } from "../store/mod.ts";
 6+import type { AnyAction } from "../store/mod.ts";
 7 import { ApiCtx, createKey, Next } from "../query/mod.ts";
 8 import { put, select } from "./mod.ts";
 9 import type { QueryState } from "../types.ts";
10@@ -44,6 +44,21 @@ export const selectDataByName = (
11 
12 export const reducers = createReducerMap(loaders, data);
13 
14+/**
15+ * This middleware will take the result of `ctx.actions` and dispatch them
16+ * as a single batch.
17+ *
18+ * @remarks This is useful because sometimes there are a lot of actions that need dispatched
19+ * within the pipeline of the middleware and instead of dispatching them serially this
20+ * improves performance by only hitting the reducers once.
21+ */
22+export function* dispatchActions(ctx: { actions: AnyAction[] }, next: Next) {
23+  if (!ctx.actions) ctx.actions = [];
24+  yield* next();
25+  if (ctx.actions.length === 0) return;
26+  yield* put(ctx.actions);
27+}
28+
29 /**
30  * This middleware will automatically cache any data found inside `ctx.json`
31  * which is where we store JSON data from the `fetcher` middleware.
M store/types.ts
+1, -1
1@@ -34,7 +34,7 @@ export interface FxStore<S extends AnyState> {
2   subscribe: (fn: Listener) => () => void;
3   update: (u: StoreUpdater<S>) => Operation<Result<UpdaterCtx<S>>>;
4   run: <T>(op: OpFn<T>) => Task<Result<T>>;
5-  dispatch: (a: AnyAction) => Task<any>;
6+  dispatch: (a: AnyAction) => any;
7   replaceReducer: (r: (s: S, a: AnyAction) => S) => void;
8   [Symbol.observable]: () => any;
9 }