repos / starfx

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

commit
f47ecce
parent
70f24ec
author
Eric Bower
date
2024-04-15 19:50:48 +0000 UTC
fix(mdw): thunk loader needs `ctx.loader`
3 files changed,  +36, -5
A Makefile
+11, -0
 1@@ -0,0 +1,11 @@
 2+fmt:
 3+	deno fmt
 4+.PHONY:
 5+
 6+lint:
 7+	deno lint
 8+.PHONY: lint
 9+
10+test:
11+	deno test --allow-env --allow-read
12+.PHONY: test
M mdw/store.ts
+21, -5
 1@@ -1,4 +1,4 @@
 2-import type { ApiCtx, ThunkCtx } from "../query/mod.ts";
 3+import type { ApiCtx, ThunkCtxWLoader } from "../query/mod.ts";
 4 import { compose } from "../compose.ts";
 5 import type { AnyState, Next } from "../types.ts";
 6 import {
 7@@ -75,28 +75,41 @@ export function loader<M extends AnyState = AnyState>(schema: {
 8   loaders: LoaderOutput<M, AnyState>;
 9 }) {
10   return function* <
11-    Ctx extends ThunkCtx = ThunkCtx,
12+    Ctx extends ThunkCtxWLoader = ThunkCtxWLoader,
13   >(ctx: Ctx, next: Next) {
14     yield* updateStore([
15       schema.loaders.start({ id: ctx.name }),
16       schema.loaders.start({ id: ctx.key }),
17     ]);
18 
19+    if (!ctx.loader) ctx.loader = {} as any;
20+
21     try {
22       yield* next();
23+
24+      if (!ctx.loader) {
25+        ctx.loader = {};
26+      }
27+
28       yield* updateStore([
29-        schema.loaders.success({ id: ctx.name }),
30-        schema.loaders.success({ id: ctx.key }),
31+        schema.loaders.success({ id: ctx.name, ...ctx.loader }),
32+        schema.loaders.success({ id: ctx.key, ...ctx.loader }),
33       ]);
34     } catch (err) {
35+      if (!ctx.loader) {
36+        ctx.loader = {};
37+      }
38+
39       yield* updateStore([
40         schema.loaders.error({
41           id: ctx.name,
42           message: err.message,
43+          ...ctx.loader,
44         }),
45         schema.loaders.error({
46           id: ctx.key,
47           message: err.message,
48+          ...ctx.loader,
49         }),
50       ]);
51     } finally {
52@@ -106,7 +119,10 @@ export function loader<M extends AnyState = AnyState>(schema: {
53       const ids = loaders
54         .filter((loader) => loader.status === "loading")
55         .map((loader) => loader.id);
56-      yield* updateStore(schema.loaders.resetByIds(ids));
57+
58+      if (ids.length > 0) {
59+        yield* updateStore(schema.loaders.resetByIds(ids));
60+      }
61     }
62   };
63 }
M query/types.ts
+4, -0
 1@@ -22,6 +22,10 @@ export interface ThunkCtx<P = any> extends Payload<P> {
 2   result: Result<void>;
 3 }
 4 
 5+export interface ThunkCtxWLoader extends ThunkCtx {
 6+  loader: Omit<LoaderPayload<any>, "id"> | null;
 7+}
 8+
 9 export interface LoaderCtx<P = unknown> extends ThunkCtx<P> {
10   loader: Partial<LoaderItemState> | null;
11 }