-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromiseStream.ts
274 lines (214 loc) · 7.98 KB
/
PromiseStream.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import * as Rx from "rxjs";
import { PartialObserver } from "rxjs/Observer";
export class PromiseStream<T> implements PromiseLike<T[]> {
public do = this.peek;
public all = this.collect;
public take = this.limit;
private observed: Rx.Observable<T>;
private constructor(observable: Rx.Observable<T>) {
this.observed = observable;
}
public static from<R>(unknown: Rx.Observable<R> | Array<PromiseLike<R>> | Promise<R[]> | R[] | R): PromiseStream<R> {
if (unknown instanceof Rx.Observable) {
return PromiseStream.fromObservable(unknown);
}
if (PromiseStream.isPromise<R[]>(unknown)) {
return PromiseStream.fromPromise(unknown);
}
if (unknown instanceof Array) {
return PromiseStream.fromArray(unknown);
}
return new PromiseStream(Rx.Observable.of(unknown));
}
public static fromObservable<R>(observable: Rx.Observable<R>): PromiseStream<R> {
return new PromiseStream(observable);
}
public static fromArray<R>(array: Array<PromiseLike<R>> | R[]): PromiseStream<R> {
const observable = Rx.Observable.from<R>(array as R[]).mergeMap((p: R) => {
if (PromiseStream.isPromise<R>(p)) {
return p;
} else {
return [p];
}
});
return new PromiseStream<R>(observable);
}
public static fromPromise<R>(promiseArray: PromiseLike<R | R[]>): PromiseStream<R> {
return new PromiseStream(Rx.Observable.fromPromise(promiseArray).mergeMap((array: R | R[]) => array instanceof Array ? array : [array]));
}
private static isPromise<R>(value: any): value is PromiseLike<R> {
return typeof value.then === "function";
}
public map<R>(callback: (value: T) => R | PromiseLike<R>): PromiseStream<R> {
const observable = this.observed.mergeMap((value: T) => {
const promiseOrValue = callback(value);
if (PromiseStream.isPromise(promiseOrValue)) {
return Rx.Observable.fromPromise(promiseOrValue);
} else {
return [promiseOrValue];
}
});
return PromiseStream.fromObservable(observable);
}
public filter(callback: (value: T) => boolean | PromiseLike<boolean>): PromiseStream<T> {
const observable = Rx.Observable.create((observer: Rx.Observer<T>) => {
this.observed.forEach((value: T) => {
const promiseOrBoolean = callback(value);
if (!promiseOrBoolean) return;
if (PromiseStream.isPromise<T>(promiseOrBoolean)) {
return promiseOrBoolean.then((bool) => {
if (bool) {
observer.next(value);
}
});
}
observer.next(value);
}).then(() => observer.complete(), (e) => observer.error(e));
});
return PromiseStream.fromObservable(observable);
}
public flatten(): PromiseStream<T> {
const observable = this.observed.mergeMap((value: T | T[]) => value instanceof Array ? value : [value]);
return PromiseStream.fromObservable(observable);
}
public flatMap<R>(callback: (value: T) => R[] | R | PromiseLike<R | R[]>): PromiseStream<R> {
const observable = this.observed.mergeMap((value: T) => {
const promiseOrArrayOrValue = callback(value);
if (PromiseStream.isPromise(promiseOrArrayOrValue)) {
return Rx.Observable.fromPromise(promiseOrArrayOrValue as PromiseLike<R>).mergeMap((array: R | R[]) => array instanceof Array ? array : [array]);
}
if (promiseOrArrayOrValue instanceof Array) {
return promiseOrArrayOrValue;
}
return [promiseOrArrayOrValue];
});
return PromiseStream.fromObservable(observable);
}
public concat(concatable: PromiseStream<T> | Array<PromiseLike<T>> | PromiseLike<T[]> | Rx.Observable<T> | T[]): PromiseStream<T> {
if (PromiseStream.isPromise<T>(concatable) || concatable instanceof Array) {
concatable = PromiseStream.from(concatable);
}
return PromiseStream.fromObservable(this.observed.merge(concatable));
}
public some(matchCallback: (value: T) => boolean | PromiseLike<boolean>): Promise<boolean> {
let resolved = false;
return new Promise((resolve, reject) => {
const subscription = this.observed.subscribe((value: T) => {
if (resolved) { return; }
const result = matchCallback(value);
if (PromiseStream.isPromise(result)) {
return result.then((doesMatch) => {
if (doesMatch) {
resolve(true);
resolved = true;
subscription.unsubscribe();
}
});
}
if (result) {
resolve(true);
resolved = true;
subscription.unsubscribe();
}
}, (e) => reject(e), () => resolved || resolve(false));
});
}
public every(matchCallback: (value: T) => boolean | PromiseLike<boolean>): Promise<boolean> {
let resolved = false;
return new Promise((resolve, reject) => {
const subscription = this.observed.subscribe((value: T) => {
if (resolved) { return; }
const result = matchCallback(value);
if (PromiseStream.isPromise(result)) {
return result.then((doesMatch) => {
if (!doesMatch) {
resolve(false);
resolved = true;
subscription.unsubscribe();
}
});
}
if (!result) {
resolve(false);
resolved = true;
subscription.unsubscribe();
}
}, (e) => reject(e), () => resolved || resolve(true));
});
}
public find(matchCallback: (value: T) => boolean | PromiseLike<boolean>): Promise<T | null> {
let resolved = false;
return new Promise((resolve, reject) => {
const subscription = this.observed.subscribe((value: T) => {
if (resolved) { return; }
const result = matchCallback(value);
if (PromiseStream.isPromise(result)) {
return result.then((doesMatch) => {
if (doesMatch && !resolved) {
resolve(value);
resolved = true;
subscription.unsubscribe();
}
});
}
if (result) {
resolve(value);
resolved = true;
subscription.unsubscribe();
}
}, (e) => reject(e), () => resolved || resolve(null));
});
}
public reduce<R>(callback: (accumulator: R, currentValue: T) => R | Promise<R>, initialValue: R | PromiseLike<R>): Promise<R> {
let accumulator: Promise<R> = Promise.resolve(initialValue);
return this.observed.forEach((value: T) => {
accumulator = accumulator.then((result) => callback(result, value));
}).then(() => accumulator);
}
public distinct(): PromiseStream<T> {
return new PromiseStream(this.observed.distinct());
}
public min(comparator: (a: T, b: T) => number): Promise<T> {
return this.observed.min(comparator).toPromise();
}
public max(comparator: (a: T, b: T) => number): Promise<T> {
return this.observed.max(comparator).toPromise();
}
public sum(adder: (a: T, b: T) => T): Promise<T> {
return this.observed.first().toPromise().then((first: T) => {
return new PromiseStream<T>(this.observed.skip(1)).reduce(adder, first);
});
}
public count(): Promise<number> {
return this.observed.count().toPromise();
}
public limit(maxElements: number): PromiseStream<T> {
return new PromiseStream(this.observed.take(maxElements));
}
public collect(): Promise<T[]> {
const values: T[] = [];
return this.observed.forEach((v: T) => values.push(v)).then(() => values);
}
public forEach(callback: (value: T) => any): Promise<void> {
return this.observed.forEach(callback);
}
public peek(callback: (value: T) => any): this {
this.forEach(callback);
return this;
}
public observable(): Rx.Observable<T> {
return this.observed;
}
public subscribe(): Rx.Subscription;
public subscribe(observer: PartialObserver<T>): Rx.Subscription;
public subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Rx.Subscription;
public subscribe(next?: any, error?: (error: any) => void, complete?: () => void): Rx.Subscription {
return this.observed.subscribe(next, error, complete);
}
public then<TResult1 = T[], TResult2 = never>(onfulfilled?: ((values: T[]) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2> {
return this.collect().then(onfulfilled, onrejected);
}
public catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T[] | TResult> {
return this.collect().catch(onrejected);
}
}