repos / starfx

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

commit
93fb0c8
parent
7a9702e
author
Eric Bower
date
2023-12-14 19:37:03 +0000 UTC
refactor(log): generalize for any logging not just errors
5 files changed,  +16, -10
M log.ts
M fx/supervisor.test.ts
+3, -3
 1@@ -55,15 +55,15 @@ it(test, "should recover with backoff pressure", async () => {
 2   });
 3 
 4   expect(actions.length).toEqual(3);
 5-  expect(actions[0].type).toEqual("fx:supervise");
 6+  expect(actions[0].type).toEqual("error:supervise");
 7   expect(actions[0].payload.message).toEqual(
 8     "Exception caught, waiting 1ms before restarting operation",
 9   );
10-  expect(actions[1].type).toEqual("fx:supervise");
11+  expect(actions[1].type).toEqual("error:supervise");
12   expect(actions[1].payload.message).toEqual(
13     "Exception caught, waiting 2ms before restarting operation",
14   );
15-  expect(actions[2].type).toEqual("fx:supervise");
16+  expect(actions[2].type).toEqual("error:supervise");
17   expect(actions[2].payload.message).toEqual(
18     "Exception caught, waiting 3ms before restarting operation",
19   );
M fx/supervisor.ts
+2, -2
 1@@ -31,12 +31,12 @@ export function supervise<T>(
 2         attempt = 0;
 3       } else {
 4         yield* log({
 5-          type: "fx:supervise",
 6+          type: "error:supervise",
 7           payload: {
 8             message:
 9               `Exception caught, waiting ${waitFor}ms before restarting operation`,
10-            op,
11             error: res.error,
12+            op,
13           },
14         });
15         yield* sleep(waitFor);
M log.ts
+0, -3
 1@@ -2,7 +2,6 @@ import { createChannel, createContext } from "./deps.ts";
 2 import type { ActionWPayload } from "./types.ts";
 3 
 4 export interface LogMessage {
 5-  message: string;
 6   [key: string]: any;
 7 }
 8 export type LogAction = ActionWPayload<LogMessage>;
 9@@ -16,8 +15,6 @@ export function createLogger(type: string) {
10 export function* log(action: LogAction) {
11   const chan = yield* LogContext;
12   yield* chan.send(action);
13-  // TODO: only for dev mode?
14-  console.error(action);
15 }
16 
17 export const LogContext = createContext(
M query/mdw.ts
+1, -1
1@@ -32,7 +32,7 @@ export function* err<Ctx extends ThunkCtx = ThunkCtx>(
2   ctx.result = yield* safe(next);
3   if (!ctx.result.ok) {
4     yield* log({
5-      type: "query:err",
6+      type: "error:query",
7       payload: {
8         message:
9           `Error: ${ctx.result.error.message}.  Check the endpoint [${ctx.name}]`,
M store/store.ts
+10, -1
 1@@ -93,6 +93,14 @@ export function createStore<S extends AnyState>({
 2     yield* next();
 3   }
 4 
 5+  function* logMdw(ctx: UpdaterCtx<S>, next: Next) {
 6+    yield* log({
 7+      type: "store",
 8+      payload: { ctx },
 9+    });
10+    yield* next();
11+  }
12+
13   function* notifyChannelMdw(_: UpdaterCtx<S>, next: Next) {
14     const chan = yield* StoreUpdateContext;
15     yield* chan.send();
16@@ -108,6 +116,7 @@ export function createStore<S extends AnyState>({
17     const fn = compose<UpdaterCtx<S>>([
18       updateMdw,
19       ...middleware,
20+      logMdw,
21       notifyChannelMdw,
22       notifyListenersMdw,
23     ]);
24@@ -127,7 +136,7 @@ export function createStore<S extends AnyState>({
25 
26     if (!ctx.result.ok) {
27       yield* log({
28-        type: "store:update",
29+        type: "error:store",
30         payload: {
31           message: `Exception raised when calling store updaters`,
32           error: ctx.result.error,