This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
/
emulate-query.ts
175 lines (159 loc) 路 5.36 KB
/
emulate-query.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
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
165
166
167
168
169
170
171
172
173
174
175
import { Observable } from 'rxjs/Observable';
import { FirebaseListFactoryOpts } from '../angularfire2-interfaces';
export class EmulateQuery {
orderKey: string;
observableValue: any[];
observableOptions: FirebaseListFactoryOpts;
query: AfoQuery = {};
queryReady: Promise<{}[]>;
subscriptions = [];
constructor() { }
destroy() {
this.subscriptions.forEach(sub => sub.unsubscribe());
this.subscriptions = [];
}
/**
* Gets the latest value of all query items, including Observable queries.
*
* If the query item's value is an observable, then we need to listen to that and update
* the query when it updates.
* @see https://goo.gl/mNVjGN
*/
setupQuery(options: FirebaseListFactoryOpts) {
// Store passed options
this.observableOptions = options;
// Ignore empty queries
if (this.observableOptions === undefined || this.observableOptions.query === undefined) {
return;
}
// Loop through query items
this.queryReady = Promise.all(Object.keys(this.observableOptions.query).map(queryKey => {
return new Promise(resolve => {
// Checks if the query item is an observable
if (this.observableOptions.query[queryKey] instanceof Observable) {
this.subscriptions.push(
this.observableOptions.query[queryKey].subscribe(value => {
this.query[queryKey] = value;
resolve();
})
);
// Otherwise it's a regular query (e.g. not an Observable)
} else {
this.query[queryKey] = this.observableOptions.query[queryKey];
resolve();
}
});
}));
}
/**
* Emulates the query that would be applied by AngularFire2
*
* Using format similar to [angularfire2](https://goo.gl/0EPvHf)
*/
emulateQuery(value) {
this.observableValue = value;
if (this.observableOptions === undefined
|| this.observableOptions.query === undefined
|| this.observableValue === undefined) {
return new Promise(resolve => resolve(this.observableValue));
}
return this.queryReady.then(() => {
// Check orderBy
if (this.query.orderByChild) {
this.orderBy(this.query.orderByChild);
} else if (this.query.orderByKey) {
this.orderBy('$key');
} else if (this.query.orderByPriority) {
this.orderBy('$priority');
} else if (this.query.orderByValue) {
this.orderBy('$value');
}
// check equalTo
if (hasKey(this.query, 'equalTo')) {
if (hasKey(this.query.equalTo, 'value')) {
this.equalTo(this.query.equalTo.value, this.query.equalTo.key);
} else {
this.equalTo(this.query.equalTo);
}
if (hasKey(this.query, 'startAt') || hasKey(this.query, 'endAt')) {
throw new Error('Query Error: Cannot use startAt or endAt with equalTo.');
}
// apply limitTos
if (!isNil(this.query.limitToFirst)) {
this.limitToFirst(this.query.limitToFirst);
}
if (!isNil(this.query.limitToLast)) {
this.limitToLast(this.query.limitToLast);
}
return this.observableValue;
}
// check startAt
if (hasKey(this.query, 'startAt')) {
if (hasKey(this.query.startAt, 'value')) {
this.startAt(this.query.startAt.value, this.query.startAt.key);
} else {
this.startAt(this.query.startAt);
}
}
if (hasKey(this.query, 'endAt')) {
if (hasKey(this.query.endAt, 'value')) {
this.endAt(this.query.endAt.value, this.query.endAt.key);
} else {
this.endAt(this.query.endAt);
}
}
if (!isNil(this.query.limitToFirst) && this.query.limitToLast) {
throw new Error('Query Error: Cannot use limitToFirst with limitToLast.');
}
// apply limitTos
if (!isNil(this.query.limitToFirst)) {
this.limitToFirst(this.query.limitToFirst);
}
if (!isNil(this.query.limitToLast)) {
this.limitToLast(this.query.limitToLast);
}
return this.observableValue;
});
}
private endAt(value, key?) {
const orderingBy = key ? key : this.orderKey;
this.observableValue = this.observableValue.filter(item => item[orderingBy] <= value);
}
private equalTo(value, key?) {
const orderingBy = key ? key : this.orderKey;
this.observableValue = this.observableValue.filter(item => item[orderingBy] === value);
}
private limitToFirst(limit: number) {
if (limit < this.observableValue.length) {
this.observableValue = this.observableValue.slice(0, limit);
}
}
private limitToLast(limit: number) {
if (limit < this.observableValue.length) {
this.observableValue = this.observableValue.slice(-limit);
}
}
private orderBy(x) {
this.orderKey = x;
this.observableValue.sort((a, b) => {
const itemA = a[x];
const itemB = b[x];
if (itemA < itemB) { return -1; }
if (itemA > itemB) { return 1; }
return 0;
});
}
private startAt(value, key?) {
const orderingBy = key ? key : this.orderKey;
this.observableValue = this.observableValue.filter(item => item[orderingBy] >= value);
}
}
export interface AfoQuery {
[key: string]: any;
}
export function isNil(obj: any): boolean {
return obj === undefined || obj === null;
}
export function hasKey(obj: Object, key: string): boolean {
return obj && obj[key] !== undefined;
}