repos / starfx

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

commit
314337f
parent
54f6fc8
author
Eric Bower
date
2024-02-06 04:57:31 +0000 UTC
fix(thunk): `toString` must return `type` instead of `name`
7 files changed,  +16, -14
M action.ts
+1, -1
1@@ -106,7 +106,7 @@ export function* takeLeading<T>(
2 export const API_ACTION_PREFIX = "@@starfx";
3 export const createAction = (curType: string) => {
4   if (!curType) throw new Error("createAction requires non-empty string");
5-  const type = `${API_ACTION_PREFIX}:${curType}`;
6+  const type = `${API_ACTION_PREFIX}${curType}`;
7   const action = () => ({ type });
8   action.toString = () => type;
9   return action;
M fx/supervisor.ts
+1, -1
1@@ -32,7 +32,7 @@ export function supervise<T>(
2         attempt = 0;
3       } else {
4         yield* put({
5-          type: `${API_ACTION_PREFIX}:supervise`,
6+          type: `${API_ACTION_PREFIX}supervise`,
7           payload: res.error,
8           meta:
9             `Exception caught, waiting ${waitFor}ms before restarting operation`,
M query/thunk.ts
+2, -2
 1@@ -134,7 +134,7 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
 2     yield* next();
 3   }
 4 
 5-  const createType = (post: string) => `${API_ACTION_PREFIX}:${post}`;
 6+  const createType = (post: string) => `${API_ACTION_PREFIX}${post}`;
 7 
 8   function* onApi<P extends CreateActionPayload>(
 9     action: ActionWithPayload<P>,
10@@ -218,7 +218,7 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
11         dynamicMiddlewareMap[name] = fn;
12       }
13     };
14-    actionFn.toString = () => name;
15+    actionFn.toString = () => type;
16     actionFn._success = {};
17     actionFn._error = {};
18     actionMap[name] = actionFn;
M store/store.ts
+2, -2
 1@@ -94,7 +94,7 @@ export function createStore<S extends AnyState>({
 2 
 3   function* logMdw(ctx: UpdaterCtx<S>, next: Next) {
 4     dispatch({
 5-      type: `${API_ACTION_PREFIX}:store`,
 6+      type: `${API_ACTION_PREFIX}store`,
 7       payload: ctx,
 8     });
 9     yield* next();
10@@ -135,7 +135,7 @@ export function createStore<S extends AnyState>({
11 
12     if (!ctx.result.ok) {
13       dispatch({
14-        type: `${API_ACTION_PREFIX}:store`,
15+        type: `${API_ACTION_PREFIX}store`,
16         payload: ctx.result.error,
17       });
18     }
M test/action.test.ts
+2, -2
 1@@ -5,10 +5,10 @@ const tests = describe("createAction()");
 2 
 3 it(tests, "should return action type when stringified", () => {
 4   const undo = createAction("UNDO");
 5-  expect(`${API_ACTION_PREFIX}:UNDO`).toEqual(`${undo}`);
 6+  expect(`${API_ACTION_PREFIX}UNDO`).toEqual(`${undo}`);
 7 });
 8 
 9 it(tests, "return object with type", () => {
10   const undo = createAction("UNDO");
11-  expect(undo()).toEqual({ type: `${API_ACTION_PREFIX}:UNDO` });
12+  expect(undo()).toEqual({ type: `${API_ACTION_PREFIX}UNDO` });
13 });
M test/api.test.ts
+5, -3
 1@@ -18,6 +18,7 @@ import {
 2   takeEvery,
 3 } from "../mod.ts";
 4 import { useCache } from "../react.ts";
 5+import { API_ACTION_PREFIX } from "../action.ts";
 6 
 7 interface User {
 8   id: string;
 9@@ -237,7 +238,7 @@ it(tests, "run() from a normal saga", () => {
10   store.dispatch(action2());
11 });
12 
13-it(tests, "createApi with hash key on a large post", async () => {
14+it(tests, "with hash key on a large post", async () => {
15   const { store, schema } = testStore();
16   const query = createApi();
17   query.use(mdw.api());
18@@ -289,12 +290,13 @@ it(tests, "createApi with hash key on a large post", async () => {
19   const largetext = "abc-def-ghi-jkl-mno-pqr".repeat(100);
20 
21   store.run(query.bootup);
22-  store.dispatch(createUserDefaultKey({ email, largetext }));
23+  const action = createUserDefaultKey({ email, largetext });
24+  store.dispatch(action);
25 
26   await sleep(150);
27 
28   const s = store.getState();
29-  const expectedKey = createKey(`${createUserDefaultKey}`, {
30+  const expectedKey = createKey(action.payload.name, {
31     email,
32     largetext,
33   });
M test/supervisor.test.ts
+3, -3
 1@@ -64,15 +64,15 @@ it(test, "should recover with backoff pressure", async () => {
 2   });
 3 
 4   expect(actions.length).toEqual(3);
 5-  expect(actions[0].type).toEqual(`${API_ACTION_PREFIX}:supervise`);
 6+  expect(actions[0].type).toEqual(`${API_ACTION_PREFIX}supervise`);
 7   expect(actions[0].meta).toEqual(
 8     "Exception caught, waiting 1ms before restarting operation",
 9   );
10-  expect(actions[1].type).toEqual(`${API_ACTION_PREFIX}:supervise`);
11+  expect(actions[1].type).toEqual(`${API_ACTION_PREFIX}supervise`);
12   expect(actions[1].meta).toEqual(
13     "Exception caught, waiting 2ms before restarting operation",
14   );
15-  expect(actions[2].type).toEqual(`${API_ACTION_PREFIX}:supervise`);
16+  expect(actions[2].type).toEqual(`${API_ACTION_PREFIX}supervise`);
17   expect(actions[2].meta).toEqual(
18     "Exception caught, waiting 3ms before restarting operation",
19   );