This repository has been archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointset.ts
84 lines (75 loc) · 3 KB
/
pointset.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
import { MemSet } from './memarrayvisitor';
import { IDataSet, DataSet } from './dataset';
import { MemOperation } from './memoperation';
import { Utility } from './core';
/**
* Ne kadar Expression eklenirse eklenirsin WhenMemorized kısmı sonra çalışır. Pipesetde ise MemOperationdan sonra işlemler memory üzerinden gerçekleşir.
*/
export class Pointset<T> extends DataSet<T>{
constructor(private datasource: IDataSet<T>, expressions: Array<any> = [], private memOperations: Array<any> = []) {
super(expressions)
}
getExpressions(): any[] {
return this.expressions.map(x => x);
}
private static insureAsPromise(value) {
return Utility.instanceof(value,Promise)? value : Promise.resolve(value);
}
/**
* Hafıza alındığında yapılacak işlemler.
* @param expressions MemOperation ya da expressions
*/
whenMemorized(...expressions: Array<MemOperation | any>): Pointset<T> {
this.memOperations = this.memOperations.concat(expressions);
return this;
}
private applyMemOps(ops: Array<any>, response: Array<any>) {
if (!Array.isArray(response) || ops.length === 0) return Pointset.insureAsPromise(response);
let operations = [].concat(ops);
let pipe = operations.pop();
if (typeof pipe === "function") {
return Pointset.insureAsPromise(pipe(response)).then((response) => {
return this.applyMemOps(operations, response).then((resp)=>{
return resp;
});
});
}
if (Utility.instanceof(pipe,MemOperation))
return Pointset.insureAsPromise(pipe.pipe(response)).then((response) => {
return this.applyMemOps(operations, response).then((resp)=>{
return resp;
});
});
//expression da olabilir.
return new MemSet(response, [pipe]).then((resp) => {
return resp;
});
}
private withOwnExpressions(expressions:any){
let exps = expressions ||[];
if(this.expressions == null) return exps.concat();
if(!Array.isArray(this.expressions)){
throw new Error('expressions are not support');
}
return this.expressions.concat.apply(this.expressions,exps);
}
get(...expressions: any[]): Promise<any> {
let exps = this.withOwnExpressions(expressions);
return this.datasource.get.apply(this.datasource,exps)
.then((response) => {
return this.applyMemOps(this.memOperations, response);
});
}
add(element: T): Promise<any> {
return this.datasource.add(element);
}
delete(element: T): Promise<any> {
return this.datasource.delete(element);
}
update(element: T): Promise<any> {
return this.datasource.update(element);
}
query(...expressions: any[]): Pointset<T> {
return new Pointset(this.datasource, this.withOwnExpressions(expressions), this.memOperations.map(x => x));
}
}