Skip to content

Commit edd2016

Browse files
Hide Firestore codec class and move each class into separate files (flutter#452)
1 parent 83b9c3c commit edd2016

File tree

5 files changed

+124
-105
lines changed

5 files changed

+124
-105
lines changed

packages/cloud_firestore/lib/cloud_firestore.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ part 'src/document_change.dart';
2020
part 'src/document_snapshot.dart';
2121
part 'src/document_reference.dart';
2222
part 'src/firestore.dart';
23+
part 'src/geo_point.dart';
2324
part 'src/query.dart';
2425
part 'src/query_snapshot.dart';
2526
part 'src/set_options.dart';
27+
part 'src/firestore_message_codec.dart';
2628
part 'src/snapshot_metadata.dart';
29+
part 'src/transaction.dart';
2730
part 'src/write_batch.dart';

packages/cloud_firestore/lib/src/firestore.dart

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -106,108 +106,3 @@ class Firestore {
106106
return result?.cast<String, dynamic>() ?? <String, dynamic>{};
107107
}
108108
}
109-
110-
typedef Future<dynamic> TransactionHandler(Transaction transaction);
111-
112-
class Transaction {
113-
int _transactionId;
114-
115-
Transaction(this._transactionId);
116-
117-
Future<DocumentSnapshot> get(DocumentReference documentReference) async {
118-
final dynamic result = await Firestore.channel
119-
.invokeMethod('Transaction#get', <String, dynamic>{
120-
'transactionId': _transactionId,
121-
'path': documentReference.path,
122-
});
123-
if (result != null) {
124-
return new DocumentSnapshot._(documentReference.path,
125-
result['data'].cast<String, dynamic>(), Firestore.instance);
126-
} else {
127-
return null;
128-
}
129-
}
130-
131-
Future<void> delete(DocumentReference documentReference) async {
132-
return Firestore.channel
133-
.invokeMethod('Transaction#delete', <String, dynamic>{
134-
'transactionId': _transactionId,
135-
'path': documentReference.path,
136-
});
137-
}
138-
139-
Future<void> update(
140-
DocumentReference documentReference, Map<String, dynamic> data) async {
141-
return Firestore.channel
142-
.invokeMethod('Transaction#update', <String, dynamic>{
143-
'transactionId': _transactionId,
144-
'path': documentReference.path,
145-
'data': data,
146-
});
147-
}
148-
149-
Future<void> set(
150-
DocumentReference documentReference, Map<String, dynamic> data) async {
151-
return Firestore.channel.invokeMethod('Transaction#set', <String, dynamic>{
152-
'transactionId': _transactionId,
153-
'path': documentReference.path,
154-
'data': data,
155-
});
156-
}
157-
}
158-
159-
class FirestoreMessageCodec extends StandardMessageCodec {
160-
const FirestoreMessageCodec();
161-
162-
static const int _kDateTime = 128;
163-
static const int _kGeoPoint = 129;
164-
static const int _kDocumentReference = 130;
165-
166-
@override
167-
void writeValue(WriteBuffer buffer, dynamic value) {
168-
if (value is DateTime) {
169-
buffer.putUint8(_kDateTime);
170-
buffer.putInt64(value.millisecondsSinceEpoch);
171-
} else if (value is GeoPoint) {
172-
buffer.putUint8(_kGeoPoint);
173-
buffer.putFloat64(value.latitude);
174-
buffer.putFloat64(value.longitude);
175-
} else if (value is DocumentReference) {
176-
buffer.putUint8(_kDocumentReference);
177-
final List<int> bytes = utf8.encoder.convert(value.path);
178-
writeSize(buffer, bytes.length);
179-
buffer.putUint8List(bytes);
180-
} else {
181-
super.writeValue(buffer, value);
182-
}
183-
}
184-
185-
@override
186-
dynamic readValueOfType(int type, ReadBuffer buffer) {
187-
switch (type) {
188-
case _kDateTime:
189-
return new DateTime.fromMillisecondsSinceEpoch(buffer.getInt64());
190-
case _kGeoPoint:
191-
return new GeoPoint(buffer.getFloat64(), buffer.getFloat64());
192-
case _kDocumentReference:
193-
final int length = readSize(buffer);
194-
final String path = utf8.decoder.convert(buffer.getUint8List(length));
195-
return Firestore.instance.document(path);
196-
default:
197-
return super.readValueOfType(type, buffer);
198-
}
199-
}
200-
}
201-
202-
class GeoPoint {
203-
final double latitude;
204-
final double longitude;
205-
const GeoPoint(this.latitude, this.longitude);
206-
207-
@override
208-
bool operator ==(dynamic o) =>
209-
o is GeoPoint && o.latitude == latitude && o.longitude == longitude;
210-
211-
@override
212-
int get hashCode => hashValues(latitude, longitude);
213-
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2017, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
part of cloud_firestore;
6+
7+
@visibleForTesting
8+
class FirestoreMessageCodec extends StandardMessageCodec {
9+
const FirestoreMessageCodec();
10+
11+
static const int _kDateTime = 128;
12+
static const int _kGeoPoint = 129;
13+
static const int _kDocumentReference = 130;
14+
15+
@override
16+
void writeValue(WriteBuffer buffer, dynamic value) {
17+
if (value is DateTime) {
18+
buffer.putUint8(_kDateTime);
19+
buffer.putInt64(value.millisecondsSinceEpoch);
20+
} else if (value is GeoPoint) {
21+
buffer.putUint8(_kGeoPoint);
22+
buffer.putFloat64(value.latitude);
23+
buffer.putFloat64(value.longitude);
24+
} else if (value is DocumentReference) {
25+
buffer.putUint8(_kDocumentReference);
26+
final List<int> bytes = utf8.encoder.convert(value.path);
27+
writeSize(buffer, bytes.length);
28+
buffer.putUint8List(bytes);
29+
} else {
30+
super.writeValue(buffer, value);
31+
}
32+
}
33+
34+
@override
35+
dynamic readValueOfType(int type, ReadBuffer buffer) {
36+
switch (type) {
37+
case _kDateTime:
38+
return new DateTime.fromMillisecondsSinceEpoch(buffer.getInt64());
39+
case _kGeoPoint:
40+
return new GeoPoint(buffer.getFloat64(), buffer.getFloat64());
41+
case _kDocumentReference:
42+
final int length = readSize(buffer);
43+
final String path = utf8.decoder.convert(buffer.getUint8List(length));
44+
return Firestore.instance.document(path);
45+
default:
46+
return super.readValueOfType(type, buffer);
47+
}
48+
}
49+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
part of cloud_firestore;
6+
7+
class GeoPoint {
8+
final double latitude;
9+
final double longitude;
10+
const GeoPoint(this.latitude, this.longitude);
11+
12+
@override
13+
bool operator ==(dynamic o) =>
14+
o is GeoPoint && o.latitude == latitude && o.longitude == longitude;
15+
16+
@override
17+
int get hashCode => hashValues(latitude, longitude);
18+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2017, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
part of cloud_firestore;
6+
7+
typedef Future<dynamic> TransactionHandler(Transaction transaction);
8+
9+
class Transaction {
10+
int _transactionId;
11+
12+
Transaction(this._transactionId);
13+
14+
Future<DocumentSnapshot> get(DocumentReference documentReference) async {
15+
final dynamic result = await Firestore.channel
16+
.invokeMethod('Transaction#get', <String, dynamic>{
17+
'transactionId': _transactionId,
18+
'path': documentReference.path,
19+
});
20+
if (result != null) {
21+
return new DocumentSnapshot._(documentReference.path,
22+
result['data'].cast<String, dynamic>(), Firestore.instance);
23+
} else {
24+
return null;
25+
}
26+
}
27+
28+
Future<void> delete(DocumentReference documentReference) async {
29+
return Firestore.channel
30+
.invokeMethod('Transaction#delete', <String, dynamic>{
31+
'transactionId': _transactionId,
32+
'path': documentReference.path,
33+
});
34+
}
35+
36+
Future<void> update(
37+
DocumentReference documentReference, Map<String, dynamic> data) async {
38+
return Firestore.channel
39+
.invokeMethod('Transaction#update', <String, dynamic>{
40+
'transactionId': _transactionId,
41+
'path': documentReference.path,
42+
'data': data,
43+
});
44+
}
45+
46+
Future<void> set(
47+
DocumentReference documentReference, Map<String, dynamic> data) async {
48+
return Firestore.channel.invokeMethod('Transaction#set', <String, dynamic>{
49+
'transactionId': _transactionId,
50+
'path': documentReference.path,
51+
'data': data,
52+
});
53+
}
54+
}

0 commit comments

Comments
 (0)