Repository navigation
Expand file tree
/
Copy pathcreate-reference.ts
More file actions
47 lines (46 loc) · 1.95 KB
/
Copy pathcreate-reference.ts
File metadata and controls
47 lines (46 loc) · 1.95 KB
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
import { AngularFireList, ChildEvent, DatabaseQuery } from '../interfaces';
import { snapshotChanges } from './snapshot-changes';
import { stateChanges } from './state-changes';
import { auditTrail } from './audit-trail';
import { createDataOperationMethod } from './data-operation';
import { createRemoveMethod } from './remove';
import { AngularFireDatabase } from '../database';
import { map } from 'rxjs/operators';
export function createListReference<T= any>(query: DatabaseQuery, afDatabase: AngularFireDatabase): AngularFireList<T> {
const outsideAngularScheduler = afDatabase.schedulers.outsideAngular;
const refInZone = afDatabase.schedulers.ngZone.run(() => query.ref);
return {
query,
update: createDataOperationMethod<Partial<T>>(refInZone, 'update'),
set: createDataOperationMethod<T>(refInZone, 'set'),
push: (data: T) => refInZone.push(data),
remove: createRemoveMethod(refInZone),
snapshotChanges(events?: ChildEvent[]) {
return snapshotChanges<T>(query, events, outsideAngularScheduler).pipe(afDatabase.keepUnstableUntilFirst);
},
stateChanges(events?: ChildEvent[]) {
return stateChanges<T>(query, events, outsideAngularScheduler).pipe(afDatabase.keepUnstableUntilFirst);
},
auditTrail(events?: ChildEvent[]) {
return auditTrail<T>(query, events, outsideAngularScheduler).pipe(afDatabase.keepUnstableUntilFirst);
},
valueChanges<K extends string>(events?: ChildEvent[], options?: {idField?: K}) {
const snapshotChanges$ = snapshotChanges<T>(query, events, outsideAngularScheduler);
return snapshotChanges$.pipe(
map(actions => actions.map(a => {
if (options && options.idField) {
return {
...a.payload.val() as T,
...{
[options.idField]: a.key
}
};
} else {
return a.payload.val() as T;
}
})),
afDatabase.keepUnstableUntilFirst
);
}
};
}