UNPKG

1.75 kBPlain TextView Raw
1import {Stream, MemoryStream} from 'xstream';
2
3export interface FantasyObserver {
4 next(x: any): void;
5 error(err: any): void;
6 complete(c?: any): void;
7}
8
9export interface FantasySubscription {
10 unsubscribe(): void;
11}
12
13export interface FantasyObservable {
14 subscribe(observer: FantasyObserver): FantasySubscription;
15}
16
17export interface DevToolEnabledSource {
18 _isCycleSource: string;
19}
20
21export type Sources = {
22 [name: string]: any;
23};
24
25export type Sinks = {
26 [name: string]: any;
27};
28
29/**
30 * Sink proxies should be MemoryStreams in order to fix race conditions for
31 * drivers that subscribe to sink proxies "later".
32 *
33 * Recall that there are two steps:
34 * 1. Setup (sink proxies -> drivers -> sources -> main -> sink)
35 * 2. Execution (also known as replication: sink proxies imitate sinks)
36 *
37 * If a driver does not synchronously/immediately subscribe to the sink proxy
38 * in step (1), but instead does that later, if step (2) feeds a value from the
39 * sink to the sink proxy, then when the driver subscribes to the sink proxy,
40 * it should receive that value. This is why we need MemoryStreams, not just
41 * Streams. Note: Cycle DOM driver is an example of such case, since it waits
42 * for 'readystatechange'.
43 */
44export type SinkProxies<Si extends Sinks> = {
45 [P in keyof Si]: MemoryStream<any>
46};
47
48export type FantasySinks<Si> = {[S in keyof Si]: FantasyObservable};
49
50export interface Driver<Sink, Source> {
51 (stream: Sink, driverName?: string): Source;
52}
53
54export type Drivers<So extends Sources, Si extends Sinks> = {
55 [P in keyof (So & Si)]: Driver<Si[P], So[P]>
56};
57
58export type DisposeFunction = () => void;
59
60export interface CycleProgram<So extends Sources, Si extends Sinks> {
61 sources: So;
62 sinks: Si;
63 run(): DisposeFunction;
64}