Skip to content

Commit 6985895

Browse files
committed
add most.ts
1 parent 93cd81f commit 6985895

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

lib/engine/most.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { from, of, mergeArray, Stream, never, Subscription } from 'most'
2+
import { async as subject, AsyncSubject } from 'most-subject'
3+
4+
export interface EngineSubject<T> extends AsyncSubject<T> {
5+
send(x: T): this
6+
}
7+
8+
export interface Update<S> {
9+
(current: S): S
10+
}
11+
12+
export class Engine<T, S> {
13+
intentStream: EngineSubject<T>
14+
historyStream: EngineSubject<S>
15+
travelStream: EngineSubject<(n: number) => number>
16+
constructor() {
17+
this.intentStream = subject() as EngineSubject<T>
18+
this.intentStream.send = this.intentStream.next.bind(this.intentStream)
19+
this.historyStream = subject() as EngineSubject<S>
20+
this.historyStream.send = this.historyStream.next.bind(this.historyStream)
21+
this.travelStream = subject() as EngineSubject<(n: number) => number>;
22+
this.travelStream.send = this.travelStream.next.bind(this.historyStream)
23+
}
24+
25+
observe<T>(actionsSinks: Stream<Update<T>>[], f): Subscription<T> {
26+
let subscriptions = mergeArray(actionsSinks)
27+
.recoverWith(e => {
28+
// console.error('There is Error in your reducer:', e, e.stack)
29+
return of(x => x)
30+
})
31+
.subscribe({
32+
next: f,
33+
error: (e) => console.error('Something is Wrong:', e, e.stack),
34+
complete: f
35+
});
36+
return subscriptions;
37+
}
38+
}

0 commit comments

Comments
 (0)