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;
11
2
12
3
enum LiveQueryEvent { create, enter, update, leave, delete, error }
13
4
@@ -31,7 +22,6 @@ class Subscription<T extends ParseObject> {
31
22
'error'
32
23
];
33
24
Map <String , Function > eventCallbacks = < String , Function > {};
34
-
35
25
void on (LiveQueryEvent op, Function callback) {
36
26
eventCallbacks[_liveQueryEvent[op.index]] = callback;
37
27
}
@@ -46,9 +36,13 @@ enum LiveQueryClientEvent { CONNECTED, DISCONNECTED, USER_DISCONNECTED }
46
36
class LiveQueryReconnectingController with WidgetsBindingObserver {
47
37
LiveQueryReconnectingController (
48
38
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
+ }
52
46
_eventStream.listen ((LiveQueryClientEvent event) {
53
47
switch (event) {
54
48
case LiveQueryClientEvent .CONNECTED :
@@ -132,10 +126,9 @@ class LiveQueryReconnectingController with WidgetsBindingObserver {
132
126
}
133
127
}
134
128
135
- class Client {
136
- factory Client () => _getInstance ();
137
-
138
- Client ._internal (
129
+ class LiveQueryClient {
130
+ factory LiveQueryClient () => _getInstance ();
131
+ LiveQueryClient ._internal (
139
132
{bool debug, ParseHTTPClient client, bool autoSendSessionId}) {
140
133
_clientEventStreamController = StreamController <LiveQueryClientEvent >();
141
134
_clientEventStream =
@@ -160,13 +153,11 @@ class Client {
160
153
reconnectingController = LiveQueryReconnectingController (
161
154
() => reconnect (userInitialized: false ), getClientEventStream, _debug);
162
155
}
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 (
168
159
{bool debug, ParseHTTPClient client, bool autoSendSessionId}) {
169
- _instance ?? = Client ._internal (
160
+ _instance ?? = LiveQueryClient ._internal (
170
161
debug: debug, client: client, autoSendSessionId: autoSendSessionId);
171
162
return _instance;
172
163
}
@@ -175,7 +166,7 @@ class Client {
175
166
return _clientEventStream;
176
167
}
177
168
178
- WebSocket _webSocket;
169
+ parse_web_socket. WebSocket _webSocket;
179
170
ParseHTTPClient _client;
180
171
bool _debug;
181
172
bool _sendSessionId;
@@ -186,7 +177,6 @@ class Client {
186
177
Stream <LiveQueryClientEvent > _clientEventStream;
187
178
LiveQueryReconnectingController reconnectingController;
188
179
189
- // ignore: always_specify_types
190
180
final Map <int , Subscription > _requestSubScription = < int , Subscription > {};
191
181
192
182
Future <void > reconnect ({bool userInitialized = false }) async {
@@ -198,11 +188,12 @@ class Client {
198
188
if (_webSocket != null ) {
199
189
return _webSocket.readyState;
200
190
}
201
- return WebSocket .connecting ;
191
+ return parse_web_socket. WebSocket .CONNECTING ;
202
192
}
203
193
204
194
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 ) {
206
197
if (_debug) {
207
198
print ('$_printConstLiveQuery : Socket closed' );
208
199
}
@@ -216,7 +207,6 @@ class Client {
216
207
await _channel.sink.close ();
217
208
_channel = null ;
218
209
}
219
- // ignore: always_specify_types
220
210
_requestSubScription.values.toList ().forEach ((Subscription subscription) {
221
211
subscription._enabled = false ;
222
212
});
@@ -274,9 +264,10 @@ class Client {
274
264
_connecting = true ;
275
265
276
266
try {
277
- _webSocket = await WebSocket .connect (_liveQueryURL);
267
+ _webSocket = await parse_web_socket. WebSocket .connect (_liveQueryURL);
278
268
_connecting = false ;
279
- if (_webSocket != null && _webSocket.readyState == WebSocket .open) {
269
+ if (_webSocket != null &&
270
+ _webSocket.readyState == parse_web_socket.WebSocket .OPEN ) {
280
271
if (_debug) {
281
272
print ('$_printConstLiveQuery : Socket opened' );
282
273
}
@@ -286,7 +277,7 @@ class Client {
286
277
}
287
278
return Future <void >.value (null );
288
279
}
289
- _channel = IOWebSocketChannel ( _webSocket);
280
+ _channel = _webSocket. createWebSocketChannel ( );
290
281
_channel.stream.listen ((dynamic message) {
291
282
_handleMessage (message);
292
283
}, onDone: () {
@@ -302,8 +293,11 @@ class Client {
302
293
print (
303
294
'$_printConstLiveQuery : Error: ${error .runtimeType .toString ()}' );
304
295
}
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' ));
307
301
});
308
302
} on Exception catch (e) {
309
303
_connecting = false ;
@@ -341,13 +335,11 @@ class Client {
341
335
_channel.sink.add (jsonEncode (connectMessage));
342
336
}
343
337
344
- // ignore: always_specify_types
345
338
void _subscribeLiveQuery (Subscription subscription) {
346
339
if (subscription._enabled) {
347
340
return ;
348
341
}
349
342
subscription._enabled = true ;
350
- // ignore: always_specify_types
351
343
final QueryBuilder query = subscription.query;
352
344
final List <String > keysToReturn = query.limiters['keys' ]? .split (',' );
353
345
query.limiters.clear (); //Remove limits in LiveQuery
@@ -386,11 +378,11 @@ class Client {
386
378
}
387
379
388
380
final Map <String , dynamic > actionData = jsonDecode (message);
389
- // ignore: always_specify_types
381
+
390
382
Subscription subscription;
391
383
if (actionData.containsKey ('op' ) && actionData['op' ] == 'connected' ) {
392
384
print ('ReSubScription:$_requestSubScription ' );
393
- // ignore: always_specify_types
385
+
394
386
_requestSubScription.values.toList ().forEach ((Subscription subcription) {
395
387
_subscribeLiveQuery (subcription);
396
388
});
@@ -436,21 +428,17 @@ class LiveQuery {
436
428
_debug = isDebugEnabled (objectLevelDebug: debug);
437
429
_sendSessionId =
438
430
autoSendSessionId ?? ParseCoreData ().autoSendSessionId ?? true ;
439
- this .client = Client ._getInstance (
431
+ this .client = LiveQueryClient ._getInstance (
440
432
client: _client, debug: _debug, autoSendSessionId: _sendSessionId);
441
433
}
442
434
443
435
ParseHTTPClient _client;
444
436
bool _debug;
445
437
bool _sendSessionId;
446
-
447
- // ignore: always_specify_types
448
438
Subscription _latestSubscription;
449
- Client client;
439
+ LiveQueryClient client;
450
440
451
- // ignore: always_specify_types
452
441
@deprecated
453
- // ignore: always_specify_types
454
442
Future <dynamic > subscribe (QueryBuilder query) async {
455
443
_latestSubscription = await client.subscribe (query);
456
444
return _latestSubscription;
0 commit comments