Skip to content

Commit

Permalink
feat(firestore, web): expose webExperimentalForceLongPolling, `webE…
Browse files Browse the repository at this point in the history
…xperimentalAutoDetectLongPolling` and `timeoutSeconds` on web (#13201)

* fix(firestore): expose  on FireStoreSettings

* fix(firestore): expose  on FireStoreSettings

* fix(firestore): change experimentalForceLongPolling to webExperimentalForceLongPolling since it applies to web only.

* feat(firestore, web): expose (webExperimentalAutoDetectLongPolling, webExperimentalForceLongPolling, timeoutSeconds)

* correct variable name

* abstract timeoutDuration within an object

* remove description on webExperimentalLongPollingOptions field

* resolve documentation

* fix analyzer issues

* test(firestore, web): add e2e tests for long polling fields

* test(firestore, web): add e2e tests for long polling fields

* remove skip field from test

* remove unused import

* tests(firestore,web): make adjustments long polling tests

* tests(firestore,web): make adjustments long polling tests

---------

Co-authored-by: jude.kwashie <[email protected]>
  • Loading branch information
SelaseKay and jude.kwashie authored Sep 3, 2024
1 parent 13bb65f commit 6ec2a10
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'instance_e2e.dart';
import 'load_bundle_e2e.dart';
import 'query_e2e.dart';
import 'second_database.dart';
import 'settings_e2e.dart';
import 'snapshot_metadata_e2e.dart';
import 'timestamp_e2e.dart';
import 'transaction_e2e.dart';
Expand Down Expand Up @@ -57,5 +58,8 @@ void main() {
if (defaultTargetPlatform != TargetPlatform.windows) {
runSecondDatabaseTests();
}
if (kIsWeb) {
runSettingsTest();
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2020, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_test/flutter_test.dart';

void runSettingsTest() {
group(
'$Settings',
() {
late FirebaseFirestore firestore;

setUpAll(() async {
firestore = FirebaseFirestore.instance;
});

Future<Settings> initializeTest() async {
Settings firestoreSettings = const Settings(
persistenceEnabled: false,
webExperimentalForceLongPolling: true,
webExperimentalAutoDetectLongPolling: true,
webExperimentalLongPollingOptions: WebExperimentalLongPollingOptions(
timeoutDuration: Duration(seconds: 15),
),
);

return firestore.settings = firestoreSettings;
}

testWidgets('checks if long polling settings were applied', (_) async {
Settings settings = await initializeTest();

expect(settings.webExperimentalForceLongPolling, true);

expect(settings.webExperimentalAutoDetectLongPolling, true);

expect(
settings.webExperimentalLongPollingOptions,
settings.webExperimentalLongPollingOptions,
);
});
},
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export 'package:cloud_firestore_platform_interface/cloud_firestore_platform_inte
DocumentChangeType,
PersistenceSettings,
Settings,
WebExperimentalLongPollingOptions,
IndexField,
Index,
FieldOverrides,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ class FirebaseFirestore extends FirebasePluginPlatform {
persistenceEnabled: settings.persistenceEnabled,
host: settings.host,
cacheSizeBytes: settings.cacheSizeBytes,
webExperimentalForceLongPolling: settings.webExperimentalForceLongPolling,
webExperimentalAutoDetectLongPolling:
settings.webExperimentalAutoDetectLongPolling,
webExperimentalLongPollingOptions:
settings.webExperimentalLongPollingOptions,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Settings {
this.host,
this.sslEnabled,
this.cacheSizeBytes,
this.webExperimentalForceLongPolling,
this.webExperimentalAutoDetectLongPolling,
this.webExperimentalLongPollingOptions,
this.ignoreUndefinedProperties = false,
});

Expand Down Expand Up @@ -51,13 +54,39 @@ class Settings {
/// Web only.
final bool ignoreUndefinedProperties;

/// Forces the SDK’s underlying network transport (WebChannel) to use long-polling.
///
/// Each response from the backend will be closed immediately after the backend sends data
/// (by default responses are kept open in case the backend has more data to send).
/// This avoids incompatibility issues with certain proxies, antivirus software, etc.
/// that incorrectly buffer traffic indefinitely.
/// Use of this option will cause some performance degradation though.
final bool? webExperimentalForceLongPolling;

/// Configures the SDK's underlying transport (WebChannel) to automatically detect if long-polling should be used.
///
///This is very similar to [webExperimentalForceLongPolling], but only uses long-polling if required.
final bool? webExperimentalAutoDetectLongPolling;

/// Options that configure the SDK’s underlying network transport (WebChannel) when long-polling is used.
///
/// These options are only used if experimentalForceLongPolling is true
/// or if [webExperimentalAutoDetectLongPolling] is true and the auto-detection determined that long-polling was needed.
/// Otherwise, these options have no effect.
final WebExperimentalLongPollingOptions? webExperimentalLongPollingOptions;

/// Returns the settings as a [Map]
Map<String, dynamic> get asMap {
return {
'persistenceEnabled': persistenceEnabled,
'host': host,
'sslEnabled': sslEnabled,
'cacheSizeBytes': cacheSizeBytes,
'webExperimentalForceLongPolling': webExperimentalForceLongPolling,
'webExperimentalAutoDetectLongPolling':
webExperimentalAutoDetectLongPolling,
'webExperimentalLongPollingOptions':
webExperimentalLongPollingOptions?.asMap,
if (kIsWeb) 'ignoreUndefinedProperties': ignoreUndefinedProperties,
};
}
Expand All @@ -67,7 +96,10 @@ class Settings {
String? host,
bool? sslEnabled,
int? cacheSizeBytes,
bool? webExperimentalForceLongPolling,
bool? webExperimentalAutoDetectLongPolling,
bool? ignoreUndefinedProperties,
WebExperimentalLongPollingOptions? webExperimentalLongPollingOptions,
}) {
assert(
cacheSizeBytes == null ||
Expand All @@ -81,6 +113,13 @@ class Settings {
host: host ?? this.host,
sslEnabled: sslEnabled ?? this.sslEnabled,
cacheSizeBytes: cacheSizeBytes ?? this.cacheSizeBytes,
webExperimentalForceLongPolling: webExperimentalForceLongPolling ??
this.webExperimentalForceLongPolling,
webExperimentalAutoDetectLongPolling:
webExperimentalAutoDetectLongPolling ??
this.webExperimentalAutoDetectLongPolling,
webExperimentalLongPollingOptions: webExperimentalLongPollingOptions ??
this.webExperimentalLongPollingOptions,
ignoreUndefinedProperties:
ignoreUndefinedProperties ?? this.ignoreUndefinedProperties,
);
Expand All @@ -94,6 +133,12 @@ class Settings {
other.host == host &&
other.sslEnabled == sslEnabled &&
other.cacheSizeBytes == cacheSizeBytes &&
other.webExperimentalForceLongPolling ==
webExperimentalForceLongPolling &&
other.webExperimentalAutoDetectLongPolling ==
webExperimentalAutoDetectLongPolling &&
other.webExperimentalLongPollingOptions ==
webExperimentalLongPollingOptions &&
other.ignoreUndefinedProperties == ignoreUndefinedProperties;

@override
Expand All @@ -103,9 +148,46 @@ class Settings {
host,
sslEnabled,
cacheSizeBytes,
webExperimentalForceLongPolling,
webExperimentalAutoDetectLongPolling,
webExperimentalLongPollingOptions,
ignoreUndefinedProperties,
);

@override
String toString() => 'Settings($asMap)';
}

/// Options that configure the SDK’s underlying network transport (WebChannel) when long-polling is used.
@immutable
class WebExperimentalLongPollingOptions {
/// The desired maximum timeout interval, in seconds, to complete a long-polling GET response
///
/// Valid values are between 5 and 30, inclusive.
/// By default, when long-polling is used the "hanging GET" request sent by the client times out after 30 seconds.
/// To request a different timeout from the server, set this setting with the desired timeout.
/// Changing the default timeout may be useful, for example,
/// if the buffering proxy that necessitated enabling long-polling in the first place has a shorter timeout for hanging GET requests,
/// in which case setting the long-polling timeout to a shorter value,
/// such as 25 seconds, may fix prematurely-closed hanging GET requests.
final Duration? timeoutDuration;

const WebExperimentalLongPollingOptions({
this.timeoutDuration,
});

Map<String, dynamic> get asMap {
return {
'timeoutDuration': timeoutDuration?.inSeconds,
};
}

@override
bool operator ==(Object other) =>
other is WebExperimentalLongPollingOptions &&
other.runtimeType == runtimeType &&
other.timeoutDuration == timeoutDuration;

@override
int get hashCode => Object.hash(runtimeType, timeoutDuration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@ void main() {
persistenceEnabled: true,
host: 'foo bar',
sslEnabled: true,
webExperimentalForceLongPolling: false,
webExperimentalAutoDetectLongPolling: false,
cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED,
webExperimentalLongPollingOptions: WebExperimentalLongPollingOptions(
timeoutDuration: Duration(seconds: 4),
),
),
equals(
const Settings(
persistenceEnabled: true,
host: 'foo bar',
sslEnabled: true,
webExperimentalForceLongPolling: false,
webExperimentalAutoDetectLongPolling: false,
cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED,
webExperimentalLongPollingOptions:
WebExperimentalLongPollingOptions(
timeoutDuration: Duration(seconds: 4),
),
),
),
);
Expand Down Expand Up @@ -49,6 +60,11 @@ void main() {
persistenceEnabled: true,
host: 'foo bar',
sslEnabled: true,
webExperimentalAutoDetectLongPolling: false,
webExperimentalForceLongPolling: false,
webExperimentalLongPollingOptions: WebExperimentalLongPollingOptions(
timeoutDuration: Duration(seconds: 4),
),
cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED,
);

Expand All @@ -60,21 +76,35 @@ void main() {
'persistenceEnabled': null,
'host': null,
'sslEnabled': null,
'cacheSizeBytes': null
'cacheSizeBytes': null,
'webExperimentalForceLongPolling': null,
'webExperimentalAutoDetectLongPolling': null,
'webExperimentalLongPollingOptions': null,
});

expect(
const Settings(
persistenceEnabled: true,
host: 'foo bar',
sslEnabled: true,
cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED,
).asMap,
persistenceEnabled: true,
host: 'foo bar',
sslEnabled: true,
cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED,
webExperimentalAutoDetectLongPolling: true,
webExperimentalForceLongPolling: true,
webExperimentalLongPollingOptions:
WebExperimentalLongPollingOptions(
timeoutDuration: Duration(seconds: 4),
)).asMap,
<String, dynamic>{
'persistenceEnabled': true,
'host': 'foo bar',
'sslEnabled': true,
'cacheSizeBytes': Settings.CACHE_SIZE_UNLIMITED,
'webExperimentalForceLongPolling': true,
'webExperimentalAutoDetectLongPolling': true,
'webExperimentalLongPollingOptions':
const WebExperimentalLongPollingOptions(
timeoutDuration: Duration(seconds: 4),
).asMap
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,33 @@ class FirebaseFirestoreWeb extends FirebaseFirestorePlatform {
));
}

JSAny experimentalLongPollingOptions =
firestore_interop.ExperimentalLongPollingOptions(
timeoutSeconds: firestoreSettings.webExperimentalLongPollingOptions
?.timeoutDuration?.inSeconds.toJS) as JSAny;

if (firestoreSettings.host != null &&
firestoreSettings.sslEnabled != null) {
_interopSettings = firestore_interop.FirestoreSettings(
localCache: localCache,
host: firestoreSettings.host?.toJS,
ssl: firestoreSettings.sslEnabled?.toJS,
experimentalForceLongPolling:
firestoreSettings.webExperimentalForceLongPolling?.toJS,
experimentalAutoDetectLongPolling:
firestoreSettings.webExperimentalAutoDetectLongPolling?.toJS,
experimentalLongPollingOptions: experimentalLongPollingOptions,
ignoreUndefinedProperties:
firestoreSettings.ignoreUndefinedProperties.toJS,
);
} else {
_interopSettings = firestore_interop.FirestoreSettings(
localCache: localCache,
experimentalForceLongPolling:
firestoreSettings.webExperimentalForceLongPolling?.toJS,
experimentalAutoDetectLongPolling:
firestoreSettings.webExperimentalAutoDetectLongPolling?.toJS,
experimentalLongPollingOptions: experimentalLongPollingOptions,
ignoreUndefinedProperties:
firestoreSettings.ignoreUndefinedProperties.toJS,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,9 @@ abstract class FirestoreSettings {
JSString? host,
JSBoolean? ssl,
JSBoolean? ignoreUndefinedProperties,
JSBoolean? experimentalForceLongPolling,
JSBoolean? experimentalAutoDetectLongPolling,
JSAny? experimentalLongPollingOptions,
JSObject localCache,
});
}
Expand Down Expand Up @@ -748,6 +751,35 @@ extension FirestoreSettingsExtension on FirestoreSettings {
external set localCache(JSObject u);
}

/// Options that configure the SDK’s underlying network transport (WebChannel) when long-polling is used
/// These options are only used if experimentalForceLongPolling is true
/// or if experimentalAutoDetectLongPolling is true and the auto-detection determined that long-polling was needed.
/// Otherwise, these options have no effect.
@anonymous
@JS()
@staticInterop
abstract class ExperimentalLongPollingOptions {
external factory ExperimentalLongPollingOptions({
JSNumber? timeoutSeconds,
});
}

extension ExperimentalLongPollingOptionsExtension
on ExperimentalLongPollingOptions {
/// The desired maximum timeout interval, in seconds, to complete a long-polling GET response
/// Valid values are between 5 and 30, inclusive.
/// Floating point values are allowed and will be rounded to the nearest millisecond
/// By default, when long-polling is used the "hanging GET" request sent by the client times out after 30 seconds.
/// To request a different timeout from the server, set this setting with the desired timeout.
/// Changing the default timeout may be useful, for example,
/// if the buffering proxy that necessitated enabling long-polling in the first place has a shorter timeout for hanging GET requests,
/// in which case setting the long-polling timeout to a shorter value,
/// such as 25 seconds, may fix prematurely-closed hanging GET requests.
external JSNumber? get timeoutSeconds;

external set timeoutSeconds(JSNumber? v);
}

/// Union type from all supported SDK cache layer.
///
/// [MemoryLocalCache] and [MemoryCacheSettings] are the two only cache types supported by the SDK. Custom implementation is not supported.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
Loading

0 comments on commit 6ec2a10

Please sign in to comment.