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
/
trackingmemset.ts
85 lines (72 loc) · 2.73 KB
/
trackingmemset.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
import { Utility } from './core';
import { Select, Property } from './expressions';
import { MemSet } from './memarrayvisitor';
import { select } from './operations';
export class TrackingMemset extends MemSet{
constructor(private memset:MemSet){
super(memset["source"],memset.getExpressions());
this.trackingId = memset.trackingId;
}
private trackObject(index,object){
let element = (Array.isArray(this.source)?this.source:[this.source])[index];
let newResult = {
test:'timeout'
};
for(let i in object){
let _value = object[i];
Utility.ObjectDefineProperty(newResult,i,{
get:function (){
return _value;
},
set:function (newValue){
_value = newValue;
element[i] = newValue;
},
configurable:true,
enumerable:true
})
}
Object.getOwnPropertySymbols(object).forEach((prop)=>{
newResult[prop] = object[prop];
})
return newResult;
}
query(...expressions){
return new TrackingMemset(this.memset.query.apply(this.memset,expressions));
}
then(callback,errorCallback?):Promise<any>{
return this.memset.then.apply(this.memset,arguments);
}
add(value):Promise<any>{
return this.memset.add.apply(this.memset,arguments);
}
update(value):Promise<any>{
return this.memset.update.apply(this.memset,arguments);
}
delete(value):Promise<any>{
return this.memset.delete.apply(this.memset,arguments);
}
private selectTrackId(expressionsArr:Array<any>){
let expressions = [].concat(expressionsArr);
let indexOfLast = expressions.lastIndexOf(x=>Utility.instanceof(x,Select));
if(indexOfLast < 0){
return expressions; // gets all
}
expressions[indexOfLast] = new Select([].concat(expressions[indexOfLast].args,{
property:new Property(this.trackingId as any)
}));
return expressions;
}
get(...expressions){
expressions = this.selectTrackId(expressions);
return this.memset.get.apply(this.memset,expressions).then((response)=>{
if(typeof response === "number") return response;
return (Array.isArray(response) ?response:[response]).map(x=>{
let indexOfItem = (Array.isArray(this.source)?this.source:[this.source]).findIndex(a=>a[this.trackingId as any] === x[this.trackingId as any]);
if(indexOfItem < 0) return x;
let result = this.trackObject(indexOfItem,x);
return result;
});
});
}
}