UNPKG

3.28 kBJavaScriptView Raw
1"use strict";
2// THESE HAVE BEEN REMOVED IN v2.0
3// Justification: These helpers encourage usage of observable inside of presentation components. This violates
4// the smart/dumb (a.k.a. container/presentational) pattern. To not encourage new users to bad practices, they are not
5// exposed anymore. Code is kept for reference and possible future internal use.
6// import { Observable, Subscription } from 'rxjs';
7// /**
8// * A map specifying which property on the components state should be populated with the value of the map value (=observable)
9// *
10// * @example
11// * const map = {
12// * secondsPassed: Observable.interval(1000)
13// * }
14// */
15// export type UnpackMap<TComponentState> = {
16// [P in keyof TComponentState]?: Observable<TComponentState[P]>
17// }
18// /*
19// * Can be used to bind the last emitted item of multiple observables to a component's internal state.
20// *
21// * @param component - The component of which we set the internal state
22// * @param map - A map for which each key in the map will used as target state property to set the observable item to
23// */
24// export function unpackToState<TComponentState extends {}>(
25// component: React.Component<object, TComponentState>,
26// map: UnpackMap<TComponentState>
27// ): Subscription {
28// const subscriptions = new Subscription();
29// for (let key in map) {
30// const observable = map[key];
31// if (observable === undefined)
32// continue;
33// if (typeof observable.subscribe !== "function") {
34// throw new Error(`Could not map non-observable for property ${key}`)
35// }
36// subscriptions.add(bindToState(component, observable!, key));
37// }
38// return subscriptions;
39// }
40// export function mapToState<T, TComponentState, TComponentProps>(
41// component: React.Component<TComponentProps, TComponentState>,
42// source: Observable<T>,
43// setStateFn: (item: T, prevState: TComponentState, props: TComponentProps) => TComponentState
44// ): Subscription {
45// return source.subscribe((item: T) => {
46// component.setState((prevState: TComponentState, props: TComponentProps) => {
47// return setStateFn(item, prevState, props);
48// })
49// })
50// }
51// /**
52// * Sets the emitted values of an observable to a components state using setState(). The
53// * subscription to the source observable is automatically unsubscribed when the component
54// * unmounts.
55// */
56// export function bindToState<T, TState extends object>(
57// component: React.Component<any, TState> ,
58// source: Observable<T>,
59// stateKey: keyof TState
60// ): Subscription {
61// const subscription = source.subscribe((item: T) => {
62// const patch = { [stateKey]: item };
63// // TODO eliminate any
64// component.setState((prevState: any) => ({ ...prevState, ...patch }))
65// })
66// // unsubscribe then the component is unmounted
67// const originalUnmount = component.componentWillUnmount;
68// component.componentWillUnmount = function() {
69// subscription.unsubscribe();
70// if (originalUnmount) {
71// originalUnmount.call(component);
72// }
73// }.bind(component);
74// return subscription;
75// }
76//# sourceMappingURL=state.js.map
\No newline at end of file