Skip to content

Commit efbb9c6

Browse files
authored
Livequery: combine both implementations (#433)
* make livequeries web implementation more similar to the mobile implementation * combining both livequery implementations * make livequery "part of flutter_parse_sdk" * smal fix
1 parent 7462a01 commit efbb9c6

File tree

6 files changed

+104
-508
lines changed

6 files changed

+104
-508
lines changed

lib/parse_server_sdk.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ import 'dart:math';
77
import 'dart:typed_data';
88
import 'dart:ui' as ui;
99

10+
import 'package:connectivity/connectivity.dart';
11+
import 'package:flutter/widgets.dart';
1012
import 'package:http/http.dart';
1113
import 'package:http/io_client.dart';
1214
import 'package:meta/meta.dart';
1315
import 'package:package_info/package_info.dart';
16+
import 'package:parse_server_sdk/src/network/parse_websocket.dart'
17+
as parse_web_socket;
1418
import 'package:path/path.dart' as path;
1519
import 'package:path_provider/path_provider.dart';
1620
import 'package:sembast/sembast.dart';
1721
import 'package:sembast/sembast_io.dart';
1822
import 'package:shared_preferences/shared_preferences.dart';
1923
import 'package:uuid/uuid.dart';
24+
import 'package:web_socket_channel/web_socket_channel.dart';
2025
import 'package:xxtea/xxtea.dart';
2126

22-
export 'src/network/parse_live_query.dart'
23-
if (dart.library.js) 'src/network/parse_live_query_web.dart';
2427
export 'src/utils/parse_live_list.dart';
2528

2629
part 'package:parse_server_sdk/src/data/core_store.dart';
@@ -37,6 +40,7 @@ part 'src/base/parse_constants.dart';
3740
part 'src/data/parse_core_data.dart';
3841
part 'src/enums/parse_enum_api_rq.dart';
3942
part 'src/network/parse_http_client.dart';
43+
part 'src/network/parse_live_query.dart';
4044
part 'src/network/parse_query.dart';
4145
part 'src/objects/parse_acl.dart';
4246
part 'src/objects/parse_base.dart';

lib/src/network/parse_live_query.dart

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import 'dart:async';
2-
import 'dart:convert';
3-
import 'dart:io';
4-
5-
import 'package:connectivity/connectivity.dart';
6-
import 'package:flutter/widgets.dart';
7-
import 'package:web_socket_channel/io.dart';
8-
import 'package:web_socket_channel/web_socket_channel.dart';
9-
10-
import '../../parse_server_sdk.dart';
1+
part of flutter_parse_sdk;
112

123
enum LiveQueryEvent { create, enter, update, leave, delete, error }
134

@@ -31,7 +22,6 @@ class Subscription<T extends ParseObject> {
3122
'error'
3223
];
3324
Map<String, Function> eventCallbacks = <String, Function>{};
34-
3525
void on(LiveQueryEvent op, Function callback) {
3626
eventCallbacks[_liveQueryEvent[op.index]] = callback;
3727
}
@@ -46,9 +36,13 @@ enum LiveQueryClientEvent { CONNECTED, DISCONNECTED, USER_DISCONNECTED }
4636
class LiveQueryReconnectingController with WidgetsBindingObserver {
4737
LiveQueryReconnectingController(
4838
this._reconnect, this._eventStream, this.debug) {
49-
Connectivity().checkConnectivity().then(_connectivityChanged);
50-
Connectivity().onConnectivityChanged.listen(_connectivityChanged);
51-
39+
//Connectivity works differently on web
40+
if (!parseIsWeb) {
41+
Connectivity().checkConnectivity().then(_connectivityChanged);
42+
Connectivity().onConnectivityChanged.listen(_connectivityChanged);
43+
} else {
44+
_connectivityChanged(ConnectivityResult.wifi);
45+
}
5246
_eventStream.listen((LiveQueryClientEvent event) {
5347
switch (event) {
5448
case LiveQueryClientEvent.CONNECTED:
@@ -132,10 +126,9 @@ class LiveQueryReconnectingController with WidgetsBindingObserver {
132126
}
133127
}
134128

135-
class Client {
136-
factory Client() => _getInstance();
137-
138-
Client._internal(
129+
class LiveQueryClient {
130+
factory LiveQueryClient() => _getInstance();
131+
LiveQueryClient._internal(
139132
{bool debug, ParseHTTPClient client, bool autoSendSessionId}) {
140133
_clientEventStreamController = StreamController<LiveQueryClientEvent>();
141134
_clientEventStream =
@@ -160,13 +153,11 @@ class Client {
160153
reconnectingController = LiveQueryReconnectingController(
161154
() => reconnect(userInitialized: false), getClientEventStream, _debug);
162155
}
163-
164-
static Client get instance => _getInstance();
165-
static Client _instance;
166-
167-
static Client _getInstance(
156+
static LiveQueryClient get instance => _getInstance();
157+
static LiveQueryClient _instance;
158+
static LiveQueryClient _getInstance(
168159
{bool debug, ParseHTTPClient client, bool autoSendSessionId}) {
169-
_instance ??= Client._internal(
160+
_instance ??= LiveQueryClient._internal(
170161
debug: debug, client: client, autoSendSessionId: autoSendSessionId);
171162
return _instance;
172163
}
@@ -175,7 +166,7 @@ class Client {
175166
return _clientEventStream;
176167
}
177168

178-
WebSocket _webSocket;
169+
parse_web_socket.WebSocket _webSocket;
179170
ParseHTTPClient _client;
180171
bool _debug;
181172
bool _sendSessionId;
@@ -186,7 +177,6 @@ class Client {
186177
Stream<LiveQueryClientEvent> _clientEventStream;
187178
LiveQueryReconnectingController reconnectingController;
188179

189-
// ignore: always_specify_types
190180
final Map<int, Subscription> _requestSubScription = <int, Subscription>{};
191181

192182
Future<void> reconnect({bool userInitialized = false}) async {
@@ -198,11 +188,12 @@ class Client {
198188
if (_webSocket != null) {
199189
return _webSocket.readyState;
200190
}
201-
return WebSocket.connecting;
191+
return parse_web_socket.WebSocket.CONNECTING;
202192
}
203193

204194
Future<dynamic> disconnect({bool userInitialized = false}) async {
205-
if (_webSocket != null && _webSocket.readyState == WebSocket.open) {
195+
if (_webSocket != null &&
196+
_webSocket.readyState == parse_web_socket.WebSocket.OPEN) {
206197
if (_debug) {
207198
print('$_printConstLiveQuery: Socket closed');
208199
}
@@ -216,7 +207,6 @@ class Client {
216207
await _channel.sink.close();
217208
_channel = null;
218209
}
219-
// ignore: always_specify_types
220210
_requestSubScription.values.toList().forEach((Subscription subscription) {
221211
subscription._enabled = false;
222212
});
@@ -274,9 +264,10 @@ class Client {
274264
_connecting = true;
275265

276266
try {
277-
_webSocket = await WebSocket.connect(_liveQueryURL);
267+
_webSocket = await parse_web_socket.WebSocket.connect(_liveQueryURL);
278268
_connecting = false;
279-
if (_webSocket != null && _webSocket.readyState == WebSocket.open) {
269+
if (_webSocket != null &&
270+
_webSocket.readyState == parse_web_socket.WebSocket.OPEN) {
280271
if (_debug) {
281272
print('$_printConstLiveQuery: Socket opened');
282273
}
@@ -286,7 +277,7 @@ class Client {
286277
}
287278
return Future<void>.value(null);
288279
}
289-
_channel = IOWebSocketChannel(_webSocket);
280+
_channel = _webSocket.createWebSocketChannel();
290281
_channel.stream.listen((dynamic message) {
291282
_handleMessage(message);
292283
}, onDone: () {
@@ -302,8 +293,11 @@ class Client {
302293
print(
303294
'$_printConstLiveQuery: Error: ${error.runtimeType.toString()}');
304295
}
305-
return Future<ParseResponse>.value(handleException(Exception(error),
306-
ParseApiRQ.liveQuery, _debug, 'IOWebSocketChannel'));
296+
return Future<ParseResponse>.value(handleException(
297+
Exception(error),
298+
ParseApiRQ.liveQuery,
299+
_debug,
300+
!parseIsWeb ? 'IOWebSocketChannel' : 'HtmlWebSocketChannel'));
307301
});
308302
} on Exception catch (e) {
309303
_connecting = false;
@@ -341,13 +335,11 @@ class Client {
341335
_channel.sink.add(jsonEncode(connectMessage));
342336
}
343337

344-
// ignore: always_specify_types
345338
void _subscribeLiveQuery(Subscription subscription) {
346339
if (subscription._enabled) {
347340
return;
348341
}
349342
subscription._enabled = true;
350-
// ignore: always_specify_types
351343
final QueryBuilder query = subscription.query;
352344
final List<String> keysToReturn = query.limiters['keys']?.split(',');
353345
query.limiters.clear(); //Remove limits in LiveQuery
@@ -386,11 +378,11 @@ class Client {
386378
}
387379

388380
final Map<String, dynamic> actionData = jsonDecode(message);
389-
// ignore: always_specify_types
381+
390382
Subscription subscription;
391383
if (actionData.containsKey('op') && actionData['op'] == 'connected') {
392384
print('ReSubScription:$_requestSubScription');
393-
// ignore: always_specify_types
385+
394386
_requestSubScription.values.toList().forEach((Subscription subcription) {
395387
_subscribeLiveQuery(subcription);
396388
});
@@ -436,21 +428,17 @@ class LiveQuery {
436428
_debug = isDebugEnabled(objectLevelDebug: debug);
437429
_sendSessionId =
438430
autoSendSessionId ?? ParseCoreData().autoSendSessionId ?? true;
439-
this.client = Client._getInstance(
431+
this.client = LiveQueryClient._getInstance(
440432
client: _client, debug: _debug, autoSendSessionId: _sendSessionId);
441433
}
442434

443435
ParseHTTPClient _client;
444436
bool _debug;
445437
bool _sendSessionId;
446-
447-
// ignore: always_specify_types
448438
Subscription _latestSubscription;
449-
Client client;
439+
LiveQueryClient client;
450440

451-
// ignore: always_specify_types
452441
@deprecated
453-
// ignore: always_specify_types
454442
Future<dynamic> subscribe(QueryBuilder query) async {
455443
_latestSubscription = await client.subscribe(query);
456444
return _latestSubscription;

0 commit comments

Comments
 (0)