-
Notifications
You must be signed in to change notification settings - Fork 4k
/
method_channel_document_reference.dart
161 lines (147 loc) · 4.77 KB
/
method_channel_document_reference.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// ignore_for_file: require_trailing_commas
// Copyright 2017, 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 'dart:async';
import 'package:_flutterfire_internals/_flutterfire_internals.dart';
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';
import 'package:cloud_firestore_platform_interface/src/internal/pointer.dart';
import 'package:flutter/services.dart';
import 'method_channel_firestore.dart';
import 'utils/exception.dart';
/// An implementation of [DocumentReferencePlatform] that uses [MethodChannel] to
/// communicate with Firebase plugins.
class MethodChannelDocumentReference extends DocumentReferencePlatform {
late Pointer _pointer;
/// Creates a [DocumentReferencePlatform] that is implemented using [MethodChannel].
MethodChannelDocumentReference(
FirebaseFirestorePlatform firestore,
String path,
this.pigeonApp,
) : super(firestore, path) {
_pointer = Pointer(path);
}
final FirestorePigeonFirebaseApp pigeonApp;
@override
Future<void> set(Map<String, dynamic> data, [SetOptions? options]) async {
try {
await MethodChannelFirebaseFirestore.pigeonChannel.documentReferenceSet(
pigeonApp,
DocumentReferenceRequest(
path: _pointer.path,
data: data,
option: PigeonDocumentOption(
merge: options?.merge,
mergeFields:
options?.mergeFields?.map((e) => e.components).toList(),
),
),
);
} catch (e, stack) {
convertPlatformException(e, stack);
}
}
@override
Future<void> update(Map<FieldPath, dynamic> data) async {
try {
await MethodChannelFirebaseFirestore.pigeonChannel
.documentReferenceUpdate(
pigeonApp,
DocumentReferenceRequest(
path: _pointer.path,
data: data,
),
);
} catch (e, stack) {
convertPlatformException(e, stack);
}
}
@override
Future<DocumentSnapshotPlatform> get(
[GetOptions options = const GetOptions()]) async {
try {
final result = await MethodChannelFirebaseFirestore.pigeonChannel
.documentReferenceGet(
pigeonApp,
DocumentReferenceRequest(
path: _pointer.path,
source: options.source,
serverTimestampBehavior: options.serverTimestampBehavior,
),
);
return DocumentSnapshotPlatform(
firestore,
_pointer.path,
result.data,
result.metadata,
);
} catch (e, stack) {
convertPlatformException(e, stack);
}
}
@override
Future<void> delete() async {
try {
await MethodChannelFirebaseFirestore.pigeonChannel
.documentReferenceDelete(
pigeonApp,
DocumentReferenceRequest(
path: _pointer.path,
),
);
} catch (e, stack) {
convertPlatformException(e, stack);
}
}
@override
Stream<DocumentSnapshotPlatform> snapshots({
bool includeMetadataChanges = false,
ServerTimestampBehavior serverTimestampBehavior =
ServerTimestampBehavior.none,
ListenSource source = ListenSource.defaultSource,
}) {
// It's fine to let the StreamController be garbage collected once all the
// subscribers have cancelled; this analyzer warning is safe to ignore.
late StreamController<DocumentSnapshotPlatform>
controller; // ignore: close_sinks
StreamSubscription<dynamic>? snapshotStreamSubscription;
controller = StreamController<DocumentSnapshotPlatform>.broadcast(
onListen: () async {
final observerId = await MethodChannelFirebaseFirestore.pigeonChannel
.documentReferenceSnapshot(
pigeonApp,
DocumentReferenceRequest(
path: _pointer.path,
serverTimestampBehavior: serverTimestampBehavior,
),
includeMetadataChanges,
source,
);
snapshotStreamSubscription =
MethodChannelFirebaseFirestore.documentSnapshotChannel(observerId)
.receiveGuardedBroadcastStream(
onError: convertPlatformException,
)
.listen(
(snapshot) {
final PigeonDocumentSnapshot result =
PigeonDocumentSnapshot.decode(snapshot);
controller.add(
DocumentSnapshotPlatform(
firestore,
result.path,
result.data,
result.metadata,
),
);
},
onError: controller.addError,
);
},
onCancel: () {
snapshotStreamSubscription?.cancel();
},
);
return controller.stream;
}
}