-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(afs): fixing the metadata in snapshotChanges and more (#2670)
* metadata updates weren't getting to the `snapshotChanges` on collections due to `docChanges()` not including metadata in the changes, even when you ask for it * pull `spliceAndSlice` from rxfire and use it in combination with `distinctUntilChanged()` to reduce unneeded change detection cycles
- Loading branch information
1 parent
2dddefd
commit d5dbe99
Showing
9 changed files
with
166 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
<p>protected-lazy works!</p> | ||
|
||
<ul> | ||
<li *ngFor="let item of snapshot | async"> | ||
<pre>data(): {{ item.payload.doc.data() | json }}, | ||
id: {{ item.payload.doc.id }}, | ||
metadata: {{ item.payload.doc.metadata | json }}</pre> | ||
</li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<ul> | ||
<li *ngFor="let animal of animals | async"> | ||
<span>{{ animal.name }}</span> | ||
<button (click)="upboat(animal.id)">👍</button> | ||
<span>{{ animal.upboats }}</span> | ||
<button (click)="downboat(animal.id)">👎</button> | ||
<span *ngIf="animal.hasPendingWrites">🕒</span> | ||
</li> | ||
</ul> | ||
|
||
<button (click)="newAnimal()">New animal</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { UpboatsComponent } from './upboats.component'; | ||
|
||
describe('UpboatsComponent', () => { | ||
let component: UpboatsComponent; | ||
let fixture: ComponentFixture<UpboatsComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [ UpboatsComponent ] | ||
}) | ||
.compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(UpboatsComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
import { map, startWith, tap } from 'rxjs/operators'; | ||
import { AngularFirestoreOffline } from '../firestore-offline/firestore-offline.module'; | ||
import firebase from 'firebase/app'; | ||
import { makeStateKey, TransferState } from '@angular/platform-browser'; | ||
import { trace } from '@angular/fire/performance'; | ||
|
||
type Animal = { name: string, upboats: number, id: string, hasPendingWrites: boolean }; | ||
|
||
@Component({ | ||
selector: 'app-upboats', | ||
templateUrl: './upboats.component.html', | ||
styleUrls: ['./upboats.component.css'] | ||
}) | ||
export class UpboatsComponent implements OnInit { | ||
|
||
public animals: Observable<Animal[]>; | ||
|
||
constructor(private firestore: AngularFirestoreOffline, state: TransferState) { | ||
const collection = firestore.collection<Animal>('animals', ref => | ||
ref.orderBy('upboats', 'desc').orderBy('updatedAt', 'desc') | ||
); | ||
const key = makeStateKey(collection.ref.path); | ||
const existing = state.get(key, undefined); | ||
this.animals = collection.snapshotChanges().pipe( | ||
trace('animals'), | ||
map(it => it.map(change => ({ | ||
...change.payload.doc.data(), | ||
id: change.payload.doc.id, | ||
hasPendingWrites: change.payload.doc.metadata.hasPendingWrites | ||
}))), | ||
existing ? startWith(existing) : tap(it => state.set(key, it)) | ||
); | ||
} | ||
|
||
ngOnInit(): void { | ||
} | ||
|
||
upboat(id: string) { | ||
// TODO add rule | ||
this.firestore.doc(`animals/${id}`).update({ | ||
upboats: firebase.firestore.FieldValue.increment(1), | ||
updatedAt: firebase.firestore.FieldValue.serverTimestamp(), | ||
}); | ||
} | ||
|
||
downboat(id: string) { | ||
// TODO add rule | ||
this.firestore.doc(`animals/${id}`).update({ | ||
upboats: firebase.firestore.FieldValue.increment(-1), | ||
updatedAt: firebase.firestore.FieldValue.serverTimestamp(), | ||
}); | ||
} | ||
|
||
newAnimal() { | ||
// TODO add rule | ||
this.firestore.collection('animals').add({ | ||
name: prompt('Can haz name?'), | ||
upboats: 1, | ||
updatedAt: firebase.firestore.FieldValue.serverTimestamp(), | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters