- commit
- 41b8fca
- parent
- 119d312
- author
- Eric Bower
- date
- 2023-05-21 13:59:46 +0000 UTC
chore: compose types
2 files changed,
+20,
-16
+15,
-16
1@@ -1,20 +1,17 @@
2 import { call } from "./fx/index.ts";
3-import type { Instruction, Result } from "./deps.ts";
4-import { Err } from "./deps.ts";
5+import type { Instruction, Operation, Result } from "./deps.ts";
6+import { Err, Ok } from "./deps.ts";
7+import type { Next } from './query/index.ts';
8
9-export type MdwCtx = Record<string, any>;
10-
11-export type Next<T = unknown> = () => Generator<Instruction, Result<T>, void>;
12-export type Middleware<Ctx extends MdwCtx = MdwCtx> = (
13+// deno-lint-ignore no-explicit-any
14+export type BaseCtx = Record<string, any>;
15+export type BaseMiddleware<Ctx extends BaseCtx = BaseCtx, T = unknown> = (
16 ctx: Ctx,
17 next: Next,
18-) => any;
19-/* type MiddlewareCo<Ctx extends MdwCtx = MdwCtx> =
20- | Middleware<Ctx>
21- | Middleware<Ctx>[]; */
22+) => Operation<T>;
23
24-export function compose<Ctx extends MdwCtx = MdwCtx>(
25- middleware: Middleware<Ctx>[],
26+export function compose<Ctx extends BaseCtx = BaseCtx, T = unknown>(
27+ middleware: BaseMiddleware<Ctx, T>[],
28 ) {
29 if (!Array.isArray(middleware)) {
30 throw new TypeError("Middleware stack must be an array!");
31@@ -26,7 +23,7 @@ export function compose<Ctx extends MdwCtx = MdwCtx>(
32 }
33 }
34
35- return function* composeFn<T>(context: Ctx, next?: Next) {
36+ return function* composeFn(context: Ctx, next?: BaseMiddleware<Ctx, T>) {
37 // last called middleware #
38 let index = -1;
39
40@@ -35,12 +32,14 @@ export function compose<Ctx extends MdwCtx = MdwCtx>(
41 return Err(new Error("next() called multiple times"));
42 }
43 index = i;
44- let fn: any = middleware[i];
45- if (i === middleware.length) fn = next;
46+ let fn: BaseMiddleware<Ctx, T> | undefined = middleware[i];
47+ if (i === middleware.length) {
48+ fn = next;
49+ }
50 if (!fn) return Err(new Error("fn is falsy"));
51 const nxt = dispatch.bind(null, i + 1);
52 const result = yield* fn(context, nxt);
53- return result;
54+ return Ok(result);
55 }
56
57 yield* call(() => dispatch(0));
+5,
-0
1@@ -275,6 +275,11 @@
2 "https://esm.sh/redux-batched-actions@0.5.0?pin=v122": "beb7e1f38fd689d68208138d857b4c39aafe7ab66c423ce0c3d5398e77545afc",
3 "https://esm.sh/robodux@15.0.1?pin=v122": "eeb98b8e64edf0cf5f73fbd462565383f94cba3a0b4891265c8c1feb64af370e",
4 "https://esm.sh/stable/react@18.2.0/deno/react.mjs": "a5a73ee24acca4744ee22c51d9357f31968d1f684ce253bde222b4e26d09f49f",
5+ "https://esm.sh/v115/@types/prop-types@15.7.5/index.d.ts": "6a386ff939f180ae8ef064699d8b7b6e62bc2731a62d7fbf5e02589383838dea",
6+ "https://esm.sh/v115/@types/react@18.0.34/global.d.ts": "49a253ec027e56c55c7450a0c331cfe96212b3d1cc215b1710ba94a083404cf3",
7+ "https://esm.sh/v115/@types/react@18.0.34/index.d.ts": "d5208107882e0e3409a5ebc1f1cd1b7cd8b22865688ac9184aab8abc1ce6231d",
8+ "https://esm.sh/v115/@types/scheduler@0.16.3/tracing.d.ts": "f5a8b384f182b3851cec3596ccc96cb7464f8d3469f48c74bf2befb782a19de5",
9+ "https://esm.sh/v115/csstype@3.1.2/index.d.ts": "4c68749a564a6facdf675416d75789ee5a557afda8960e0803cf6711fa569288",
10 "https://esm.sh/v118/@types/prop-types@15.7.5/index.d.ts": "6a386ff939f180ae8ef064699d8b7b6e62bc2731a62d7fbf5e02589383838dea",
11 "https://esm.sh/v118/@types/react@18.2.6/global.d.ts": "549df62b64a71004aee17685b445a8289013daf96246ce4d9b087d13d7a27a61",
12 "https://esm.sh/v118/@types/react@18.2.6/index.d.ts": "2ef11908f0e5f1cae0a42694e5e38852c976ca99a5d7eecfb71ed6a1187736c7",