This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
59 lines (49 loc) · 1.52 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
import { Observable, of, empty, from } from 'rxjs';
import { map, concat, concatMap, concatAll, expand, toArray, delay, tap } from 'rxjs/operators';
type Getter = (x: number) => Observable<number>;
const http = {
1: [2, 6],
2: [3, 4],
4: [5],
6: [7, 13, 14],
7: [8, 9, 10, 11, 12],
14: [15, 16],
17: [18],
18: [19],
20: [21],
};
const getChildren = (id: number) =>
(http[id] ? from(http[id] as number[]) : empty());
const getChildrenWithDelay = (id: number) =>
getChildren(id).pipe(delay(Math.random() * 300));
class Test {
// This doesn't maintain depth-first order; it can vary depending on timing.
brokenDFS(getChildren: Getter, ids: number[]): Observable<number[]> {
return of(ids).pipe(
concatMap(ids => from(ids)),
expand(id => getChildren(id)),
toArray()
);
}
workingDFS(getChildren: Getter, ids: number[]): Observable<number[]> {
return from(ids).pipe(
concatMap(id => this.parentAndChildren(getChildren, id)),
toArray()
);
}
private parentAndChildren(getChildren: Getter, id: number): Observable<number> {
return of(id).pipe(
concat(
getChildren(id).pipe(
map(child => this.parentAndChildren(getChildren, child)),
concatAll()
)
),
);
}
}
const getter = getChildrenWithDelay;
const rootIds = [1, 17, 20];
const test = new Test();
test.brokenDFS(getter, rootIds).subscribe(data => console.log(`Broken: ${data}`));
test.workingDFS(getter, rootIds).subscribe(data => console.log(`Working: ${data}`));