1 | import {Stream} from 'xstream';
|
2 |
|
3 | export type FantasyObserver<T> = {
|
4 | next(x: T): void;
|
5 | error(err: any): void;
|
6 | complete(c?: any): void;
|
7 | };
|
8 |
|
9 | export type FantasySubscription = {
|
10 | unsubscribe(): void;
|
11 | };
|
12 |
|
13 | export type FantasyObservable<T> = {
|
14 | subscribe(observer: FantasyObserver<T>): FantasySubscription;
|
15 | };
|
16 |
|
17 | export type DevToolEnabledSource = {
|
18 | _isCycleSource: string;
|
19 | };
|
20 |
|
21 | export type SinkProxies<Si> = {[P in keyof Si]: Stream<any>};
|
22 |
|
23 | export type Driver<Si, So> = Si extends void
|
24 | ? (() => So)
|
25 | : ((stream: Si) => So);
|
26 |
|
27 | export type DisposeFunction = () => void;
|
28 |
|
29 | export type Drivers = {
|
30 | [name: string]: Driver<Stream<any>, any | void>;
|
31 | };
|
32 |
|
33 | export type Main = (...args: Array<any>) => any;
|
34 |
|
35 | export type Sources<D extends Drivers> = {[k in keyof D]: ReturnType<D[k]>};
|
36 |
|
37 | export type Sinks<M extends Main> = ReturnType<M>;
|
38 |
|
39 | export 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 |
|
49 |
|
50 |
|
51 |
|
52 | export type ToStream<S> = S extends FantasyObservable<infer T> ? Stream<T> : S;
|
53 |
|
54 | export type WidenStream<S, U> = S extends Stream<infer T>
|
55 | ? (T extends U ? U : never)
|
56 | : any;
|
57 |
|
58 | export 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 |
|
65 | export 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 |
|
74 | export 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 |
|
83 | export 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 |