-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrxreact.tsx
More file actions
70 lines (63 loc) · 2.47 KB
/
Copy pathrxreact.tsx
File metadata and controls
70 lines (63 loc) · 2.47 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
61
62
63
64
65
66
67
68
69
70
import * as React from "react";
import { subjectMapToActionMap } from "./subjectMapToActionMap";
import { combineObservables } from "./combineObservables";
import { Observable, Subject, Subscription, ReplaySubject } from "rxjs";
import {
ObservableMap,
SubjectMap,
ActionMap,
ViewModelFactory,
Difference,
ViewModel
} from "./types";
function withViewModelFactory<S, A, P extends S & ActionMap<A>>(
viewModelFactory: ViewModelFactory<S, A, Difference<P, S & ActionMap<A>>>
): (
WrappedComponent: React.ComponentType<P>
) => React.ComponentClass<Difference<P, S & ActionMap<A>>> {
return function wrapWithConnect(
WrappedComponent: React.ComponentType<P>
): React.ComponentClass<Difference<P, S & ActionMap<A>>> {
return class ConnectState extends React.Component<Difference<P, S & ActionMap<A>>, S> {
subscription: Subscription | undefined;
actions: ActionMap<A>;
observableState: Observable<S>;
propsObservable: Subject<Difference<P, S & ActionMap<A>>> = new ReplaySubject<
Difference<P, S & ActionMap<A>>
>(1);
constructor(props: Difference<P, S & ActionMap<A>>) {
super(props);
let viewModel = viewModelFactory(this.propsObservable);
this.observableState = Object.keys(viewModel.inputs).length ? combineObservables(viewModel.inputs) : Observable.of({} as S);
this.actions = subjectMapToActionMap(viewModel.outputs);
}
componentWillMount() {
this.propsObservable.next(this.props);
this.subscription = this.observableState.subscribe(newState => this.setState(newState));
}
componentWillReceiveProps(nextProps: Difference<P, S & ActionMap<A>>) {
this.propsObservable.next(nextProps);
}
componentWillUnmount() {
this.subscription && this.subscription.unsubscribe();
}
render() {
if (this.state !== null) {
return <WrappedComponent {...this.state} {...this.actions} {...this.props} />;
} else {
return null;
}
}
};
};
}
export function withViewModel<S, A, P extends S & ActionMap<A>>(
viewModel: ViewModel<S, A> | ViewModelFactory<S, A, Difference<P, S & ActionMap<A>>>,
WrappedComponent: React.ComponentType<P>
): React.ComponentClass<Difference<P, S & ActionMap<A>>> {
if (typeof viewModel === "function") {
return withViewModelFactory<S, A, P>(viewModel)(WrappedComponent);
} else {
return withViewModelFactory<S, A, P>(() => viewModel)(WrappedComponent);
}
}