-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorient-sync.ts
133 lines (109 loc) · 3.38 KB
/
orient-sync.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as _ from 'lodash';
import { Node } from 'ancient-mixins/lib/node';
class OrientSync extends Node {
constructor(
public db,
public from: string,
public where: string,
) {
super();
this.outerUpdate = (d) => {
const rid = `#${d.cluster}:${d.position}`;
this.removed(rid);
this.resubscribe();
}
this.innerInsert = (d) => {
const rid = `#${d.cluster}:${d.position}`;
this.added(rid);
this.resubscribe();
}
this.innerUpdate = (d) => {
const rid = `#${d.cluster}:${d.position}`;
if (!_.includes(this.rids, rid)) {
this.added(rid);
this.resubscribe();
} else {
this.changed(rid);
}
}
this.innerDelete = (d) => {
const rid = `#${d.cluster}:${d.position}`;
this.removed(rid);
this.resubscribe();
}
}
rids: string[] = [];
added(rid, callback?) {
this.rids.push(rid);
this.db.query(`select from ${this.from} where @rid=${rid}`)
.then((record) => {
this.emit('added', { rid, record: record[0], subscription: this });
if (callback) callback(rid, record);
});
}
changed(rid, callback?) {
this.db.query(`select from ${this.from} where @rid=${rid}`)
.then((record) => {
this.emit('changed', { rid, record: record[0], subscription: this });
if (callback) callback(rid, record);
});
}
removed(rid, callback?) {
this.db.query(`select from ${this.from} where @rid=${rid}`)
.then((record) => {
this.emit('removed', { rid, record: record[0], subscription: this });
if (callback) callback(rid, record);
});
_.remove(this.rids, r => r == rid);
}
reselect(callback) {
this.unsubscribe();
this.select((records) => {
this.emit('reselected', { records, subscription: this });
const rids = _.map(records, d => d['@rid'].toString());
const newRids = _.difference(rids, this.rids);
const oldRids = _.difference(this.rids, rids);
_.each(newRids, rid => this.added(rid));
_.each(oldRids, rid => this.removed(rid));
this.resubscribe();
if (callback) callback(records);
});
return this;
}
select(callback) {
this.db.query(`select from ${this.from} where ${this.where}`)
.then((records) => {
this.emit('selected', { records, subscription: this });
if (callback) callback(records);
});
return this;
}
resubscribe() {
this.unsubscribe();
const outer = `live select from ${this.from} where @rid in [${this.rids.join(',')}] and not (${this.where})`;
const inner = `live select from ${this.from} where ${this.where}`;
this.emit('resubscribe', { outer, inner, subscription: this });
this.outer = this.db.liveQuery(outer)
.on('live-update', this.outerUpdate)
this.inner = this.db.liveQuery(inner)
.on('live-insert', this.innerInsert)
.on('live-update', this.innerUpdate)
.on('live-delete', this.innerDelete)
return this;
}
unsubscribe() {
this.emit('unsubscribe', { subscription: this });
if (this.outer) {
this.outer.removeListener('live-update', this.outerUpdate);
}
if (this.inner) {
this.inner.removeListener('live-insert', this.innerInsert);
this.inner.removeListener('live-update', this.innerUpdate);
this.inner.removeListener('live-delete', this.innerDelete);
}
return this;
}
}
export {
OrientSync,
}