repos / starfx

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

commit
a525a64
parent
d9adf0a
author
Eric Bower
date
2023-04-20 01:24:43 +0000 UTC
chore: moved `once`
5 files changed,  +36, -25
M fx/mod.ts
+0, -1
1@@ -3,7 +3,6 @@ export * from "./call.ts";
2 export * from "./cancel.ts";
3 export * from "./cancelled.ts";
4 export * from "./race.ts";
5-export * from "./once.ts";
6 export * from "./emit.ts";
7 export * from "./request.ts";
8 export * from "./watch.ts";
D fx/once.ts
+0, -22
 1@@ -1,22 +0,0 @@
 2-import type { Channel, Operation } from "../deps.ts";
 3-import type { Action } from "../types.ts";
 4-import { ActionPattern, matcher } from "../matcher.ts";
 5-
 6-export function* once({
 7-  channel,
 8-  pattern,
 9-}: {
10-  channel: Operation<Channel<Action, void>>;
11-  pattern: ActionPattern;
12-}) {
13-  const { output } = yield* channel;
14-  const msgList = yield* output;
15-  let next = yield* msgList;
16-  while (!next.done) {
17-    const match = matcher(pattern);
18-    if (match(next.value)) {
19-      return next.value;
20-    }
21-    next = yield* msgList;
22-  }
23-}
M fx/request.ts
+13, -1
 1@@ -1,7 +1,19 @@
 2-import { expect, useAbortSignal } from "../deps.ts";
 3+import { ErrContext } from "../context.ts";
 4+import { Err, expect, Ok, useAbortSignal } from "../deps.ts";
 5 
 6 export function* request(url: string | URL | Request, opts?: RequestInit) {
 7   const signal = yield* useAbortSignal();
 8   const response = yield* expect(fetch(url, { signal, ...opts }));
 9   return response;
10 }
11+
12+export function* json(response: Response) {
13+  try {
14+    const result = yield* expect(response.json());
15+    return Ok(result);
16+  } catch (error) {
17+    const { input } = yield* ErrContext;
18+    yield* input.send(error);
19+    return Err(error);
20+  }
21+}
M iter.ts
+21, -0
 1@@ -1,4 +1,25 @@
 2 import type { Channel, Operation } from "./deps.ts";
 3+import type { Action } from "./types.ts";
 4+import { ActionPattern, matcher } from "./matcher.ts";
 5+
 6+export function* once({
 7+  channel,
 8+  pattern,
 9+}: {
10+  channel: Operation<Channel<Action, void>>;
11+  pattern: ActionPattern;
12+}) {
13+  const { output } = yield* channel;
14+  const msgList = yield* output;
15+  let next = yield* msgList;
16+  while (!next.done) {
17+    const match = matcher(pattern);
18+    if (match(next.value)) {
19+      return next.value;
20+    }
21+    next = yield* msgList;
22+  }
23+}
24 
25 export function* cforEach<T>(
26   chan: Channel<T, void>,
M redux.ts
+2, -1
 1@@ -4,7 +4,8 @@ import type { ActionPattern } from "./matcher.ts";
 2 
 3 import { createChannel, createContext, createScope } from "./deps.ts";
 4 import { contextualize } from "./context.ts";
 5-import { call, emit, once, parallel } from "./fx/mod.ts";
 6+import { call, emit, parallel } from "./fx/mod.ts";
 7+import { once } from "./iter.ts";
 8 
 9 export const ActionContext = createContext<Channel<Action, void>>(
10   "redux:action",