repos / starfx

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

commit
098b864
parent
8f49b0c
author
Eric Bower
date
2023-12-01 22:16:12 +0000 UTC
feat(query): thunk and api thunks can simply accept payload (#29)

3 files changed,  +50, -2
M query/thunk.test.ts
+43, -0
 1@@ -334,6 +334,49 @@ it(tests, "run() on endpoint action - should run the effect", () => {
 2   store.dispatch(action2());
 3 });
 4 
 5+it(
 6+  tests,
 7+  "run() on endpoint action with payload - should run the effect",
 8+  () => {
 9+    const api = createThunks<RoboCtx>();
10+    api.use(api.routes());
11+    let acc = "";
12+    const action1 = api.create<{ id: string }>(
13+      "/users",
14+      { supervisor: takeEvery },
15+      function* (ctx, next) {
16+        yield* next();
17+        ctx.request = { method: "expect this" };
18+        acc += "a";
19+      },
20+    );
21+    const action2 = api.create(
22+      "/users2",
23+      { supervisor: takeEvery },
24+      function* (_, next) {
25+        yield* next();
26+        const curCtx = yield* action1.run({ id: "1" });
27+        acc += "b";
28+        asserts.assert(acc === "ab");
29+        assertLike(curCtx, {
30+          action: {
31+            type: `@@starfx${action1}`,
32+            payload: {
33+              name: "/users",
34+            },
35+          },
36+          name: "/users",
37+          request: { method: "expect this" },
38+        });
39+      },
40+    );
41+
42+    const store = configureStore({ initialState: {} });
43+    store.run(api.bootup);
44+    store.dispatch(action2());
45+  },
46+);
47+
48 it(tests, "middleware order of execution", async () => {
49   let acc = "";
50   const api = createThunks();
M query/thunk.ts
+6, -1
 1@@ -209,7 +209,12 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
 2       const key = createKey(name, options);
 3       return action({ name, key, options });
 4     };
 5-    actionFn.run = onApi;
 6+    actionFn.run = (action?: unknown): Operation<Ctx> => {
 7+      if (action && Object.hasOwn(action, "type")) {
 8+        return onApi(action as ActionWithPayload<CreateActionPayload>);
 9+      }
10+      return onApi(actionFn(action));
11+    };
12     actionFn.use = (fn: Middleware<Ctx>) => {
13       const cur = middlewareMap[name];
14       if (cur) {
M query/types.ts
+1, -1
1@@ -121,7 +121,7 @@ export type CreateActionFnWithPayload<P = any> = (
2 
3 export interface CreateActionWithPayload<Ctx extends ThunkCtx, P>
4   extends CreateActionFnWithPayload<P> {
5-  run: (a: ActionWithPayload<CreateActionPayload<P>>) => Operation<Ctx>;
6+  run: (a: ActionWithPayload<CreateActionPayload<P>> | P) => Operation<Ctx>;
7   use: (mdw: Middleware<Ctx>) => void;
8 }
9