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
/
Copy pathchangeset.ts
93 lines (77 loc) · 2.74 KB
/
changeset.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
86
87
88
89
90
91
92
93
import { Emitter } from './core';
import { DataSet, IDataSet } from './dataset';
export class ChangeSet<T> extends DataSet<T>{
private onAdding = new Emitter('sync');
private onAdded = new Emitter('async');
private onDeleting = new Emitter('sync');
private onDeleted = new Emitter('async');
private onUpdating = new Emitter('sync');
private onUpdated = new Emitter('async');
constructor(private source:IDataSet<any>){
super(source.getExpressions())
}
onAdd(callback:(value:any)=>boolean):ChangeSet<T>{
this.onAdding.hook(callback);
return this;
}
whenAdded(callback:(value:any)=>any):ChangeSet<T>{
this.onAdded.hook(callback);
return this;
}
whenDeleted(callback:(value:any)=>any):ChangeSet<T>{
this.onDeleted.hook(callback);
return this;
}
whenUpdated(callback:(value:any)=>any):ChangeSet<T>{
this.onUpdated.hook(callback);
return this;
}
onDelete(callback:(value:any)=>boolean):ChangeSet<T>{
this.onDeleting.hook(callback);
return this;
}
onUpdate(callback:(value:any) =>boolean):ChangeSet<T>{
this.onUpdating.hook(callback);
return this;
}
getEmitValue(obj){
return Object.assign({
source:this.source,
changeset:this,
},obj);
}
getInterrupted(message){
return new ChangeSetinterruptedArgs(this,message);
}
query(...expressions:Array<any>):ChangeSet<T>{
return new ChangeSet(this.source.query.apply(this.source,arguments));
}
add(value):Promise<T>{
if(this.onAdding.emit(this.getEmitValue({value})) == false) return Promise.reject(this.getInterrupted('adding is interrupted'));
return this.source.add(value).then((response)=>{
this.onAdded.emit(this.getEmitValue({value,response}));
return response;
});
}
update(value):Promise<T>{
if(this.onUpdating.emit(this.getEmitValue({value})) == false) return Promise.reject(this.getInterrupted('updating is interrupted'));
return this.source.update(value).then((response)=>{
this.onUpdated.emit(this.getEmitValue({value,response}));
return response;
});
}
delete(value):Promise<T>{
if(this.onDeleting.emit(this.getEmitValue({value})) == false) return Promise.reject(this.getInterrupted('deleting is interrupted'));
return this.source.delete(value).then((response)=>{
this.onDeleted.emit(this.getEmitValue({value,response}));
return response;
})
}
get(){
return this.source.get.apply(this.source,arguments);
}
}
export class ChangeSetinterruptedArgs{
constructor(public changeset,public message){
}
}