-
Notifications
You must be signed in to change notification settings - Fork 6
/
collection.js
164 lines (140 loc) · 4.02 KB
/
collection.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import Promise from 'bluebird';
import Firebase from 'firebase';
import {Container} from 'aurelia-dependency-injection';
import {Configuration} from './configuration';
export class ReactiveCollection {
_query = null;
_valueMap = new Map();
items = [];
constructor(path: Array<string>) {
if (!Container || !Container.instance) throw Error('Container has not been made global');
let config = Container.instance.get(Configuration);
if (!config) throw Error('Configuration has not been set');
this._query = new Firebase(ReactiveCollection._getChildLocation(
config.getFirebaseUrl(),
path));
this._listenToQuery(this._query);
}
add(item:any) : Promise {
return new Promise((resolve, reject) => {
let query = this._query.ref().push();
query.set(item, (error) => {
if (error) {
reject(error);
return;
}
resolve(item);
});
});
}
remove(item: any): Promise {
if (item === null || item.__firebaseKey__ === null) {
return Promise.reject({message: 'Unknown item'});
}
return this.removeByKey(item.__firebaseKey__);
}
getByKey(key): any {
return this._valueMap.get(key);
}
removeByKey(key) {
return new Promise((resolve, reject) => {
this._query.ref().child(key).remove((error) =>{
if (error) {
reject(error);
return;
}
resolve(key);
});
});
}
clear() {
//this._stopListeningToQuery(this._query);
return new Promise((resolve, reject) => {
let query = this._query.ref();
query.remove((error) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
}
_listenToQuery(query) {
query.on('child_added', (snapshot, previousKey) => {
this._onItemAdded(snapshot, previousKey);
});
query.on('child_removed', (snapshot) => {
this._onItemRemoved(snapshot);
});
query.on('child_changed', (snapshot, previousKey) => {
this._onItemChanded(snapshot, previousKey);
});
query.on('child_moved', (snapshot, previousKey) => {
this._onItemMoved(snapshot, previousKey);
});
}
_stopListeningToQuery(query) {
query.off();
}
_onItemAdded(snapshot, previousKey) {
let value = this._valueFromSnapshot(snapshot);
let index = previousKey !== null ?
this.items.indexOf(this._valueMap.get(previousKey)) + 1 : 0;
this._valueMap.set(value.__firebaseKey__, value);
this.items.splice(index, 0, value);
}
_onItemRemoved(oldSnapshot) {
let key = oldSnapshot.key();
let value = this._valueMap.get(key);
if (!value) {
return;
}
let index = this.items.indexOf(value);
this._valueMap.delete(key);
if (index !== -1) {
this.items.splice(index, 1);
}
}
_onItemChanged(snapshot, previousKey) {
let value = this._valueFromSnapshot(snapshot);
let oldValue = this._valueMap.get(value.__firebaseKey__);
if (!oldValue) {
return;
}
this._valueMap.delete(oldValue.__firebaseKey__);
this._valueMap.set(value.__firebaseKey__, value);
this.items.splice(this.items.indexOf(oldValue), 1, value);
}
_onItemMoved(snapshot, previousKey) {
let key = snapshot.key();
let value = this._valueMap.get(key);
if (!value) {
return;
}
let previousValue = this._valueMap.get(previousKey);
let newIndex = previousValue !== null ? this.items.indexOf(previousValue) + 1 : 0;
this.items.splice(this.items.indexOf(value), 1);
this.items.splice(newIndex, 0, value);
}
_valueFromSnapshot(snapshot) {
let value = snapshot.val();
if (!(value instanceof Object)) {
value = {
value: value,
__firebasePrimitive__: true
};
}
value.__firebaseKey__ = snapshot.key();
return value;
}
static _getChildLocation(root: string, path: Array<string>) {
if (!path) {
return root;
}
if (!root.endsWith('/')) {
root = root + '/';
}
return root + (Array.isArray(path) ? path.join('/') : path);
}
}