Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 2883060

Browse files
committed
[firestore] - settings function implementation
1 parent 12b5a2a commit 2883060

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/app/firestore/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ export namespace firestore {
3333
batch(): firebase.firestore.WriteBatch {
3434
return firebase.firestore.batch();
3535
}
36+
37+
settings(settings: firebase.firestore.Settings): void {
38+
firebase.firestore.settings(settings);
39+
}
3640
}
3741
}

src/firebase.android.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ firebase._googleSignInIdToken = null;
4949
firebase._facebookAccessToken = null;
5050

5151
let fbCallbackManager = null;
52+
let initializeArguments: any;
5253

5354
const GOOGLE_SIGNIN_INTENT_ID = 123;
5455
const REQUEST_INVITE_INTENT_ID = 48;
@@ -310,6 +311,7 @@ firebase.init = arg => {
310311

311312
const runInit = () => {
312313
arg = arg || {};
314+
initializeArguments = arg;
313315

314316
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
315317
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
@@ -2233,6 +2235,22 @@ firebase.firestore.runTransaction = (updateFunction: (transaction: firestore.Tra
22332235
};
22342236
*/
22352237

2238+
firebase.firestore.settings = (settings: firestore.Settings) => {
2239+
if (typeof (com.google.firebase.firestore) !== "undefined") {
2240+
try {
2241+
const builder = new com.google.firebase.firestore.FirebaseFirestoreSettings.Builder();
2242+
(settings.cacheSizeBytes !== undefined) && builder.setCacheSizeBytes(long(settings.cacheSizeBytes));
2243+
(settings.ssl !== undefined) && builder.setSslEnabled(settings.ssl);
2244+
(settings.host !== undefined) && builder.setHost(settings.host);
2245+
(initializeArguments.persist !== undefined) && builder.setPersistenceEnabled(initializeArguments.persist);
2246+
2247+
com.google.firebase.firestore.FirebaseFirestore.getInstance().setFirestoreSettings(builder.build());
2248+
} catch (err) {
2249+
console.log("Error: " + err );
2250+
}
2251+
}
2252+
};
2253+
22362254
firebase.firestore.collection = (collectionPath: string): firestore.CollectionReference => {
22372255
try {
22382256

src/firebase.d.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,52 @@ export namespace firestore {
691691

692692
export function GeoPoint(latitude: number, longitude: number): GeoPoint;
693693

694+
export interface Settings {
695+
/** The hostname to connect to. */
696+
host?: string;
697+
/** Whether to use SSL when connecting. */
698+
ssl?: boolean;
699+
700+
/**
701+
* Specifies whether to use `Timestamp` objects for timestamp fields in
702+
* `DocumentSnapshot`s. This is enabled by default and should not be
703+
* disabled.
704+
*
705+
* Previously, Firestore returned timestamp fields as `Date` but `Date`
706+
* only supports millisecond precision, which leads to truncation and
707+
* causes unexpected behavior when using a timestamp from a snapshot as a
708+
* part of a subsequent query.
709+
*
710+
* So now Firestore returns `Timestamp` values instead of `Date`, avoiding
711+
* this kind of problem.
712+
*
713+
* To opt into the old behavior of returning `Date` objects, you can
714+
* temporarily set `timestampsInSnapshots` to false.
715+
*
716+
* @deprecated This setting will be removed in a future release. You should
717+
* update your code to expect `Timestamp` objects and stop using the
718+
* `timestampsInSnapshots` setting.
719+
*/
720+
timestampsInSnapshots?: boolean;
721+
722+
/**
723+
* An approximate cache size threshold for the on-disk data. If the cache grows beyond this
724+
* size, Firestore will start removing data that hasn't been recently used. The size is not a
725+
* guarantee that the cache will stay below that size, only that if the cache exceeds the given
726+
* size, cleanup will be attempted.
727+
*
728+
* The default value is 40 MB. The threshold must be set to at least 1 MB, and can be set to
729+
* CACHE_SIZE_UNLIMITED to disable garbage collection.
730+
*/
731+
cacheSizeBytes?: number;
732+
}
733+
/**
734+
* Specifies custom settings to be used to configure the `Firestore`
735+
* instance. Must be set before invoking any other methods.
736+
*
737+
* @param settings The settings to use.
738+
*/
739+
export function settings(settings: Settings): void;
694740
export interface SetOptions {
695741
merge?: boolean;
696742
}

src/firebase.ios.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ firebase._configured = false;
2727

2828
const useExternalPushProvider = NSBundle.mainBundle.infoDictionary.objectForKey("UseExternalPushProvider") === true;
2929

30+
let initializeArguments: any;
3031
class DocumentSnapshot extends DocumentSnapshotBase {
3132
ios: FIRDocumentSnapshot;
3233

@@ -361,6 +362,7 @@ firebase.init = arg => {
361362
}
362363

363364
arg = arg || {};
365+
initializeArguments = arg;
364366

365367
// if deeplinks are used, then for this scheme to work the use must have added the bundle as a scheme to their plist (this is in our docs)
366368
if (FIROptions.defaultOptions() !== null) {
@@ -1886,6 +1888,21 @@ firebase.firestore.runTransaction = (updateFunction: (transaction: firestore.Tra
18861888
});
18871889
};
18881890

1891+
firebase.firestore.settings = (settings: firestore.Settings) => {
1892+
if (typeof (FIRFirestore) !== "undefined") {
1893+
try {
1894+
const fIRFirestoreSettings = FIRFirestoreSettings.new();
1895+
if (initializeArguments.persist !== undefined) fIRFirestoreSettings.persistenceEnabled = initializeArguments.persist;
1896+
if (settings.ssl !== undefined) fIRFirestoreSettings.sslEnabled = settings.ssl;
1897+
if (settings.host !== undefined) fIRFirestoreSettings.host = settings.host;
1898+
// Cannot do this because of nativescript cannot convert Number to int64_t
1899+
// fIRFirestoreSettings.cacheSizeBytes = settings.cacheSizeBytes;
1900+
FIRFirestore.firestore().settings = fIRFirestoreSettings;
1901+
} catch (err) {
1902+
console.log("Error: " + err);
1903+
}
1904+
}
1905+
};
18891906

18901907
firebase.firestore.collection = (collectionPath: string): firestore.CollectionReference => {
18911908
try {

0 commit comments

Comments
 (0)