-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathnow.ts
232 lines (208 loc) · 5.6 KB
/
now.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { IO, runIO } from "@funkia/io";
import { placeholder } from "./placeholder";
import { Time } from "./common";
import { Future, fromPromise, mapCbFuture } from "./future";
import { Behavior } from "./behavior";
import { Stream, mapCbStream, isStream } from "./stream";
import { tick } from "./clock";
export type MapNowTuple<A> = { [K in keyof A]: Now<A[K]> };
const isRecord = (a: unknown): a is Record<PropertyKey, unknown> =>
typeof a === "object";
export abstract class Now<A> {
isNow: true;
constructor() {
this.isNow = true;
}
static is(a: unknown): a is Now<unknown> {
return isRecord(a) && a.isNow === true;
}
abstract run(time: Time): A;
of<B>(b: B): Now<B> {
return new OfNow(b);
}
static of<B>(b: B): Now<B> {
return new OfNow(b);
}
static multi = false;
multi = false;
map<B>(f: (a: A) => B): Now<B> {
return new MapNow(f, this);
}
mapTo<B>(b: B): Now<B> {
return new MapNow((_) => b, this);
}
flatMap<B>(f: (a: A) => Now<B>): Now<B> {
return new FlatMapNow(this, f);
}
chain<B>(f: (a: A) => Now<B>): Now<B> {
return new FlatMapNow(this, f);
}
flat<B>(this: Now<Now<B>>): Now<B> {
return new FlatMapNow(this, (n) => n);
}
ap<B>(a: Now<(a: A) => B>): Now<B> {
return this.lift((f, a) => f(a), a, this);
}
lift<A extends unknown[], R>(
f: (...args: A) => R,
...args: MapNowTuple<A>
): Now<R> {
return args.length === 1
? new MapNow(f as any, args[0])
: new LiftNow(f, args);
}
}
export class OfNow<A> extends Now<A> {
constructor(private value: A) {
super();
}
run(_: Time): A {
return this.value;
}
}
export class MapNow<A, B> extends Now<B> {
constructor(private f: (a: A) => B, private parent: Now<A>) {
super();
}
run(t: Time): B {
return this.f(this.parent.run(t));
}
}
export class LiftNow<A extends Now<unknown>[], R> extends Now<R> {
constructor(readonly f: Function, readonly parents: A) {
super();
}
run(t: Time): R {
return this.f(...this.parents.map((n) => n.run(t)));
}
}
export class FlatMapNow<A, B> extends Now<B> {
constructor(private first: Now<A>, private f: (a: A) => Now<B>) {
super();
}
run(t: Time): B {
return this.f(this.first.run(t)).run(t);
}
}
export class SampleNow<A> extends Now<A> {
constructor(private b: Behavior<A>) {
super();
}
run(t: Time): A {
return this.b.at(t);
}
}
export function sample<A>(b: Behavior<A>): Now<A> {
return new SampleNow(b);
}
export class PerformNow<A> extends Now<A> {
constructor(private _run: () => A) {
super();
}
run(): A {
return this._run();
}
}
/**
* Create a now-computation that executes the effectful computation `cb` when it
* is run.
*/
export function perform<A>(cb: () => A): Now<A> {
return new PerformNow(cb);
}
export function performIO<A>(comp: IO<A>): Now<Future<A>> {
return perform(() => fromPromise(runIO(comp)));
}
export function performStream<A>(s: Stream<IO<A>>): Now<Stream<Future<A>>> {
return perform(() =>
mapCbStream<IO<A>, Future<A>>((io, cb) => cb(fromPromise(runIO(io))), s)
);
}
export class PerformMapNow<A, B> extends Now<Stream<B> | Future<B>> {
constructor(private cb: (a: A) => B, private s: Stream<A> | Future<A>) {
super();
}
run(): Stream<B> | Future<B> {
return isStream(this.s)
? mapCbStream((value, done) => done(this.cb(value)), this.s)
: mapCbFuture((value, done) => done(this.cb(value)), this.s);
}
}
/**
* Maps a function with side-effects over a future or stream.
*/
export function performMap<A, B>(cb: (a: A) => B, f: Future<A>): Now<Future<B>>;
export function performMap<A, B>(cb: (a: A) => B, s: Stream<A>): Now<Stream<B>>;
export function performMap<A, B>(
cb: (a: A) => B,
s: Stream<A> | Future<A>
): Now<Stream<B> | Future<B>> {
return perform(() =>
isStream(s)
? mapCbStream((value, done) => done(cb(value)), s)
: mapCbFuture((value, done) => done(cb(value)), s)
);
}
export function plan<A>(future: Future<Now<A>>): Now<Future<A>> {
return performMap<Now<A>, A>(runNow, future);
}
export function runNow<A>(now: Now<A>, time: Time = tick()): A {
return now.run(time);
}
export interface ReactivesObject {
[a: string]: Behavior<unknown> | Stream<unknown>;
}
const placeholderProxyHandler = {
get: function(
target: ReactivesObject,
name: string
): ReactivesObject[string] {
if (!(name in target)) {
target[name] = placeholder();
}
return target[name];
}
};
class LoopNow<A extends ReactivesObject> extends Now<A> {
constructor(
private fn: (a: A) => Now<A>,
private placeholderNames?: string[]
) {
super();
}
run(t: Time): A {
let placeholderObject: any;
if (this.placeholderNames === undefined) {
placeholderObject = new Proxy({}, placeholderProxyHandler);
} else {
placeholderObject = {};
for (const name of this.placeholderNames) {
placeholderObject[name] = placeholder();
}
}
const result = this.fn(placeholderObject).run(t);
const returned: (keyof A)[] = Object.keys(result);
for (const name of returned) {
placeholderObject[name].replaceWith(result[name]);
}
return result;
}
}
export function loopNow<A extends ReactivesObject>(
fn: (a: A) => Now<A>,
names?: string[]
): Now<A> {
return new LoopNow(fn, names);
}
export type InstantRun = <A>(now: Now<A>) => A;
export class InstantNow<A> extends Now<A> {
constructor(private fn: (run: InstantRun) => A) {
super();
}
run(t: Time): A {
return this.fn((now) => now.run(t));
}
}
export function instant<A>(fn: (run: InstantRun) => A): Now<A> {
return new InstantNow(fn);
}