Repository navigation
Expand file tree
/
Copy pathReactSource.ts
More file actions
60 lines (51 loc) · 1.69 KB
/
Copy pathReactSource.ts
File metadata and controls
60 lines (51 loc) · 1.69 KB
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
import xs, {Stream, MemoryStream} from 'xstream';
import {ReactElement, createElement} from 'react';
import {adapt} from '@cycle/run/lib/adapt';
import {Scope} from './scope';
import {ScopeContext} from './context';
type Sink = Stream<ReactElement<any>>;
export class ReactSource<P = any> {
public _selector: string | symbol | null;
public _scope: Scope;
public _props$: MemoryStream<P>;
public _childScopes: Map<string, Scope>;
constructor(
scope: Scope = new Scope(),
selector: string | symbol | null = null,
props$: MemoryStream<P> = xs.createWithMemory<P>()
) {
this._scope = scope;
this._selector = selector;
this._props$ = props$;
this._childScopes = new Map();
}
public select(selector: string | symbol): ReactSource {
return new ReactSource(this._scope, selector, this._props$);
}
public events<T = any>(eventType: string): Stream<T> {
if (this._selector === null) {
return adapt(xs.empty());
} else {
return adapt(this._scope.getHandler(this._selector, eventType));
}
}
public props(): MemoryStream<P> {
return adapt(this._props$);
}
private getChildScope(scopeId: string): Scope {
if (!this._childScopes.has(scopeId)) {
this._childScopes.set(scopeId, new Scope());
}
return this._childScopes.get(scopeId) as Scope;
}
public isolateSink(sink: Sink, scopeId: string): Sink {
const isolation = this.getChildScope(scopeId);
return sink.map((elem) =>
createElement(ScopeContext.Provider, {value: isolation}, elem)
);
}
public isolateSource(source: ReactSource, scopeId: string): ReactSource {
const isolation = this.getChildScope(scopeId);
return new ReactSource(isolation);
}
}