1 | import * as i0 from '@angular/core';
|
2 | import { InjectionToken, Injectable, Inject, Optional, PLATFORM_ID, NgZone, NgModule } from '@angular/core';
|
3 | import { asyncScheduler, Observable, from, of } from 'rxjs';
|
4 | import { startWith, pairwise, map, scan, distinctUntilChanged, filter, observeOn } from 'rxjs/operators';
|
5 | import * as i1 from '@angular/fire';
|
6 | import { ɵAngularFireSchedulers, ɵkeepUnstableUntilFirstFactory, ɵfirebaseAppFactory, ɵlogAuthEmulatorError, ɵfetchInstance, FIREBASE_OPTIONS, FIREBASE_APP_NAME } from '@angular/fire';
|
7 | import { isPlatformServer } from '@angular/common';
|
8 | import firebase from 'firebase/app';
|
9 | import 'firebase/firestore';
|
10 | import * as i2 from '@angular/fire/auth';
|
11 | import { USE_EMULATOR as USE_EMULATOR$1 } from '@angular/fire/auth';
|
12 |
|
13 | function _fromRef(ref, scheduler = asyncScheduler) {
|
14 | return new Observable(subscriber => {
|
15 | let unsubscribe;
|
16 | if (scheduler != null) {
|
17 | scheduler.schedule(() => {
|
18 | unsubscribe = ref.onSnapshot({ includeMetadataChanges: true }, subscriber);
|
19 | });
|
20 | }
|
21 | else {
|
22 | unsubscribe = ref.onSnapshot({ includeMetadataChanges: true }, subscriber);
|
23 | }
|
24 | return () => {
|
25 | if (unsubscribe != null) {
|
26 | unsubscribe();
|
27 | }
|
28 | };
|
29 | });
|
30 | }
|
31 | function fromRef(ref, scheduler) {
|
32 | return _fromRef(ref, scheduler);
|
33 | }
|
34 | function fromDocRef(ref, scheduler) {
|
35 | return fromRef(ref, scheduler)
|
36 | .pipe(startWith(undefined), pairwise(), map(([priorPayload, payload]) => {
|
37 | if (!payload.exists) {
|
38 | return { payload, type: 'removed' };
|
39 | }
|
40 | if (!(priorPayload === null || priorPayload === void 0 ? void 0 : priorPayload.exists)) {
|
41 | return { payload, type: 'added' };
|
42 | }
|
43 | return { payload, type: 'modified' };
|
44 | }));
|
45 | }
|
46 | function fromCollectionRef(ref, scheduler) {
|
47 | return fromRef(ref, scheduler).pipe(map(payload => ({ payload, type: 'query' })));
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function docChanges(query, scheduler) {
|
55 | return fromCollectionRef(query, scheduler)
|
56 | .pipe(startWith(undefined), pairwise(), map(([priorAction, action]) => {
|
57 | const docChanges = action.payload.docChanges();
|
58 | const actions = docChanges.map(change => ({ type: change.type, payload: change }));
|
59 |
|
60 | if (priorAction && JSON.stringify(priorAction.payload.metadata) !== JSON.stringify(action.payload.metadata)) {
|
61 |
|
62 | action.payload.docs.forEach((currentDoc, currentIndex) => {
|
63 | const docChange = docChanges.find(d => d.doc.ref.isEqual(currentDoc.ref));
|
64 | const priorDoc = priorAction === null || priorAction === void 0 ? void 0 : priorAction.payload.docs.find(d => d.ref.isEqual(currentDoc.ref));
|
65 | if (docChange && JSON.stringify(docChange.doc.metadata) === JSON.stringify(currentDoc.metadata) ||
|
66 | !docChange && priorDoc && JSON.stringify(priorDoc.metadata) === JSON.stringify(currentDoc.metadata)) {
|
67 |
|
68 | }
|
69 | else {
|
70 |
|
71 | actions.push({
|
72 | type: 'modified',
|
73 | payload: {
|
74 | oldIndex: currentIndex,
|
75 | newIndex: currentIndex,
|
76 | type: 'modified',
|
77 | doc: currentDoc
|
78 | }
|
79 | });
|
80 | }
|
81 | });
|
82 | }
|
83 | return actions;
|
84 | }));
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 | function sortedChanges(query, events, scheduler) {
|
90 | return docChanges(query, scheduler)
|
91 | .pipe(scan((current, changes) => combineChanges(current, changes.map(it => it.payload), events), []), distinctUntilChanged(),
|
92 | map(changes => changes.map(c => ({ type: c.type, payload: c }))));
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function combineChanges(current, changes, events) {
|
99 | changes.forEach(change => {
|
100 |
|
101 | if (events.indexOf(change.type) > -1) {
|
102 | current = combineChange(current, change);
|
103 | }
|
104 | });
|
105 | return current;
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | function sliceAndSplice(original, start, deleteCount, ...args) {
|
112 | const returnArray = original.slice();
|
113 | returnArray.splice(start, deleteCount, ...args);
|
114 | return returnArray;
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function combineChange(combined, change) {
|
122 | switch (change.type) {
|
123 | case 'added':
|
124 | if (combined[change.newIndex] && combined[change.newIndex].doc.ref.isEqual(change.doc.ref)) {
|
125 |
|
126 | }
|
127 | else {
|
128 | return sliceAndSplice(combined, change.newIndex, 0, change);
|
129 | }
|
130 | break;
|
131 | case 'modified':
|
132 | if (combined[change.oldIndex] == null || combined[change.oldIndex].doc.ref.isEqual(change.doc.ref)) {
|
133 |
|
134 |
|
135 | if (change.oldIndex !== change.newIndex) {
|
136 | const copiedArray = combined.slice();
|
137 | copiedArray.splice(change.oldIndex, 1);
|
138 | copiedArray.splice(change.newIndex, 0, change);
|
139 | return copiedArray;
|
140 | }
|
141 | else {
|
142 | return sliceAndSplice(combined, change.newIndex, 1, change);
|
143 | }
|
144 | }
|
145 | break;
|
146 | case 'removed':
|
147 | if (combined[change.oldIndex] && combined[change.oldIndex].doc.ref.isEqual(change.doc.ref)) {
|
148 | return sliceAndSplice(combined, change.oldIndex, 1);
|
149 | }
|
150 | break;
|
151 | }
|
152 | return combined;
|
153 | }
|
154 |
|
155 | function validateEventsArray(events) {
|
156 | if (!events || events.length === 0) {
|
157 | events = ['added', 'removed', 'modified'];
|
158 | }
|
159 | return events;
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | class AngularFirestoreCollection {
|
185 | |
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | constructor(ref, query, afs) {
|
195 | this.ref = ref;
|
196 | this.query = query;
|
197 | this.afs = afs;
|
198 | }
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 | stateChanges(events) {
|
205 | let source = docChanges(this.query, this.afs.schedulers.outsideAngular);
|
206 | if (events && events.length > 0) {
|
207 | source = source.pipe(map(actions => actions.filter(change => events.indexOf(change.type) > -1)));
|
208 | }
|
209 | return source.pipe(
|
210 |
|
211 |
|
212 | startWith(undefined), pairwise(), filter(([prior, current]) => current.length > 0 || !prior), map(([prior, current]) => current), this.afs.keepUnstableUntilFirst);
|
213 | }
|
214 | |
215 |
|
216 |
|
217 |
|
218 | auditTrail(events) {
|
219 | return this.stateChanges(events).pipe(scan((current, action) => [...current, ...action], []));
|
220 | }
|
221 | |
222 |
|
223 |
|
224 |
|
225 | snapshotChanges(events) {
|
226 | const validatedEvents = validateEventsArray(events);
|
227 | const scheduledSortedChanges$ = sortedChanges(this.query, validatedEvents, this.afs.schedulers.outsideAngular);
|
228 | return scheduledSortedChanges$.pipe(this.afs.keepUnstableUntilFirst);
|
229 | }
|
230 | valueChanges(options = {}) {
|
231 | return fromCollectionRef(this.query, this.afs.schedulers.outsideAngular)
|
232 | .pipe(map(actions => actions.payload.docs.map(a => {
|
233 | if (options.idField) {
|
234 | return Object.assign(Object.assign({}, a.data()), { [options.idField]: a.id });
|
235 | }
|
236 | else {
|
237 | return a.data();
|
238 | }
|
239 | })), this.afs.keepUnstableUntilFirst);
|
240 | }
|
241 | |
242 |
|
243 |
|
244 | get(options) {
|
245 | return from(this.query.get(options)).pipe(observeOn(this.afs.schedulers.insideAngular));
|
246 | }
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | add(data) {
|
255 | return this.ref.add(data);
|
256 | }
|
257 | |
258 |
|
259 |
|
260 | doc(path) {
|
261 |
|
262 | return new AngularFirestoreDocument(this.ref.doc(path), this.afs);
|
263 | }
|
264 | }
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | class AngularFirestoreDocument {
|
289 | |
290 |
|
291 |
|
292 |
|
293 | constructor(ref, afs) {
|
294 | this.ref = ref;
|
295 | this.afs = afs;
|
296 | }
|
297 | |
298 |
|
299 |
|
300 | set(data, options) {
|
301 | return this.ref.set(data, options);
|
302 | }
|
303 | |
304 |
|
305 |
|
306 | update(data) {
|
307 | return this.ref.update(data);
|
308 | }
|
309 | |
310 |
|
311 |
|
312 | delete() {
|
313 | return this.ref.delete();
|
314 | }
|
315 | |
316 |
|
317 |
|
318 |
|
319 | collection(path, queryFn) {
|
320 | const collectionRef = this.ref.collection(path);
|
321 | const { ref, query } = associateQuery(collectionRef, queryFn);
|
322 | return new AngularFirestoreCollection(ref, query, this.afs);
|
323 | }
|
324 | |
325 |
|
326 |
|
327 | snapshotChanges() {
|
328 | const scheduledFromDocRef$ = fromDocRef(this.ref, this.afs.schedulers.outsideAngular);
|
329 | return scheduledFromDocRef$.pipe(this.afs.keepUnstableUntilFirst);
|
330 | }
|
331 | valueChanges(options = {}) {
|
332 | return this.snapshotChanges().pipe(map(({ payload }) => options.idField ? Object.assign(Object.assign({}, payload.data()), { [options.idField]: payload.id }) : payload.data()));
|
333 | }
|
334 | |
335 |
|
336 |
|
337 | get(options) {
|
338 | return from(this.ref.get(options)).pipe(observeOn(this.afs.schedulers.insideAngular));
|
339 | }
|
340 | }
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 | class AngularFirestoreCollectionGroup {
|
361 | |
362 |
|
363 |
|
364 |
|
365 | constructor(query, afs) {
|
366 | this.query = query;
|
367 | this.afs = afs;
|
368 | }
|
369 | |
370 |
|
371 |
|
372 |
|
373 |
|
374 | stateChanges(events) {
|
375 | if (!events || events.length === 0) {
|
376 | return docChanges(this.query, this.afs.schedulers.outsideAngular).pipe(this.afs.keepUnstableUntilFirst);
|
377 | }
|
378 | return docChanges(this.query, this.afs.schedulers.outsideAngular)
|
379 | .pipe(map(actions => actions.filter(change => events.indexOf(change.type) > -1)), filter(changes => changes.length > 0), this.afs.keepUnstableUntilFirst);
|
380 | }
|
381 | |
382 |
|
383 |
|
384 |
|
385 | auditTrail(events) {
|
386 | return this.stateChanges(events).pipe(scan((current, action) => [...current, ...action], []));
|
387 | }
|
388 | |
389 |
|
390 |
|
391 |
|
392 | snapshotChanges(events) {
|
393 | const validatedEvents = validateEventsArray(events);
|
394 | const scheduledSortedChanges$ = sortedChanges(this.query, validatedEvents, this.afs.schedulers.outsideAngular);
|
395 | return scheduledSortedChanges$.pipe(this.afs.keepUnstableUntilFirst);
|
396 | }
|
397 | valueChanges(options = {}) {
|
398 | const fromCollectionRefScheduled$ = fromCollectionRef(this.query, this.afs.schedulers.outsideAngular);
|
399 | return fromCollectionRefScheduled$
|
400 | .pipe(map(actions => actions.payload.docs.map(a => {
|
401 | if (options.idField) {
|
402 | return Object.assign({ [options.idField]: a.id }, a.data());
|
403 | }
|
404 | else {
|
405 | return a.data();
|
406 | }
|
407 | })), this.afs.keepUnstableUntilFirst);
|
408 | }
|
409 | |
410 |
|
411 |
|
412 | get(options) {
|
413 | return from(this.query.get(options)).pipe(observeOn(this.afs.schedulers.insideAngular));
|
414 | }
|
415 | }
|
416 |
|
417 |
|
418 |
|
419 |
|
420 | const ENABLE_PERSISTENCE = new InjectionToken('angularfire2.enableFirestorePersistence');
|
421 | const PERSISTENCE_SETTINGS = new InjectionToken('angularfire2.firestore.persistenceSettings');
|
422 | const SETTINGS = new InjectionToken('angularfire2.firestore.settings');
|
423 | const USE_EMULATOR = new InjectionToken('angularfire2.firestore.use-emulator');
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 | function associateQuery(collectionRef, queryFn = ref => ref) {
|
437 | const query = queryFn(collectionRef);
|
438 | const ref = collectionRef;
|
439 | return { query, ref };
|
440 | }
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | class AngularFirestore {
|
497 | |
498 |
|
499 |
|
500 |
|
501 |
|
502 | constructor(options, nameOrConfig, shouldEnablePersistence, settings,
|
503 | // tslint:disable-next-line:ban-types
|
504 | platformId, zone, persistenceSettings, _useEmulator, useAuthEmulator) {
|
505 | this.schedulers = new ɵAngularFireSchedulers(zone);
|
506 | this.keepUnstableUntilFirst = ɵkeepUnstableUntilFirstFactory(this.schedulers);
|
507 | const app = ɵfirebaseAppFactory(options, zone, nameOrConfig);
|
508 | if (!firebase.auth && useAuthEmulator) {
|
509 | ɵlogAuthEmulatorError();
|
510 | }
|
511 | const useEmulator = _useEmulator;
|
512 | [this.firestore, this.persistenceEnabled$] = ɵfetchInstance(`${app.name}.firestore`, 'AngularFirestore', app, () => {
|
513 | const firestore = zone.runOutsideAngular(() => app.firestore());
|
514 | if (settings) {
|
515 | firestore.settings(settings);
|
516 | }
|
517 | if (useEmulator) {
|
518 | firestore.useEmulator(...useEmulator);
|
519 | }
|
520 | if (shouldEnablePersistence && !isPlatformServer(platformId)) {
|
521 |
|
522 |
|
523 | const enablePersistence = () => {
|
524 | try {
|
525 | return from(firestore.enablePersistence(persistenceSettings || undefined).then(() => true, () => false));
|
526 | }
|
527 | catch (e) {
|
528 | if (typeof console !== 'undefined') {
|
529 | console.warn(e);
|
530 | }
|
531 | return of(false);
|
532 | }
|
533 | };
|
534 | return [firestore, zone.runOutsideAngular(enablePersistence)];
|
535 | }
|
536 | else {
|
537 | return [firestore, of(false)];
|
538 | }
|
539 | }, [settings, useEmulator, shouldEnablePersistence]);
|
540 | }
|
541 | collection(pathOrRef, queryFn) {
|
542 | let collectionRef;
|
543 | if (typeof pathOrRef === 'string') {
|
544 | collectionRef = this.firestore.collection(pathOrRef);
|
545 | }
|
546 | else {
|
547 | collectionRef = pathOrRef;
|
548 | }
|
549 | const { ref, query } = associateQuery(collectionRef, queryFn);
|
550 | const refInZone = this.schedulers.ngZone.run(() => ref);
|
551 | return new AngularFirestoreCollection(refInZone, query, this);
|
552 | }
|
553 | |
554 |
|
555 |
|
556 |
|
557 |
|
558 | collectionGroup(collectionId, queryGroupFn) {
|
559 | const queryFn = queryGroupFn || (ref => ref);
|
560 | const collectionGroup = this.firestore.collectionGroup(collectionId);
|
561 | return new AngularFirestoreCollectionGroup(queryFn(collectionGroup), this);
|
562 | }
|
563 | doc(pathOrRef) {
|
564 | let ref;
|
565 | if (typeof pathOrRef === 'string') {
|
566 | ref = this.firestore.doc(pathOrRef);
|
567 | }
|
568 | else {
|
569 | ref = pathOrRef;
|
570 | }
|
571 | const refInZone = this.schedulers.ngZone.run(() => ref);
|
572 | return new AngularFirestoreDocument(refInZone, this);
|
573 | }
|
574 | |
575 |
|
576 |
|
577 | createId() {
|
578 | return this.firestore.collection('_').doc().id;
|
579 | }
|
580 | }
|
581 | AngularFirestore.ɵprov = i0.ɵɵdefineInjectable({ factory: function AngularFirestore_Factory() { return new AngularFirestore(i0.ɵɵinject(i1.FIREBASE_OPTIONS), i0.ɵɵinject(i1.FIREBASE_APP_NAME, 8), i0.ɵɵinject(ENABLE_PERSISTENCE, 8), i0.ɵɵinject(SETTINGS, 8), i0.ɵɵinject(i0.PLATFORM_ID), i0.ɵɵinject(i0.NgZone), i0.ɵɵinject(PERSISTENCE_SETTINGS, 8), i0.ɵɵinject(USE_EMULATOR, 8), i0.ɵɵinject(i2.USE_EMULATOR, 8)); }, token: AngularFirestore, providedIn: "any" });
|
582 | AngularFirestore.decorators = [
|
583 | { type: Injectable, args: [{
|
584 | providedIn: 'any'
|
585 | },] }
|
586 | ];
|
587 |
|
588 | AngularFirestore.ctorParameters = () => [
|
589 | { type: undefined, decorators: [{ type: Inject, args: [FIREBASE_OPTIONS,] }] },
|
590 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [FIREBASE_APP_NAME,] }] },
|
591 | { type: Boolean, decorators: [{ type: Optional }, { type: Inject, args: [ENABLE_PERSISTENCE,] }] },
|
592 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [SETTINGS,] }] },
|
593 | { type: Object, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] },
|
594 | { type: NgZone },
|
595 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [PERSISTENCE_SETTINGS,] }] },
|
596 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [USE_EMULATOR,] }] },
|
597 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [USE_EMULATOR$1,] }] }
|
598 | ];
|
599 |
|
600 | class AngularFirestoreModule {
|
601 | |
602 |
|
603 |
|
604 | static enablePersistence(persistenceSettings) {
|
605 | return {
|
606 | ngModule: AngularFirestoreModule,
|
607 | providers: [
|
608 | { provide: ENABLE_PERSISTENCE, useValue: true },
|
609 | { provide: PERSISTENCE_SETTINGS, useValue: persistenceSettings },
|
610 | ]
|
611 | };
|
612 | }
|
613 | }
|
614 | AngularFirestoreModule.decorators = [
|
615 | { type: NgModule, args: [{
|
616 | providers: [AngularFirestore]
|
617 | },] }
|
618 | ];
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 | export { AngularFirestore, AngularFirestoreCollection, AngularFirestoreCollectionGroup, AngularFirestoreDocument, AngularFirestoreModule, ENABLE_PERSISTENCE, PERSISTENCE_SETTINGS, SETTINGS, USE_EMULATOR, associateQuery, combineChange, combineChanges, docChanges, fromCollectionRef, fromDocRef, fromRef, sortedChanges, validateEventsArray };
|
625 |
|