UNPKG

2.08 kBPlain TextView Raw
1import {Stream} from 'xstream';
2
3export type FantasyObserver<T> = {
4 next(x: T): void;
5 error(err: any): void;
6 complete(c?: any): void;
7};
8
9export type FantasySubscription = {
10 unsubscribe(): void;
11};
12
13export type FantasyObservable<T> = {
14 subscribe(observer: FantasyObserver<T>): FantasySubscription;
15};
16
17export type DevToolEnabledSource = {
18 _isCycleSource: string;
19};
20
21export type SinkProxies<Si> = {[P in keyof Si]: Stream<any>};
22
23export type Driver<Si, So> = Si extends void
24 ? (() => So)
25 : ((stream: Si) => So);
26
27export type DisposeFunction = () => void;
28
29export type Drivers = {
30 [name: string]: Driver<Stream<any>, any | void>;
31};
32
33export type Main = (...args: Array<any>) => any;
34
35export type Sources<D extends Drivers> = {[k in keyof D]: ReturnType<D[k]>};
36
37export type Sinks<M extends Main> = ReturnType<M>;
38
39export type MatchingMain<D extends Drivers, M extends Main> =
40 | Main & {
41 (so: Sources<D>): Sinks<M>;
42 }
43 | Main & {
44 (): Sinks<M>;
45 };
46
47/**
48 * For whatever reason, this does not work with RxJS observables,
49 * this for this reason, `MatchingDrivers` has to be redefined
50 * in @cycle/rxjs-run-
51 */
52export type ToStream<S> = S extends FantasyObservable<infer T> ? Stream<T> : S;
53
54export type WidenStream<S, U> = S extends Stream<infer T>
55 ? (T extends U ? U : never)
56 : any;
57
58export type GetValidInputs<D extends Driver<any, any>> = D extends Driver<
59 infer S,
60 any
61>
62 ? (S extends Stream<infer T> ? T : never)
63 : never;
64
65export type MatchingDrivers<D extends Drivers, M extends Main> = Drivers &
66 {
67 [k in string & keyof Sinks<M>]:
68 | (() => Sources<D>[k])
69 | ((
70 si: Stream<WidenStream<ToStream<Sinks<M>[k]>, GetValidInputs<D[k]>>>
71 ) => Sources<D>[k])
72 };
73
74export interface CycleProgram<
75 D extends MatchingDrivers<D, M>,
76 M extends MatchingMain<D, M>
77> {
78 sources: Sources<D>;
79 sinks: Sinks<M>;
80 run(): DisposeFunction;
81}
82
83export interface Engine<D extends Drivers> {
84 sources: Sources<D>;
85 run<M extends MatchingMain<D, M>>(sinks: Sinks<M>): DisposeFunction;
86 dispose(): void;
87}
88
\No newline at end of file