Skip to content

Commit

Permalink
refactor(generic): rename class member and improve cohesion
Browse files Browse the repository at this point in the history
using clear name. Moving class member `cmd` and `params` to RcloneAuth to implove code cohesion

BREAKING CHANGE: using RcloneAuth instand of Generic
  • Loading branch information
ElonH committed May 13, 2020
1 parent 355fcb3 commit d8bec07
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/app/@dataflow/generic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestGeneric extends Generic {
protected request(x: DataFlowNode): AjaxRequest {
return {} as AjaxResponse;
}
protected generateModule(current: DataFlowNode, previous: DataFlowNode): DataFlowNode {
protected generateSuperset(current: DataFlowNode, previous: DataFlowNode): DataFlowNode {
return [{ pre: previous[0]['pre'], cur: 2 }, null];
}
}
Expand Down
48 changes: 27 additions & 21 deletions src/app/@dataflow/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,37 @@ import { AjaxResponse, ajax, AjaxRequest } from 'rxjs/ajax';
export type DataFlowNode = [object, any[]];

export abstract class Generic {
protected abstract cmd: string;
protected abstract params: object;
protected abstract cacheSupport: boolean;

protected cachePath?: string;
protected abstract cachePath?: string;

protected abstract prerequest(): Observable<DataFlowNode>;
protected abstract request(x: DataFlowNode): AjaxRequest;
protected _request(x: DataFlowNode): Observable<AjaxResponse> {
return ajax(this.request(x));
}
protected operate(rsp: Observable<DataFlowNode>): Observable<DataFlowNode> {
protected reconstruct(rsp: Observable<DataFlowNode>): Observable<DataFlowNode> {
return rsp;
}
protected abstract generateModule(current: DataFlowNode, previous: DataFlowNode): DataFlowNode;
protected abstract generateSuperset(current: DataFlowNode, previous: DataFlowNode): DataFlowNode;

private interalData$: Observable<DataFlowNode>;
private data$: Observable<DataFlowNode>;
private static cacheData: { [index: string]: DataFlowNode } = {};
private moduleRst: DataFlowNode;
private static cacheStorage: { [index: string]: DataFlowNode } = {};
private supersetCache: DataFlowNode;

/**
* getOutput
*/
public getDataOutput(): Observable<DataFlowNode> {
public getOutput(): Observable<DataFlowNode> {
return this.data$;
}

/**
* getModelOutput
* getSupersetoutput
*/
public getModuleOutput(): Observable<DataFlowNode> {
return this.interalData$.pipe(startWith(this.moduleRst), distinctUntilChanged());
public getSupersetOutput(): Observable<DataFlowNode> {
return this.interalData$.pipe(startWith(this.supersetCache), distinctUntilChanged());
}

/**
Expand All @@ -54,40 +52,43 @@ export abstract class Generic {
public deploy() {
const prerequest$ = this.prerequest();
// request
if (!this.cachePath) this.cachePath = JSON.stringify([this.cmd, this.params]);
const request$ = prerequest$.pipe(
switchMap(
([data, preErrors]): Observable<DataFlowNode> => {
if (preErrors.length !== 0) return prerequest$; // todo: test work around? or replase as of([null, preErrors])
return iif(
() => this.cacheSupport && Generic.cacheData.hasOwnProperty(this.cachePath),
of(Generic.cacheData[this.cachePath]),
() => this.cacheSupport && Generic.cacheStorage.hasOwnProperty(this.cachePath),
of(Generic.cacheStorage[this.cachePath]),
this._request([data, preErrors]).pipe(
map((rsp): DataFlowNode => [rsp, []]),
catchError((err): Observable<DataFlowNode> => of([{}, [err]])),
tap((x: DataFlowNode) => {
if (this.cacheSupport) Generic.cacheData[this.cachePath] = x;
if (this.cacheSupport) Generic.cacheStorage[this.cachePath] = x;
})
)
);
}
)
);
// operate
const operate$ = this.operate(request$);
this.interalData$ = operate$.pipe(
// reconstruct
const reconstruct$ = this.reconstruct(request$);
this.interalData$ = reconstruct$.pipe(
withLatestFrom(prerequest$),
map(([cur, pre]) => this.generateModule(cur, pre)),
tap((x) => (this.moduleRst = x))
map(([cur, pre]) => this.generateSuperset(cur, pre)),
tap((x) => (this.supersetCache = x))
);
this.data$ = operate$.pipe(
this.data$ = reconstruct$.pipe(
withLatestFrom(this.interalData$),
map(([x, y]) => x)
);
}
}

export abstract class RcloneAuth extends Generic {
protected abstract cmd: string;
protected abstract params: object;
protected cachePath?: string;

protected request(x: DataFlowNode): AjaxRequest {
let headers = {
'Content-Type': 'application/json',
Expand All @@ -101,4 +102,9 @@ export abstract class RcloneAuth extends Generic {
body: this.params,
};
}

public deploy() {
if (!this.cachePath) this.cachePath = JSON.stringify([this.cmd, this.params]);
super.deploy();
}
}

0 comments on commit d8bec07

Please sign in to comment.