Skip to content

Commit a1e6bed

Browse files
committed
chore: Method Channel to Pigeon
1 parent d4c4fb9 commit a1e6bed

File tree

11 files changed

+3151
-1196
lines changed

11 files changed

+3151
-1196
lines changed

packages/firebase_database/firebase_database/android/src/main/kotlin/io/flutter/plugins/firebase/database/GeneratedAndroidFirebaseDatabase.g.kt

Lines changed: 491 additions & 160 deletions
Large diffs are not rendered by default.

packages/firebase_database/firebase_database/ios/firebase_database/Sources/firebase_database/FirebaseDatabaseMessages.g.swift

Lines changed: 467 additions & 175 deletions
Large diffs are not rendered by default.

packages/firebase_database/firebase_database/windows/messages.g.cpp

Lines changed: 802 additions & 306 deletions
Large diffs are not rendered by default.

packages/firebase_database/firebase_database/windows/messages.g.h

Lines changed: 316 additions & 145 deletions
Large diffs are not rendered by default.

packages/firebase_database/firebase_database_platform_interface/lib/src/method_channel/method_channel_database.dart

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:firebase_core/firebase_core.dart';
66
import 'package:firebase_database_platform_interface/firebase_database_platform_interface.dart';
77
import 'package:firebase_database_platform_interface/src/method_channel/utils/utils.dart';
8+
import 'package:firebase_database_platform_interface/src/pigeon/messages.pigeon.dart' as pigeon;
89
import 'package:flutter/services.dart';
910

1011
import 'method_channel_database_reference.dart';
@@ -106,6 +107,22 @@ class MethodChannelDatabase extends DatabasePlatform {
106107
static const MethodChannel channel =
107108
MethodChannel('plugins.flutter.io/firebase_database');
108109

110+
/// The pigeon channel instance to communicate through.
111+
static final pigeon.FirebaseDatabaseHostApi pigeonChannel = pigeon.FirebaseDatabaseHostApi();
112+
113+
/// FirebaseApp pigeon instance
114+
pigeon.FirebaseApp get pigeonFirebaseApp {
115+
return pigeon.FirebaseApp(
116+
appName: app!.name,
117+
databaseURL: databaseURL,
118+
persistenceEnabled: _persistenceEnabled,
119+
cacheSizeBytes: _cacheSizeBytes,
120+
loggingEnabled: _loggingEnabled,
121+
emulatorHost: _emulatorHost,
122+
emulatorPort: _emulatorPort,
123+
);
124+
}
125+
109126
@override
110127
void useDatabaseEmulator(String host, int port) {
111128
_emulatorHost = host;
@@ -138,10 +155,7 @@ class MethodChannelDatabase extends DatabasePlatform {
138155
@override
139156
Future<void> goOnline() {
140157
try {
141-
return channel.invokeMethod<void>(
142-
'FirebaseDatabase#goOnline',
143-
getChannelArguments(),
144-
);
158+
return pigeonChannel.goOnline(pigeonFirebaseApp);
145159
} catch (e, s) {
146160
convertPlatformException(e, s);
147161
}
@@ -152,10 +166,7 @@ class MethodChannelDatabase extends DatabasePlatform {
152166
@override
153167
Future<void> goOffline() {
154168
try {
155-
return channel.invokeMethod<void>(
156-
'FirebaseDatabase#goOffline',
157-
getChannelArguments(),
158-
);
169+
return pigeonChannel.goOffline(pigeonFirebaseApp);
159170
} catch (e, s) {
160171
convertPlatformException(e, s);
161172
}
@@ -174,10 +185,7 @@ class MethodChannelDatabase extends DatabasePlatform {
174185
@override
175186
Future<void> purgeOutstandingWrites() {
176187
try {
177-
return channel.invokeMethod<void>(
178-
'FirebaseDatabase#purgeOutstandingWrites',
179-
getChannelArguments(),
180-
);
188+
return pigeonChannel.purgeOutstandingWrites(pigeonFirebaseApp);
181189
} catch (e, s) {
182190
convertPlatformException(e, s);
183191
}

packages/firebase_database/firebase_database_platform_interface/lib/src/method_channel/method_channel_database_reference.dart

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:firebase_database_platform_interface/firebase_database_platform_interface.dart';
66
import 'package:firebase_database_platform_interface/src/method_channel/utils/utils.dart';
7+
import 'package:firebase_database_platform_interface/src/pigeon/messages.pigeon.dart' as pigeon;
78

89
import 'method_channel_database.dart';
910
import 'method_channel_on_disconnect.dart';
@@ -12,14 +13,6 @@ import 'method_channel_transaction_result.dart';
1213
import 'utils/exception.dart';
1314
import 'utils/push_id_generator.dart';
1415

15-
import 'package:firebase_database_platform_interface/src/pigeon/messages.dart';
16-
import 'package:firebase_database_platform_interface/src/pigeon/messages.pigeon.dart';
17-
18-
extension DatabaseReferencePlatformExtension on DatabaseReferencePlatform {
19-
Future<void> set(Object? value) async {
20-
await FirebaseDatabaseHostApi.set(value);
21-
}
22-
}
2316

2417
/// DatabaseReference represents a particular location in your Firebase
2518
/// Database and can be used for reading or writing data to that location.
@@ -84,13 +77,10 @@ class MethodChannelDatabaseReference extends MethodChannelQuery
8477
@override
8578
Future<void> set(Object? value) async {
8679
try {
87-
await MethodChannelDatabase.channel.invokeMethod<void>(
88-
'DatabaseReference#set',
89-
database.getChannelArguments({
90-
'path': path,
91-
if (value != null) 'value': transformValue(value),
92-
}),
93-
);
80+
await MethodChannelDatabase.pigeonChannel.set(pigeon.SetOptions(
81+
path: path,
82+
value: value != null ? transformValue(value) : null,
83+
));
9484
} catch (e, s) {
9585
convertPlatformException(e, s);
9686
}
@@ -99,14 +89,11 @@ class MethodChannelDatabaseReference extends MethodChannelQuery
9989
@override
10090
Future<void> setWithPriority(Object? value, Object? priority) async {
10191
try {
102-
await MethodChannelDatabase.channel.invokeMethod<void>(
103-
'DatabaseReference#setWithPriority',
104-
database.getChannelArguments({
105-
'path': path,
106-
if (value != null) 'value': transformValue(value),
107-
if (priority != null) 'priority': priority,
108-
}),
109-
);
92+
await MethodChannelDatabase.pigeonChannel.setWithPriority(pigeon.SetOptions(
93+
path: path,
94+
value: value != null ? transformValue(value) : null,
95+
priority: priority,
96+
));
11097
} catch (e, s) {
11198
convertPlatformException(e, s);
11299
}
@@ -115,13 +102,10 @@ class MethodChannelDatabaseReference extends MethodChannelQuery
115102
@override
116103
Future<void> update(Map<String, Object?> value) async {
117104
try {
118-
await MethodChannelDatabase.channel.invokeMethod<void>(
119-
'DatabaseReference#update',
120-
database.getChannelArguments({
121-
'path': path,
122-
'value': transformValue(value),
123-
}),
124-
);
105+
await MethodChannelDatabase.pigeonChannel.update(pigeon.UpdateOptions(
106+
path: path,
107+
value: transformValue(value) as Map<String, Object?>,
108+
));
125109
} catch (e, s) {
126110
convertPlatformException(e, s);
127111
}
@@ -130,13 +114,10 @@ class MethodChannelDatabaseReference extends MethodChannelQuery
130114
@override
131115
Future<void> setPriority(Object? priority) async {
132116
try {
133-
await MethodChannelDatabase.channel.invokeMethod<void>(
134-
'DatabaseReference#setPriority',
135-
database.getChannelArguments({
136-
'path': path,
137-
if (priority != null) 'priority': priority,
138-
}),
139-
);
117+
await MethodChannelDatabase.pigeonChannel.setPriority(pigeon.SetPriorityOptions(
118+
path: path,
119+
priority: priority!,
120+
));
140121
} catch (e, s) {
141122
convertPlatformException(e, s);
142123
}
@@ -159,14 +140,11 @@ class MethodChannelDatabaseReference extends MethodChannelQuery
159140
MethodChannelDatabase.transactions[key] = transactionHandler;
160141

161142
try {
162-
final result = await channel.invokeMethod(
163-
'DatabaseReference#runTransaction',
164-
database.getChannelArguments({
165-
'path': path,
166-
'transactionApplyLocally': applyLocally,
167-
'transactionKey': key,
168-
}),
169-
);
143+
await MethodChannelDatabase.pigeonChannel.runTransaction(pigeon.TransactionOptions(
144+
path: path,
145+
transactionHandler: pigeon.TransactionHandler(transactionKey: key),
146+
applyLocally: applyLocally,
147+
));
170148

171149
// We store Dart only errors that occur inside users handlers - to avoid
172150
// serializing the error and sending it to native only to have to send it
@@ -176,6 +154,17 @@ class MethodChannelDatabaseReference extends MethodChannelQuery
176154
throw possibleError;
177155
}
178156

157+
// For now, we'll need to keep the old method channel for transaction results
158+
// until we can fully migrate the transaction handling
159+
final result = await channel.invokeMethod(
160+
'DatabaseReference#runTransaction',
161+
database.getChannelArguments({
162+
'path': path,
163+
'transactionApplyLocally': applyLocally,
164+
'transactionKey': key,
165+
}),
166+
);
167+
179168
return MethodChannelTransactionResult(
180169
result!['committed'] as bool,
181170
this,

packages/firebase_database/firebase_database_platform_interface/lib/src/method_channel/method_channel_on_disconnect.dart

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:firebase_database_platform_interface/firebase_database_platform_interface.dart';
66
import 'package:firebase_database_platform_interface/src/method_channel/utils/utils.dart';
7+
import 'package:firebase_database_platform_interface/src/pigeon/messages.pigeon.dart' as pigeon;
78

89
import 'method_channel_database.dart';
910
import 'utils/exception.dart';
@@ -19,13 +20,10 @@ class MethodChannelOnDisconnect extends OnDisconnectPlatform {
1920
@override
2021
Future<void> set(Object? value) async {
2122
try {
22-
await MethodChannelDatabase.channel.invokeMethod<void>(
23-
'OnDisconnect#set',
24-
database.getChannelArguments({
25-
'path': ref.path,
26-
if (value != null) 'value': transformValue(value),
27-
}),
28-
);
23+
await MethodChannelDatabase.pigeonChannel.onDisconnectSet(pigeon.OnDisconnectOptions(
24+
path: ref.path,
25+
value: value != null ? transformValue(value) : null,
26+
));
2927
} catch (e, s) {
3028
convertPlatformException(e, s);
3129
}
@@ -34,16 +32,11 @@ class MethodChannelOnDisconnect extends OnDisconnectPlatform {
3432
@override
3533
Future<void> setWithPriority(Object? value, Object? priority) async {
3634
try {
37-
await MethodChannelDatabase.channel.invokeMethod<void>(
38-
'OnDisconnect#setWithPriority',
39-
database.getChannelArguments(
40-
{
41-
'path': ref.path,
42-
if (value != null) 'value': transformValue(value),
43-
if (priority != null) 'priority': priority,
44-
},
45-
),
46-
);
35+
await MethodChannelDatabase.pigeonChannel.onDisconnectSetWithPriority(pigeon.OnDisconnectOptions(
36+
path: ref.path,
37+
value: value != null ? transformValue(value) : null,
38+
priority: priority,
39+
));
4740
} catch (e, s) {
4841
convertPlatformException(e, s);
4942
}
@@ -55,10 +48,9 @@ class MethodChannelOnDisconnect extends OnDisconnectPlatform {
5548
@override
5649
Future<void> cancel() async {
5750
try {
58-
await MethodChannelDatabase.channel.invokeMethod<void>(
59-
'OnDisconnect#cancel',
60-
database.getChannelArguments({'path': ref.path}),
61-
);
51+
await MethodChannelDatabase.pigeonChannel.onDisconnectCancel(pigeon.DatabaseReference(
52+
path: ref.path,
53+
));
6254
} catch (e, s) {
6355
convertPlatformException(e, s);
6456
}
@@ -67,13 +59,10 @@ class MethodChannelOnDisconnect extends OnDisconnectPlatform {
6759
@override
6860
Future<void> update(Map<String, Object?> value) async {
6961
try {
70-
await MethodChannelDatabase.channel.invokeMethod<void>(
71-
'OnDisconnect#update',
72-
database.getChannelArguments({
73-
'path': ref.path,
74-
'value': transformValue(value),
75-
}),
76-
);
62+
await MethodChannelDatabase.pigeonChannel.onDisconnectUpdate(pigeon.UpdateOptions(
63+
path: ref.path,
64+
value: transformValue(value) as Map<String, Object?>,
65+
));
7766
} catch (e, s) {
7867
convertPlatformException(e, s);
7968
}

packages/firebase_database/firebase_database_platform_interface/lib/src/method_channel/method_channel_query.dart

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:_flutterfire_internals/_flutterfire_internals.dart';
66
import 'package:firebase_database_platform_interface/firebase_database_platform_interface.dart';
7+
import 'package:firebase_database_platform_interface/src/pigeon/messages.pigeon.dart' as pigeon;
78
import 'package:flutter/services.dart';
89

910
import 'method_channel_data_snapshot.dart';
@@ -37,24 +38,21 @@ class MethodChannelQuery extends QueryPlatform {
3738
QueryModifiers modifiers,
3839
DatabaseEventType eventType,
3940
) async* {
40-
const channel = MethodChannelDatabase.channel;
4141
List<Map<String, Object?>> modifierList = modifiers.toList();
4242
// Create a unique event channel naming prefix using path, app name,
4343
// databaseUrl, event type and ordered modifier list
4444
String eventChannelNamePrefix =
4545
'$path-${database.app!.name}-${database.databaseURL}-$eventType-$modifierList';
4646

4747
// Create the EventChannel on native.
48-
final channelName = await channel.invokeMethod<String>(
49-
'Query#observe',
50-
database.getChannelArguments({
51-
'path': path,
52-
'modifiers': modifierList,
53-
'eventChannelNamePrefix': eventChannelNamePrefix,
54-
}),
55-
);
48+
final channelName = await MethodChannelDatabase.pigeonChannel.observe(pigeon.EventObserver(
49+
path: path,
50+
eventType: eventTypeToString(eventType),
51+
eventChannelNamePrefix: eventChannelNamePrefix,
52+
modifiers: modifierList,
53+
));
5654

57-
yield* EventChannel(channelName!).receiveGuardedBroadcastStream(
55+
yield* EventChannel(channelName).receiveGuardedBroadcastStream(
5856
arguments: <String, Object?>{'eventType': eventTypeToString(eventType)},
5957
onError: convertPlatformException,
6058
).map(
@@ -67,16 +65,13 @@ class MethodChannelQuery extends QueryPlatform {
6765
@override
6866
Future<DataSnapshotPlatform> get(QueryModifiers modifiers) async {
6967
try {
70-
final result = await channel.invokeMapMethod(
71-
'Query#get',
72-
database.getChannelArguments({
73-
'path': path,
74-
'modifiers': modifiers.toList(),
75-
}),
76-
);
68+
final result = await MethodChannelDatabase.pigeonChannel.get(pigeon.GetOptions(
69+
path: path,
70+
modifiers: modifiers.toList(),
71+
));
7772
return MethodChannelDataSnapshot(
7873
ref,
79-
Map<String, dynamic>.from(result!['snapshot']),
74+
Map<String, dynamic>.from(result.snapshot),
8075
);
8176
} catch (e, s) {
8277
convertPlatformException(e, s);
@@ -99,12 +94,11 @@ class MethodChannelQuery extends QueryPlatform {
9994
@override
10095
Future<void> keepSynced(QueryModifiers modifiers, bool value) async {
10196
try {
102-
await channel.invokeMethod<void>(
103-
'Query#keepSynced',
104-
database.getChannelArguments(
105-
{'path': path, 'modifiers': modifiers.toList(), 'value': value},
106-
),
107-
);
97+
await MethodChannelDatabase.pigeonChannel.keepSynced(pigeon.KeepSyncedOptions(
98+
path: path,
99+
modifiers: modifiers.toList(),
100+
value: value,
101+
));
108102
} catch (e, s) {
109103
convertPlatformException(e, s);
110104
}

0 commit comments

Comments
 (0)