Skip to content

Commit

Permalink
fix(firestore): update settings on firestore instance
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Dec 12, 2022
1 parent c73e5ed commit 089b92e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
32 changes: 20 additions & 12 deletions packages/firebase-firestore/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,17 +506,15 @@ export class Query<T extends DocumentData = DocumentData> implements IQuery<T> {
return Query.fromNative(this.native.limitToLast(limitToLast));
}

onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: QuerySnapshot) => void; }): () => void;
onSnapshot(options: SnapshotListenOptions, observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: QuerySnapshot) => void; }): () => void;
onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: QuerySnapshot) => void }): () => void;
onSnapshot(options: SnapshotListenOptions, observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: QuerySnapshot) => void }): () => void;
onSnapshot(onNext: (snapshot: QuerySnapshot) => void, onError?: (error: Error) => void, onCompletion?: () => void): () => void;
onSnapshot(options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot) => void, onError?: (error: Error) => void, onCompletion?: () => void): () => void;
onSnapshot(...args: OnSnapshotParameters<QuerySnapshot>): () => void {
const { includeMetadataChanges, ...handlers } = parseOnSnapshotArgs(args);

const listener = this.native.addSnapshotListener(
includeMetadataChanges
? com.google.firebase.firestore.MetadataChanges.INCLUDE
: com.google.firebase.firestore.MetadataChanges.EXCLUDE,
includeMetadataChanges ? com.google.firebase.firestore.MetadataChanges.INCLUDE : com.google.firebase.firestore.MetadataChanges.EXCLUDE,
new com.google.firebase.firestore.EventListener<com.google.firebase.firestore.QuerySnapshot>({
onEvent(querySnapshot, error: com.google.firebase.firestore.FirebaseFirestoreException) {
if (error) {
Expand Down Expand Up @@ -907,17 +905,15 @@ export class DocumentReference<T extends DocumentData = DocumentData> implements
});
}

onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: DocumentSnapshot<T>) => void; }): () => void;
onSnapshot(options: SnapshotListenOptions, observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: DocumentSnapshot<T>) => void; }): () => void;
onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: DocumentSnapshot<T>) => void }): () => void;
onSnapshot(options: SnapshotListenOptions, observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: DocumentSnapshot<T>) => void }): () => void;
onSnapshot(onNext: (snapshot: DocumentSnapshot<T>) => void, onError?: (error: Error) => void, onCompletion?: () => void): () => void;
onSnapshot(options: SnapshotListenOptions, onNext: (snapshot: DocumentSnapshot<T>) => void, onError?: (error: Error) => void, onCompletion?: () => void): () => void;
onSnapshot(...args: OnSnapshotParameters<DocumentSnapshot<T>>): () => void {
const { includeMetadataChanges, ...handlers } = parseOnSnapshotArgs(args);

const listener = this.native.addSnapshotListener(
includeMetadataChanges
? com.google.firebase.firestore.MetadataChanges.INCLUDE
: com.google.firebase.firestore.MetadataChanges.EXCLUDE,
includeMetadataChanges ? com.google.firebase.firestore.MetadataChanges.INCLUDE : com.google.firebase.firestore.MetadataChanges.EXCLUDE,
new com.google.firebase.firestore.EventListener<com.google.firebase.firestore.DocumentSnapshot>({
onEvent(docSnapshot, error: com.google.firebase.firestore.FirebaseFirestoreException) {
if (error) {
Expand Down Expand Up @@ -1362,26 +1358,35 @@ export class WriteBatch implements IWriteBatch {

export class Settings implements ISettings {
_builder: com.google.firebase.firestore.FirebaseFirestoreSettings.Builder;
_firestore: com.google.firebase.firestore.FirebaseFirestore;

constructor() {
this._builder = new com.google.firebase.firestore.FirebaseFirestoreSettings.Builder();
}

static fromNative(ffs: com.google.firebase.firestore.FirebaseFirestoreSettings) {
static fromNative(ffs: com.google.firebase.firestore.FirebaseFirestoreSettings, firestore = undefined) {
if (ffs instanceof com.google.firebase.firestore.FirebaseFirestoreSettings) {
const settings = new Settings();
settings._firestore = firestore ?? undefined;
settings._builder = new com.google.firebase.firestore.FirebaseFirestoreSettings.Builder(ffs);
return settings;
}
return null;
}

_updateStoreSettings() {
if (this._firestore !== undefined) {
this._firestore.setFirestoreSettings(this.native);
}
}

get cacheSizeBytes(): number {
return this.native.getCacheSizeBytes();
}

set cacheSizeBytes(value) {
this._builder.setCacheSizeBytes(value);
this._updateStoreSettings();
}

get host(): string {
Expand All @@ -1390,6 +1395,7 @@ export class Settings implements ISettings {

set host(value) {
this._builder.setHost(value);
this._updateStoreSettings();
}

ignoreUndefinedProperties: boolean;
Expand All @@ -1400,6 +1406,7 @@ export class Settings implements ISettings {

set persistence(value) {
this._builder.setPersistenceEnabled(value);
this._updateStoreSettings();
}

get ssl(): boolean {
Expand All @@ -1408,6 +1415,7 @@ export class Settings implements ISettings {

set ssl(value) {
this._builder.setSslEnabled(value);
this._updateStoreSettings();
}

toJSON() {
Expand Down Expand Up @@ -1620,7 +1628,7 @@ export class Firestore implements IFirestore {
}

get settings() {
return Settings.fromNative(this.native?.getFirestoreSettings?.());
return Settings.fromNative(this.native?.getFirestoreSettings?.(), this.native);
}

set settings(value) {
Expand Down
24 changes: 18 additions & 6 deletions packages/firebase-firestore/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ export class Query<T extends DocumentData = DocumentData> implements IQuery<T> {
return Query.fromNative(this.native.queryLimitedToLast(limitToLast));
}

onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: QuerySnapshot) => void; }): () => void;
onSnapshot(options: SnapshotListenOptions, observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: QuerySnapshot) => void; }): () => void;
onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: QuerySnapshot) => void }): () => void;
onSnapshot(options: SnapshotListenOptions, observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: QuerySnapshot) => void }): () => void;
onSnapshot(onNext: (snapshot: QuerySnapshot) => void, onError?: (error: Error) => void, onCompletion?: () => void): () => void;
onSnapshot(options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot) => void, onError?: (error: Error) => void, onCompletion?: () => void): () => void;
onSnapshot(...args: OnSnapshotParameters<QuerySnapshot>): () => void {
Expand Down Expand Up @@ -829,8 +829,8 @@ export class DocumentReference<T extends DocumentData = DocumentData> implements
});
}

onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: DocumentSnapshot<T>) => void; }): () => void;
onSnapshot(options: SnapshotListenOptions, observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: DocumentSnapshot<T>) => void; }): () => void;
onSnapshot(observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: DocumentSnapshot<T>) => void }): () => void;
onSnapshot(options: SnapshotListenOptions, observer: { complete?: () => void; error?: (error: Error) => void; next?: (snapshot: DocumentSnapshot<T>) => void }): () => void;
onSnapshot(onNext: (snapshot: DocumentSnapshot<T>) => void, onError?: (error: Error) => void, onCompletion?: () => void): () => void;
onSnapshot(options: SnapshotListenOptions, onNext: (snapshot: DocumentSnapshot<T>) => void, onError?: (error: Error) => void, onCompletion?: () => void): () => void;
onSnapshot(...args: OnSnapshotParameters<DocumentSnapshot<T>>): () => void {
Expand Down Expand Up @@ -1210,22 +1210,31 @@ export class WriteBatch implements IWriteBatch {

export class Settings implements ISettings {
_native: FIRFirestoreSettings;
_firestore: FIRFirestore;

static fromNative(ffs: FIRFirestoreSettings) {
static fromNative(ffs: FIRFirestoreSettings, firestore = undefined) {
if (ffs instanceof FIRFirestoreSettings) {
const settings = new Settings();
settings._native = ffs;
settings._firestore = firestore ?? undefined;
return settings;
}
return null;
}

_updateStoreSettings() {
if (this._firestore !== undefined) {
this._firestore.settings = this.native;
}
}

get cacheSizeBytes(): number {
return this.native.cacheSizeBytes;
}

set cacheSizeBytes(value) {
this.native.cacheSizeBytes = value;
this._updateStoreSettings();
}

get host(): string {
Expand All @@ -1234,6 +1243,7 @@ export class Settings implements ISettings {

set host(value) {
this.native.host = value;
this._updateStoreSettings();
}

ignoreUndefinedProperties: boolean;
Expand All @@ -1244,6 +1254,7 @@ export class Settings implements ISettings {

set persistence(value) {
this.native.persistenceEnabled = value;
this._updateStoreSettings();
}

get ssl(): boolean {
Expand All @@ -1252,6 +1263,7 @@ export class Settings implements ISettings {

set ssl(value) {
this.native.sslEnabled = value;
this._updateStoreSettings();
}

toJSON() {
Expand Down Expand Up @@ -1435,7 +1447,7 @@ export class Firestore implements IFirestore {
}

get settings() {
return Settings.fromNative(this.native?.settings);
return Settings.fromNative(this.native?.settings, this.native);
}

set settings(value) {
Expand Down

0 comments on commit 089b92e

Please sign in to comment.