-
Notifications
You must be signed in to change notification settings - Fork 3
/
action.ts
148 lines (132 loc) · 3.2 KB
/
action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
call,
Callable,
createContext,
createSignal,
each,
Operation,
Signal,
SignalQueueFactory,
spawn,
Stream,
} from "./deps.ts";
import { ActionPattern, matcher } from "./matcher.ts";
import type { Action, ActionWithPayload, AnyAction } from "./types.ts";
import { createFilterQueue } from "./queue.ts";
import { ActionFnWithPayload } from "./types.ts";
export const ActionContext = createContext(
"starfx:action",
createSignal<AnyAction, void>(),
);
export function useActions(pattern: ActionPattern): Stream<AnyAction, void> {
return {
*subscribe() {
const actions = yield* ActionContext;
const match = matcher(pattern);
yield* SignalQueueFactory.set(() => createFilterQueue(match) as any);
return yield* actions.subscribe();
},
};
}
export function emit({
signal,
action,
}: {
signal: Signal<AnyAction, void>;
action: AnyAction | AnyAction[];
}) {
if (Array.isArray(action)) {
if (action.length === 0) {
return;
}
action.map((a) => signal.send(a));
} else {
signal.send(action);
}
}
export function* put(action: AnyAction | AnyAction[]) {
const signal = yield* ActionContext;
return emit({
signal,
action,
});
}
export function take<P>(
pattern: ActionPattern,
): Operation<ActionWithPayload<P>>;
export function* take(pattern: ActionPattern): Operation<Action> {
const fd = useActions(pattern);
for (const action of yield* each(fd)) {
return action;
}
return { type: "take failed, this should not be possible" };
}
export function* takeEvery<T>(
pattern: ActionPattern,
op: (action: AnyAction) => Operation<T>,
) {
const fd = useActions(pattern);
for (const action of yield* each(fd)) {
yield* spawn(() => op(action));
yield* each.next();
}
}
export function* takeLatest<T>(
pattern: ActionPattern,
op: (action: AnyAction) => Operation<T>,
) {
const fd = useActions(pattern);
let lastTask;
for (const action of yield* each(fd)) {
if (lastTask) {
yield* lastTask.halt();
}
lastTask = yield* spawn(() => op(action));
yield* each.next();
}
}
export function* takeLeading<T>(
pattern: ActionPattern,
op: (action: AnyAction) => Operation<T>,
) {
while (true) {
const action = yield* take(pattern);
yield* call(() => op(action));
}
}
export function* waitFor(
predicate: Callable<boolean>,
) {
const init = yield* call(predicate as any);
if (init) {
return;
}
while (true) {
yield* take("*");
const result = yield* call(() => predicate as any);
if (result) {
return;
}
}
}
export function getIdFromAction(
action: ActionWithPayload<{ key: string }> | ActionFnWithPayload,
): string {
return typeof action === "function" ? action.toString() : action.payload.key;
}
export const API_ACTION_PREFIX = "";
export function createAction(actionType: string): () => Action;
export function createAction<P>(
actionType: string,
): (p: P) => ActionWithPayload<P>;
export function createAction(actionType: string) {
if (!actionType) {
throw new Error("createAction requires non-empty string");
}
const fn = (payload?: unknown) => ({
type: actionType,
payload,
});
fn.toString = () => actionType;
return fn;
}