-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
207 lines (189 loc) · 5.18 KB
/
index.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import * as React from "react";
import { BehaviorSubject, identity, Observable, OperatorFunction, Subject, Subscription } from "rxjs";
import { distinctUntilChanged, map, scan, skip } from "rxjs/operators";
export type Mutation<T> = (t: T) => T;
export type MapToUnion<T> = T[keyof T];
export type MapToAction<T> = {
[K in keyof T]: {
type: K;
payload: T[K];
};
};
export type MapToConditionalAction<T> = MapToUnion<MapToAction<T>>;
export interface Action<T = any> {
type: string | number | symbol;
payload?: T;
}
export interface Reducer<T, Action> {
(state: T, action: Action): T;
}
export type Store<T, PayloadType = {}> = {
readonly stream: BehaviorSubject<T>;
readonly value: T;
next(m: Mutation<T>): void;
dispatch(action: MapToConditionalAction<PayloadType>): void;
destroy(): void;
use(): T;
};
export function createStore<T, PayloadType = {}>(
defaultState: T,
middleware: OperatorFunction<Mutation<T>, Mutation<T>> = identity,
reducer: Reducer<T, MapToConditionalAction<PayloadType>> = (s) => s
): Store<T, PayloadType> {
const mutations = new Subject<Mutation<T>>();
const stream = new BehaviorSubject(defaultState);
mutations
.pipe(
middleware,
scan<Mutation<T>, T>((state, mutation) => {
return mutation(state);
}, defaultState),
distinctUntilChanged(),
)
.subscribe(stream);
return {
get stream() {
return stream;
},
get value() {
return stream.value;
},
next(m) {
mutations.next(m);
},
dispatch(action) {
mutations.next((state) => reducer(state, action));
},
//we don't use the name `complete` because it will cause store to terminate when you use pattern like .subscribe(store)
destroy() {
mutations.complete();
stream.complete();
},
use() {
return useObservables(stream)[0];
},
};
}
type ObservedValueOf<T> = T extends BehaviorSubject<infer U1>
? U1
: T extends Observable<infer U2>
? U2 | null
: never;
export function useObservables<T>(
ob: T | undefined | null
): [ObservedValueOf<T>];
export function useObservables<T1, T2>(
ob1: T1 | undefined | null,
ob2: T2 | undefined | null
): [ObservedValueOf<T1>, ObservedValueOf<T2>];
export function useObservables<T1, T2, T3>(
ob1: T1 | undefined | null,
ob2: T2 | undefined | null,
ob3: T3 | undefined | null
): [ObservedValueOf<T1>, ObservedValueOf<T2>, ObservedValueOf<T3>];
export function useObservables<T1, T2, T3, T4>(
ob1: T1 | undefined | null,
ob2: T2 | undefined | null,
ob3: T3 | undefined | null,
ob4: T4 | undefined | null
): [
ObservedValueOf<T1>,
ObservedValueOf<T2>,
ObservedValueOf<T3>,
ObservedValueOf<T4>
];
export function useObservables<T1, T2, T3, T4, T5>(
ob1: T1 | undefined | null,
ob2: T2 | undefined | null,
ob3: T3 | undefined | null,
ob4: T4 | undefined | null,
ob5: T5 | undefined | null
): [
ObservedValueOf<T1>,
ObservedValueOf<T2>,
ObservedValueOf<T3>,
ObservedValueOf<T4>,
ObservedValueOf<T5>
];
export function useObservables(...obs: (Observable<any> | null | undefined)[]) {
const [v, setV] = React.useState(() => {
return obs.map((x) => (x instanceof BehaviorSubject ? x.value : null));
});
React.useEffect(() => {
let subs = obs.map((x, i) => {
if (x) {
return (x instanceof BehaviorSubject ? skip(1)(x) : x).subscribe(
(x) => {
setV((value) => {
value[i] = x;
return value.slice();
});
}
);
}
});
return () => {
subs.forEach((x) => x && x.unsubscribe());
};
}, obs);
return v;
}
export function useSink<T>(
operation: (sub: Subject<T>) => Subscription,
deps: any[] = []
): Subject<T>["next"] {
const [subject, next] = React.useMemo<[Subject<T>, Subject<T>["next"]]>(
() => {
const subject = new Subject<T>();
return [subject, subject.next.bind(subject)];
},
deps
);
React.useEffect(() => {
const subscription: Subscription = operation(subject);
return () => {
subject.complete();
subscription.unsubscribe(); //this is to prevent leak when operation fn contains some operation like combineLatest
};
}, [subject]);
return next;
}
/**
* @deprecated use useObservables
*/
export function useObservable<T>(ob: Observable<T>) {
const [value, setValue] = React.useState<T | null>(null);
React.useEffect(() => {
const sub = ob.subscribe(setValue);
return sub.unsubscribe.bind(sub);
}, [ob]);
return value;
}
/**
* @deprecated use useObservables
*/
export function useSource<State, Slice = State>(
ob: Observable<State>,
operator: (s: Observable<State>) => Observable<Slice> = map((x) => x as any),
deps: any[] = []
) {
const selected = React.useMemo(() => {
return ob.pipe(operator, distinctUntilChanged<Slice>(shallowEqual));
}, [ob, ...deps]);
return useObservable(selected);
}
function shallowEqual(a: any, b: any) {
if (
typeof a !== "object" ||
a === null ||
typeof b !== "object" ||
b === null
) {
return a === b;
} else {
const ka = Object.keys(a);
const kb = Object.keys(b);
if (ka.length !== kb.length) return false;
return ka.every((k) => a[k] === b[k]);
}
}