Skip to content

Commit d83f632

Browse files
authored
feat(web): remove the dependency on package:js in favor of dart:js_interop (firebase#12534)
* feat(web): remove the dependency on package:js in favor of dart:js_interop * fix typing * fixing more stuffs * tests? * tests * more * fix typing * fix * fix typing * fixing interop typing * fixing null safety * more fix * database * tests * tests? * fix typing * test order? * storage * tests messaging * fix firestore boxing * test * improve * test * final?? * clean example app
1 parent 4b78987 commit d83f632

File tree

41 files changed

+276
-744
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+276
-744
lines changed

packages/cloud_firestore/cloud_firestore_web/lib/src/document_reference_web.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class DocumentReferenceWeb extends DocumentReferencePlatform {
4040
@override
4141
Future<void> update(Map<Object, dynamic> data) {
4242
return convertWebExceptions(
43-
() => _delegate.update(EncodeUtility.encodeMapDataFieldPath(data)!));
43+
() => _delegate.update(EncodeUtility.encodeMapDataFieldPath(data)!),
44+
);
4445
}
4546

4647
@override

packages/cloud_firestore/cloud_firestore_web/lib/src/interop/firestore.dart

+74-53
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
import 'dart:async';
99
import 'dart:js_interop';
10+
import 'dart:js_interop_unsafe';
1011

1112
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart'
1213
as platform_interface;
1314
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';
1415
import 'package:cloud_firestore_web/src/utils/encode_utility.dart';
1516
import 'package:firebase_core/firebase_core.dart';
16-
import 'package:firebase_core_web/firebase_core_web_interop.dart'
17-
hide jsify, dartify;
17+
import 'package:firebase_core_web/firebase_core_web_interop.dart';
1818
import 'package:flutter/foundation.dart';
1919

2020
import 'firestore_interop.dart' as firestore_interop;
@@ -275,8 +275,7 @@ class LoadBundleTaskProgress
275275
final int totalDocuments;
276276
}
277277

278-
class WriteBatch extends JsObjectWrapper<firestore_interop.WriteBatchJsImpl>
279-
with _Updatable {
278+
class WriteBatch extends JsObjectWrapper<firestore_interop.WriteBatchJsImpl> {
280279
static final _expando = Expando<WriteBatch>();
281280

282281
/// Creates a new WriteBatch from a [jsObject].
@@ -302,12 +301,12 @@ class WriteBatch extends JsObjectWrapper<firestore_interop.WriteBatchJsImpl>
302301

303302
WriteBatch update(DocumentReference documentRef, Map<String, dynamic> data) =>
304303
WriteBatch.getInstance(
305-
_wrapUpdateFunctionCall(jsObject, data, documentRef));
304+
jsObject.update(documentRef.jsObject, jsify(data)! as JSObject),
305+
);
306306
}
307307

308308
class DocumentReference
309-
extends JsObjectWrapper<firestore_interop.DocumentReferenceJsImpl>
310-
with _Updatable {
309+
extends JsObjectWrapper<firestore_interop.DocumentReferenceJsImpl> {
311310
static final _expando = Expando<DocumentReference>();
312311

313312
/// Non-null [Firestore] the document is in.
@@ -395,24 +394,25 @@ class DocumentReference
395394
}
396395

397396
Future<void> set(Map<String, dynamic> data,
398-
[firestore_interop.SetOptions? options]) {
399-
var jsObjectSet = (options != null)
400-
? firestore_interop.setDoc(jsObject, jsify(data)! as JSObject, options)
401-
: firestore_interop.setDoc(jsObject, jsify(data)! as JSObject);
402-
403-
return jsObjectSet.toDart;
397+
[firestore_interop.SetOptions? options]) async {
398+
if (options != null) {
399+
await firestore_interop.setDoc(jsObject, jsify(data), options).toDart;
400+
return;
401+
}
402+
await firestore_interop.setDoc(jsObject, jsify(data)).toDart;
404403
}
405404

406-
Future<void> update(Map<firestore_interop.FieldPath, dynamic> data) {
407-
final alternatingFieldValues = data.keys
405+
Future<void> update(Map<firestore_interop.FieldPath, dynamic> data) async {
406+
final List<JSAny?> alternatingFieldValues = data.keys
408407
.map((e) => [jsify(e), jsify(data[e])])
409408
.expand((e) => e)
410409
.toList();
411410

412-
return handleThenable(callMethod(firestore_interop.updateDoc, 'apply', [
411+
await firestore_interop.updateDoc
412+
.callMethodVarArgs<JSPromise>('apply'.toJS, [
413413
null,
414-
[jsObject, ...alternatingFieldValues]
415-
]));
414+
[jsObject, ...alternatingFieldValues].jsify()
415+
]).toDart;
416416
}
417417
}
418418

@@ -503,16 +503,28 @@ class Query<T extends firestore_interop.QueryJsImpl>
503503
}
504504

505505
Query startAfter({DocumentSnapshot? snapshot, List<dynamic>? fieldValues}) =>
506-
Query.fromJsObject(firestore_interop.query(
506+
Query.fromJsObject(
507+
firestore_interop.query(
507508
jsObject,
508509
_createQueryConstraint(
509-
firestore_interop.startAfter, snapshot, fieldValues)));
510+
firestore_interop.startAfter,
511+
snapshot,
512+
fieldValues,
513+
),
514+
),
515+
);
510516

511517
Query startAt({DocumentSnapshot? snapshot, List<dynamic>? fieldValues}) =>
512-
Query.fromJsObject(firestore_interop.query(
518+
Query.fromJsObject(
519+
firestore_interop.query(
513520
jsObject,
514521
_createQueryConstraint(
515-
firestore_interop.startAt, snapshot, fieldValues)));
522+
firestore_interop.startAt,
523+
snapshot,
524+
fieldValues,
525+
),
526+
),
527+
);
516528

517529
Query where(dynamic fieldPath, String opStr, dynamic value) =>
518530
Query.fromJsObject(
@@ -530,18 +542,24 @@ class Query<T extends firestore_interop.QueryJsImpl>
530542
/// [fieldValues].
531543
/// We need to call this method in all paginating methods to fix that Dart
532544
/// doesn't support varargs - we need to use [List] to call js function.
533-
S? _createQueryConstraint<S>(
545+
firestore_interop.QueryConstraintJsImpl _createQueryConstraint<S>(
534546
Object method, DocumentSnapshot? snapshot, List<dynamic>? fieldValues) {
535547
if (snapshot == null && fieldValues == null) {
536548
throw ArgumentError(
537549
'Please provide either snapshot or fieldValues parameter.');
538550
}
539551

540-
var args = (snapshot != null)
552+
final args = (snapshot != null)
541553
? [snapshot.jsObject]
542554
: fieldValues!.map(jsify).toList();
543555

544-
return callMethod(method, 'apply', [null, jsify(args)]);
556+
return (method as JSObject).callMethodVarArgs<JSAny>(
557+
'apply'.toJS,
558+
[
559+
null,
560+
jsify(args).jsify(),
561+
],
562+
) as firestore_interop.QueryConstraintJsImpl;
545563
}
546564

547565
Object _parseFilterWith(Map<String, Object?> map) {
@@ -566,9 +584,21 @@ class Query<T extends firestore_interop.QueryJsImpl>
566584
}
567585

568586
if (opStr == 'OR') {
569-
return callMethod(firestore_interop.or, 'apply', [null, jsFilters]);
587+
return firestore_interop.or.callMethodVarArgs<JSAny>(
588+
'apply'.toJS,
589+
[
590+
null,
591+
jsFilters.jsify(),
592+
],
593+
);
570594
} else if (opStr == 'AND') {
571-
return callMethod(firestore_interop.and, 'apply', [null, jsFilters]);
595+
return firestore_interop.and.callMethodVarArgs<JSAny>(
596+
'apply'.toJS,
597+
[
598+
null,
599+
jsFilters.jsify(),
600+
],
601+
);
572602
}
573603

574604
throw Exception('InvalidOperator');
@@ -747,8 +777,7 @@ class QuerySnapshot
747777
.toDart;
748778
}
749779

750-
class Transaction extends JsObjectWrapper<firestore_interop.TransactionJsImpl>
751-
with _Updatable {
780+
class Transaction extends JsObjectWrapper<firestore_interop.TransactionJsImpl> {
752781
static final _expando = Expando<Transaction>();
753782

754783
/// Creates a new Transaction from a [jsObject].
@@ -779,24 +808,8 @@ class Transaction extends JsObjectWrapper<firestore_interop.TransactionJsImpl>
779808
Transaction update(
780809
DocumentReference documentRef, Map<String, dynamic> data) =>
781810
Transaction.getInstance(
782-
_wrapUpdateFunctionCall(jsObject, data, documentRef));
783-
}
784-
785-
/// Mixin class for all classes with the [update()] method. We need to call
786-
/// [_wrapUpdateFunctionCall()] in all [update()] methods to fix that Dart
787-
/// doesn't support varargs - we need to use [List] to call js function.
788-
mixin _Updatable {
789-
/// Calls js [:update():] method on [jsObject] with [data] or list of
790-
/// [fieldsAndValues] and optionally [documentRef].
791-
T? _wrapUpdateFunctionCall<T>(jsObject, Map<String, dynamic> data,
792-
[DocumentReference? documentRef]) {
793-
var args = [jsify(data)];
794-
// documentRef has to be the first parameter in list of args
795-
if (documentRef != null) {
796-
args.insert(0, documentRef.jsObject as JSObject);
797-
}
798-
return callMethod(jsObject, 'update', args);
799-
}
811+
jsObject.update(documentRef.jsObject, jsify(data)!),
812+
);
800813
}
801814

802815
class _FieldValueDelete implements FieldValue {
@@ -826,9 +839,13 @@ class _FieldValueArrayUnion extends _FieldValueArray {
826839

827840
@override
828841
firestore_interop.FieldValue? _jsify() {
829-
// This uses var arg so cannot use js package
830-
return callMethod(
831-
firestore_interop.arrayUnion, 'apply', [null, jsify(elements)]);
842+
return firestore_interop.arrayUnion.callMethodVarArgs<JSAny>(
843+
'apply'.toJS,
844+
[
845+
null,
846+
jsify(elements),
847+
],
848+
) as firestore_interop.FieldValue;
832849
}
833850

834851
@override
@@ -840,9 +857,13 @@ class _FieldValueArrayRemove extends _FieldValueArray {
840857

841858
@override
842859
firestore_interop.FieldValue? _jsify() {
843-
// This uses var arg so cannot use js package
844-
return callMethod(
845-
firestore_interop.arrayRemove, 'apply', [null, jsify(elements)]);
860+
return firestore_interop.arrayRemove.callMethodVarArgs<JSAny>(
861+
'apply'.toJS,
862+
[
863+
null,
864+
jsify(elements),
865+
],
866+
) as firestore_interop.FieldValue;
846867
}
847868

848869
@override

packages/cloud_firestore/cloud_firestore_web/lib/src/interop/firestore_interop.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ external FieldValue serverTimestamp();
273273
@staticInterop
274274
external JSPromise setDoc(
275275
DocumentReferenceJsImpl reference,
276-
JSObject data, [
276+
JSAny? data, [
277277
SetOptions? options,
278278
]);
279279

@@ -348,7 +348,9 @@ extension WriteBatchJsImplExtension on WriteBatchJsImpl {
348348
[SetOptions? options]);
349349

350350
external WriteBatchJsImpl update(
351-
DocumentReferenceJsImpl documentRef, JSObject dataOrFieldsAndValues);
351+
DocumentReferenceJsImpl documentRef,
352+
JSAny? dataOrFieldsAndValues,
353+
);
352354
}
353355

354356
@JS('CollectionReference')
@@ -593,7 +595,7 @@ extension TransactionJsImplExtension on TransactionJsImpl {
593595
[SetOptions? options]);
594596

595597
external TransactionJsImpl update(
596-
DocumentReferenceJsImpl documentRef, JSObject dataOrFieldsAndValues);
598+
DocumentReferenceJsImpl documentRef, JSAny dataOrFieldsAndValues);
597599
}
598600

599601
@JS('Timestamp')

packages/cloud_firestore/cloud_firestore_web/pubspec.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dependencies:
1919
sdk: flutter
2020
flutter_web_plugins:
2121
sdk: flutter
22-
js: ^0.6.3
2322

2423
dev_dependencies:
2524
firebase_core_platform_interface: ^5.0.0

packages/cloud_functions/cloud_functions_web/lib/interop/functions.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ dynamic _convertNested(dynamic object) {
111111
return map;
112112
} else {
113113
// For non-nested types, attempt to convert directly
114-
return dartify(object);
114+
return _dartify(object);
115115
}
116116
}
117117

packages/cloud_functions/cloud_functions_web/pubspec.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ dependencies:
1717
sdk: flutter
1818
flutter_web_plugins:
1919
sdk: flutter
20-
js: ^0.6.3
21-
web: ^0.5.1
2220

2321
dev_dependencies:
2422
firebase_core_platform_interface: ^5.0.0

packages/cloud_functions/cloud_functions_web/test/cloud_functions_web_test.dart

-95
This file was deleted.

0 commit comments

Comments
 (0)