Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added cb for meta data #181

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { setLoading, syncStoreFromDocAction, resetStore } from '../utils/sync-fr
import { getStoreName } from '../utils/store-options';
import { Observable } from 'rxjs';
import { SyncOptions } from '../utils/types';
import { filter } from 'rxjs/operators';

/** @deprecated Use CollectionService instead */
export abstract class CollectionGroupService<S extends EntityState> {
Expand Down Expand Up @@ -67,9 +68,9 @@ export abstract class CollectionGroupService<S extends EntityState> {
}

return this.db.collectionGroup(this.collectionId, query).stateChanges().pipe(
filter(data => storeOptions?.metadata ? storeOptions.metadata(data) : true),
withTransaction(actions => syncStoreFromDocAction(storeName, actions, this.idKey, this.resetOnUpdate,
this.mergeRef,
(entity) => this.formatFromFirestore(entity)))
this.mergeRef, (entity) => this.formatFromFirestore(entity)))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '../utils/sync-from-action';
import { WriteOptions, SyncOptions, PathParams, UpdateCallback, AtomicWrite } from '../utils/types';
import { Observable, isObservable, of, combineLatest } from 'rxjs';
import { tap, map, switchMap } from 'rxjs/operators';
import { tap, map, switchMap, filter } from 'rxjs/operators';
import { getStoreName } from '../utils/store-options';
import { pathWithParams } from '../utils/path-with-params';
import { hasChildGetter } from '../utils/has-path-getter';
Expand Down Expand Up @@ -206,6 +206,7 @@ export class CollectionService<S extends EntityState<EntityType, string>, Entity
}
// Start Listening
return this.db.collection<EntityType>(path, queryFn).stateChanges().pipe(
filter(data => syncOptions.metadata ? syncOptions.metadata(data) : true),
withTransaction(actions =>
syncStoreFromDocAction(storeName, actions, this.idKey, this.resetOnUpdate, this.mergeRef,
(entity) => this.formatFromFirestore(entity)))
Expand Down Expand Up @@ -272,6 +273,7 @@ export class CollectionService<S extends EntityState<EntityType, string>, Entity

const collectionId = path.split('/').pop();
return this.db.collectionGroup<EntityType>(collectionId, query).stateChanges().pipe(
filter(data => syncOptions?.metadata ? syncOptions.metadata(data) : true),
withTransaction(actions =>
syncStoreFromDocAction(storeName, actions, this.idKey, this.resetOnUpdate,
this.mergeRef, (entity) => this.formatFromFirestore(entity)))
Expand Down
1 change: 1 addition & 0 deletions projects/akita-ng-fire/src/lib/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface SyncOptions extends PathParams {
storeName: string;
loading: boolean;
reset: boolean;
metadata: (meta: DocumentChangeAction<unknown>[]) => boolean
}

/** Function used to update an entity within a transaction */
Expand Down
4 changes: 3 additions & 1 deletion src/app/collection/movie.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export class MovieListGuard extends CollectionGuard<MovieState> {

// Sync to collection. If empty redirect to to 'movies/create'
sync() {
return this.service.syncCollection('movies/').pipe(
return this.service.syncCollection('movies', {metadata: (metas) => {
return metas.every(meta => !meta.payload.doc.metadata.hasPendingWrites)
}}).pipe(
redirectIfEmpty('movies/create')
);
}
Expand Down