Skip to content

Commit

Permalink
fix: dispatch type sig
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Jul 15, 2023
1 parent d05fa1c commit 3db088e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion redux/query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLoaderTable, createReducerMap, createTable } from "../deps.ts";
import { compose } from "../compose.ts";
export { defaultLoader } from "../store/mod.ts";
import { dispatchActions } from "../store/mod.ts";
import type { AnyAction } from "../store/mod.ts";
import { ApiCtx, createKey, Next } from "../query/mod.ts";
import { put, select } from "./mod.ts";
import type { QueryState } from "../types.ts";
Expand Down Expand Up @@ -44,6 +44,21 @@ export const selectDataByName = (

export const reducers = createReducerMap(loaders, data);

/**
* This middleware will take the result of `ctx.actions` and dispatch them
* as a single batch.
*
* @remarks This is useful because sometimes there are a lot of actions that need dispatched
* within the pipeline of the middleware and instead of dispatching them serially this
* improves performance by only hitting the reducers once.
*/
export function* dispatchActions(ctx: { actions: AnyAction[] }, next: Next) {
if (!ctx.actions) ctx.actions = [];
yield* next();
if (ctx.actions.length === 0) return;
yield* put(ctx.actions);
}

/**
* This middleware will automatically cache any data found inside `ctx.json`
* which is where we store JSON data from the `fetcher` middleware.
Expand Down
2 changes: 1 addition & 1 deletion store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface FxStore<S extends AnyState> {
subscribe: (fn: Listener) => () => void;
update: (u: StoreUpdater<S>) => Operation<Result<UpdaterCtx<S>>>;
run: <T>(op: OpFn<T>) => Task<Result<T>>;
dispatch: (a: AnyAction) => Task<any>;
dispatch: (a: AnyAction) => any;
replaceReducer: (r: (s: S, a: AnyAction) => S) => void;
[Symbol.observable]: () => any;
}

0 comments on commit 3db088e

Please sign in to comment.