repos / starfx

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

commit
2f1d1ad
parent
0ba489e
author
Eric Bower
date
2023-05-21 15:13:43 +0000 UTC
chore: reorg files and types
10 files changed,  +55, -64
M fx/emit.ts
+1, -2
1@@ -1,5 +1,4 @@
2-import type { Channel, Operation } from "../deps.ts";
3-import type { Action } from "../types.ts";
4+import type { Action, Channel, Operation } from "../deps.ts";
5 
6 import { parallel } from "./parallel.ts";
7 
M iter.ts
+1, -22
 1@@ -1,25 +1,4 @@
 2-import type { Channel, Operation, Stream } 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+import type { Operation, Stream } from "./deps.ts";
25 
26 export function* forEach<T>(
27   stream: Stream<T, void>,
M query/supervisor.ts
+3, -3
 1@@ -1,7 +1,7 @@
 2 import { call, race } from "../fx/index.ts";
 3-import { take } from "../redux/index.ts";
 4-import { Operation, sleep, spawn, Task } from "../deps.ts";
 5-import type { Action, ActionWPayload, OpFn } from "../types.ts";
 6+import { ActionWPayload, take } from "../redux/index.ts";
 7+import { Action, Operation, sleep, spawn, Task } from "../deps.ts";
 8+import type { OpFn } from "../types.ts";
 9 
10 import type { CreateActionPayload } from "./types.ts";
11 
M react.ts
+1, -2
 1@@ -1,8 +1,7 @@
 2 import { React } from "./deps.ts";
 3 const { createContext, createElement: h, useContext } = React;
 4 
 5-import type { Operation, Scope } from "./deps.ts";
 6-import type { Action } from "./types.ts";
 7+import type { Action, Operation, Scope } from "./deps.ts";
 8 import { ActionContext } from "./redux/index.ts";
 9 
10 export * from "./query/react.ts";
M redux/index.ts
+36, -5
 1@@ -1,4 +1,6 @@
 2 import {
 3+  Action,
 4+  AnyAction,
 5   BATCH,
 6   Channel,
 7   Middleware,
 8@@ -6,8 +8,9 @@ import {
 9   ReducersMapObject,
10   Scope,
11 } from "../deps.ts";
12-import type { Action, ActionWPayload, OpFn, StoreLike } from "../types.ts";
13-import type { ActionPattern } from "../matcher.ts";
14+
15+import type { OpFn } from "../types.ts";
16+import { ActionPattern, matcher } from "./matcher.ts";
17 
18 import {
19   combineReducers,
20@@ -20,9 +23,18 @@ import {
21 } from "../deps.ts";
22 import { contextualize } from "../context.ts";
23 import { call, cancel, emit, parallel } from "../fx/index.ts";
24-import { once } from "../iter.ts";
25 import { reducers as queryReducers } from "../query/index.ts";
26 
27+export interface ActionWPayload<P> {
28+  type: string;
29+  payload: P;
30+}
31+
32+export interface StoreLike<S = unknown> {
33+  getState: () => S;
34+  dispatch: (action: Action) => void;
35+}
36+
37 export const ActionContext = createContext<Channel<Action, void>>(
38   "redux:action",
39   createChannel<Action, void>(),
40@@ -30,6 +42,25 @@ export const ActionContext = createContext<Channel<Action, void>>(
41 
42 export const StoreContext = createContext<StoreLike>("redux:store");
43 
44+export function* once({
45+  channel,
46+  pattern,
47+}: {
48+  channel: Operation<Channel<Action, void>>;
49+  pattern: ActionPattern;
50+}) {
51+  const { output } = yield* channel;
52+  const msgList = yield* output;
53+  let next = yield* msgList;
54+  while (!next.done) {
55+    const match = matcher(pattern);
56+    if (match(next.value)) {
57+      return next.value;
58+    }
59+    next = yield* msgList;
60+  }
61+}
62+
63 export function* select<S, R>(selectorFn: (s: S) => R) {
64   const store = yield* StoreContext;
65   return selectorFn(store.getState() as S);
66@@ -87,7 +118,7 @@ export function* takeLeading<T>(
67   });
68 }
69 
70-export function* put(action: Action | Action[]) {
71+export function* put(action: AnyAction | AnyAction[]) {
72   const store = yield* StoreContext;
73   if (Array.isArray(action)) {
74     action.map((act) => store.dispatch(act));
75@@ -100,7 +131,7 @@ export function* put(action: Action | Action[]) {
76   });
77 }
78 
79-function* send(action: Action) {
80+function* send(action: AnyAction) {
81   if (action.type === BATCH) {
82     const actions: Action[] = action.payload;
83     yield* parallel(
R matcher.ts => redux/matcher.ts
+2, -1
1@@ -1,5 +1,6 @@
2-import type { Action, ActionType } from "./types.ts";
3+import type { Action } from "../deps.ts";
4 
5+type ActionType = string;
6 type GuardPredicate<G extends T, T = unknown> = (arg: T) => arg is G;
7 type Predicate = (action: Action) => boolean;
8 type StringableActionCreator<A extends Action = Action> = {
M redux/middleware.test.ts
+1, -1
1@@ -1,6 +1,6 @@
2 import { describe, expect, it } from "../test.ts";
3 import { call } from "../fx/index.ts";
4-import { Action } from "../types.ts";
5+import { Action } from "../deps.ts";
6 
7 import { createFxMiddleware, select } from "./index.ts";
8 
M redux/take-helper.test.ts
+2, -2
 1@@ -1,6 +1,6 @@
 2 import { describe, expect, it } from "../test.ts";
 3 import { cancel } from "../fx/index.ts";
 4-import type { Action } from "../types.ts";
 5+import type { AnyAction } from "../deps.ts";
 6 
 7 import { configureStore, take, takeEvery } from "./index.ts";
 8 const reducers = { init: () => null };
 9@@ -20,7 +20,7 @@ it(testEvery, "should work", async () => {
10     yield* cancel(task);
11   }
12 
13-  function* worker(arg1: string, arg2: string, action: Action) {
14+  function* worker(arg1: string, arg2: string, action: AnyAction) {
15     actual.push([arg1, arg2, action.payload]);
16   }
17 
M redux/take.test.ts
+6, -7
 1@@ -1,6 +1,5 @@
 2 import { describe, expect, it, setupReduxScope } from "../test.ts";
 3-import { sleep, spawn } from "../deps.ts";
 4-import type { Action } from "../types.ts";
 5+import { AnyAction, sleep, spawn } from "../deps.ts";
 6 
 7 import { put, take } from "./index.ts";
 8 
 9@@ -10,7 +9,7 @@ it(
10   takeTests,
11   "a put should complete before more `take` are added and then consumed automatically",
12   async () => {
13-    const actual: Action[] = [];
14+    const actual: AnyAction[] = [];
15 
16     function* channelFn() {
17       yield* sleep(10);
18@@ -59,7 +58,7 @@ it(takeTests, "take from default channel", async () => {
19     });
20   }
21 
22-  const actual: Action[] = [];
23+  const actual: AnyAction[] = [];
24   function* genFn() {
25     yield* spawn(channelFn);
26 
27@@ -67,17 +66,17 @@ it(takeTests, "take from default channel", async () => {
28       actual.push(yield* take("*")); // take all actions
29       actual.push(yield* take("action-1")); // take only actions of type 'action-1'
30       actual.push(yield* take(["action-2", "action-2222"])); // take either type
31-      actual.push(yield* take((a: Action) => a.payload?.isAction)); // take if match predicate
32+      actual.push(yield* take((a: AnyAction) => a.payload?.isAction)); // take if match predicate
33       actual.push(
34         yield* take([
35           "action-3",
36-          (a: Action) => a.payload?.isMixedWithPredicate,
37+          (a: AnyAction) => a.payload?.isMixedWithPredicate,
38         ]),
39       ); // take if match any from the mixed array
40       actual.push(
41         yield* take([
42           "action-3",
43-          (a: Action) => a.payload?.isMixedWithPredicate,
44+          (a: AnyAction) => a.payload?.isMixedWithPredicate,
45         ]),
46       ); // take if match any from the mixed array
47     } finally {
M types.ts
+2, -19
 1@@ -1,28 +1,11 @@
 2 import type { Instruction, Operation } from "./deps.ts";
 3 
 4-export interface Computation<T = any> {
 5+export interface Computation<T = unknown> {
 6+  // deno-lint-ignore no-explicit-any
 7   [Symbol.iterator](): Iterator<Instruction, T, any>;
 8 }
 9 
10-export type ActionType = string;
11-export interface Action {
12-  type: ActionType;
13-  payload?: any;
14-  meta?: any;
15-  error?: boolean;
16-}
17-export interface ActionWPayload<P> {
18-  type: ActionType;
19-  payload: P;
20-}
21-export type AnyAction = Action | ActionWPayload<unknown>;
22-
23 export type OpFn<T = unknown> =
24   | (() => Operation<T>)
25   | (() => PromiseLike<T>)
26   | (() => T);
27-
28-export interface StoreLike<S = unknown> {
29-  getState: () => S;
30-  dispatch: (action: Action) => void;
31-}