Skip to content

Commit

Permalink
perf: using shareReplay to reuse previous result
Browse files Browse the repository at this point in the history
  • Loading branch information
ElonH committed May 21, 2020
1 parent 21e6827 commit 179d5d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
25 changes: 14 additions & 11 deletions src/app/@dataflow/core/bare-flow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Observable, of } from 'rxjs';
import { switchMap, take, tap, startWith, distinctUntilChanged, skipWhile } from 'rxjs/operators';
import {
switchMap,
take,
tap,
startWith,
distinctUntilChanged,
skipWhile,
shareReplay,
} from 'rxjs/operators';

export interface FlowInNode {}
export interface FlowOutNode {}
Expand All @@ -10,28 +18,23 @@ export abstract class BareFlow<Tin extends FlowInNode, Tout extends FlowOutNode>
protected abstract request(pre: CombErr<Tin>): Observable<CombErr<Tout>>;
private bareData$: Observable<CombErr<Tout>>;
private deployed = false;
private boostrapData: CombErr<Tout>;
public deploy() {
this.bareData$ = this.prerequest$.pipe(
switchMap(
(pre): Observable<CombErr<Tout>> => {
if (pre[1].length === 0) return this.request(pre).pipe(take(1));
return of((pre as any) as CombErr<Tout>); // force to convert. There are some errors at privious flow.
// Just make sure that checking Error[] at first in subscription
return of((pre as any) as CombErr<Tout>); // force to convert. There are some errors at privious flow.
// Just make sure that checking Error[] at first in subscription
}
),
tap((x) => (this.boostrapData = x))
distinctUntilChanged(),
shareReplay()
);
this.bareData$.pipe(take(1)).subscribe();
this.deployed = true;
}
public getOutput(): Observable<CombErr<Tout>> {
if (!this.deployed) throw new Error('run deploy before getOutput');
return this.bareData$.pipe(
startWith(this.boostrapData),
distinctUntilChanged(),
skipWhile((x) => typeof x === 'undefined')
// skip(1), // don't why need it , otherwise, test failure. refs: https://stackoverflow.com/a/52157317
);
return this.bareData$;
}
}
21 changes: 3 additions & 18 deletions src/app/@dataflow/core/superset-flow.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { BareFlow, FlowInNode, FlowOutNode, CombErr } from './bare-flow';
import { Observable } from 'rxjs';
import {
tap,
take,
startWith,
distinctUntilChanged,
skip,
withLatestFrom,
map,
} from 'rxjs/operators';
import { take, distinctUntilChanged, withLatestFrom, map, shareReplay } from 'rxjs/operators';

export interface FlowSupNode {}

Expand All @@ -18,10 +10,9 @@ export abstract class SupersetFlow<
Tsup extends FlowSupNode = Tin & Tout
> extends BareFlow<Tin, Tout> {
private boostrapPrerequest$: Observable<CombErr<Tin>>;
private boostrapPrerequest: CombErr<Tin>;
public deploy() {
super.deploy();
this.boostrapPrerequest$ = this.prerequest$.pipe(tap((x) => (this.boostrapPrerequest = x)));
this.boostrapPrerequest$ = this.prerequest$.pipe(distinctUntilChanged(), shareReplay());
this.boostrapPrerequest$.pipe(take(1)).subscribe();
}
protected generateSuperset(current: CombErr<Tout>, previous: CombErr<Tin>): CombErr<Tsup> {
Expand All @@ -32,13 +23,7 @@ export abstract class SupersetFlow<
}
public getSupersetOutput(): Observable<CombErr<Tsup>> {
return this.getOutput().pipe(
withLatestFrom(
this.boostrapPrerequest$.pipe(
startWith(this.boostrapPrerequest),
distinctUntilChanged(),
skip(1)
)
),
withLatestFrom(this.boostrapPrerequest$),
map(([cur, pre]) => this.generateSuperset(cur, pre))
);
}
Expand Down

0 comments on commit 179d5d0

Please sign in to comment.