@@ -13,14 +13,7 @@ class Subscription<T extends ParseObject> {
13
13
T ? _copyObject;
14
14
int requestId;
15
15
bool _enabled = false ;
16
- final List <String > _liveQueryEvent = < String > [
17
- 'create' ,
18
- 'enter' ,
19
- 'update' ,
20
- 'leave' ,
21
- 'delete' ,
22
- 'error'
23
- ];
16
+ final List <String > _liveQueryEvent = < String > ['create' , 'enter' , 'update' , 'leave' , 'delete' , 'error' ];
24
17
Map <String , Function > eventCallbacks = < String , Function > {};
25
18
26
19
void on (LiveQueryEvent op, Function callback) {
@@ -40,14 +33,12 @@ class LiveQueryReconnectingController {
40
33
this ._eventStream,
41
34
this .debug,
42
35
) {
43
- final ParseConnectivityProvider ? connectivityProvider =
44
- ParseCoreData ().connectivityProvider;
36
+ final ParseConnectivityProvider ? connectivityProvider = ParseCoreData ().connectivityProvider;
45
37
if (connectivityProvider != null ) {
46
38
connectivityProvider.checkConnectivity ().then (_connectivityChanged);
47
39
connectivityProvider.connectivityStream.listen (_connectivityChanged);
48
40
} else {
49
- print (
50
- 'LiveQuery does not work, if there is no ParseConnectivityProvider provided.' );
41
+ print ('LiveQuery does not work, if there is no ParseConnectivityProvider provided.' );
51
42
}
52
43
_eventStream.listen ((LiveQueryClientEvent event) {
53
44
switch (event) {
@@ -106,13 +97,8 @@ class LiveQueryReconnectingController {
106
97
}
107
98
108
99
void _setReconnect () {
109
- if (_isOnline &&
110
- ! _isConnected &&
111
- _currentTimer == null &&
112
- ! _userDisconnected &&
113
- retryInterval[_retryState] >= 0 ) {
114
- _currentTimer =
115
- Timer (Duration (milliseconds: retryInterval[_retryState]), () {
100
+ if (_isOnline && ! _isConnected && _currentTimer == null && ! _userDisconnected && retryInterval[_retryState] >= 0 ) {
101
+ _currentTimer = Timer (Duration (milliseconds: retryInterval[_retryState]), () {
116
102
_currentTimer = null ;
117
103
_reconnect ();
118
104
});
@@ -129,17 +115,14 @@ class LiveQueryReconnectingController {
129
115
class LiveQueryClient {
130
116
factory LiveQueryClient () => _getInstance ();
131
117
132
- LiveQueryClient ._internal (this ._liveQueryURL,
133
- {bool ? debug, bool ? autoSendSessionId}) {
118
+ LiveQueryClient ._internal (this ._liveQueryURL, {bool ? debug, bool ? autoSendSessionId}) {
134
119
_clientEventStreamController = StreamController <LiveQueryClientEvent >();
135
- _clientEventStream =
136
- _clientEventStreamController.stream.asBroadcastStream ();
120
+ _clientEventStream = _clientEventStreamController.stream.asBroadcastStream ();
137
121
138
122
_debug = isDebugEnabled (objectLevelDebug: debug);
139
123
_sendSessionId = autoSendSessionId ?? ParseCoreData ().autoSendSessionId;
140
124
141
- reconnectingController = LiveQueryReconnectingController (
142
- () => reconnect (userInitialized: false ), getClientEventStream, _debug);
125
+ reconnectingController = LiveQueryReconnectingController (() => reconnect (userInitialized: false ), getClientEventStream, _debug);
143
126
}
144
127
145
128
static LiveQueryClient get instance => _getInstance ();
@@ -148,8 +131,7 @@ class LiveQueryClient {
148
131
static LiveQueryClient _getInstance ({bool ? debug, bool ? autoSendSessionId}) {
149
132
String ? liveQueryURL = ParseCoreData ().liveQueryURL;
150
133
if (liveQueryURL == null ) {
151
- assert (false ,
152
- 'liveQueryUrl is not set. For how to setup Live Queries, see https://github.com/parse-community/Parse-SDK-Flutter/tree/master/packages/flutter#live-queries.' );
134
+ assert (false , 'liveQueryUrl is not set. For how to setup Live Queries, see https://github.com/parse-community/Parse-SDK-Flutter/tree/master/packages/flutter#live-queries.' );
153
135
liveQueryURL = "" ;
154
136
} else {
155
137
if (liveQueryURL.contains ('https' )) {
@@ -158,9 +140,7 @@ class LiveQueryClient {
158
140
liveQueryURL = liveQueryURL.replaceAll ('http' , 'ws' );
159
141
}
160
142
}
161
- LiveQueryClient instance = _instance ??
162
- LiveQueryClient ._internal (liveQueryURL,
163
- debug: debug, autoSendSessionId: autoSendSessionId);
143
+ LiveQueryClient instance = _instance ?? LiveQueryClient ._internal (liveQueryURL, debug: debug, autoSendSessionId: autoSendSessionId);
164
144
_instance ?? = instance;
165
145
return instance;
166
146
}
@@ -197,8 +177,7 @@ class LiveQueryClient {
197
177
198
178
Future <dynamic > disconnect ({bool userInitialized = false }) async {
199
179
parse_web_socket.WebSocket ? webSocket = _webSocket;
200
- if (webSocket != null &&
201
- webSocket.readyState == parse_web_socket.WebSocket .open) {
180
+ if (webSocket != null && webSocket.readyState == parse_web_socket.WebSocket .open) {
202
181
if (_debug) {
203
182
print ('$_printConstLiveQuery : Socket closed' );
204
183
}
@@ -218,21 +197,16 @@ class LiveQueryClient {
218
197
});
219
198
_connecting = false ;
220
199
if (userInitialized) {
221
- _clientEventStreamController.sink
222
- .add (LiveQueryClientEvent .userDisconnected);
200
+ _clientEventStreamController.sink.add (LiveQueryClientEvent .userDisconnected);
223
201
}
224
202
}
225
203
226
- Future <Subscription <T >> subscribe <T extends ParseObject >(
227
- QueryBuilder <T > query,
228
- {T ? copyObject}) async {
204
+ Future <Subscription <T >> subscribe <T extends ParseObject >(QueryBuilder <T > query, {T ? copyObject}) async {
229
205
if (_webSocket == null ) {
230
- await _clientEventStream.any ((LiveQueryClientEvent event) =>
231
- event == LiveQueryClientEvent .connected);
206
+ await _clientEventStream.any ((LiveQueryClientEvent event) => event == LiveQueryClientEvent .connected);
232
207
}
233
208
final int requestId = _requestIdGenerator ();
234
- final Subscription <T > subscription =
235
- Subscription <T >(query, requestId, copyObject: copyObject);
209
+ final Subscription <T > subscription = Subscription <T >(query, requestId, copyObject: copyObject);
236
210
_requestSubscription[requestId] = subscription;
237
211
//After a client connects to the LiveQuery server,
238
212
//it can send a subscribe message to subscribe a ParseQuery.
@@ -272,8 +246,7 @@ class LiveQueryClient {
272
246
_connecting = true ;
273
247
274
248
try {
275
- parse_web_socket.WebSocket webSocket =
276
- await parse_web_socket.WebSocket .connect (_liveQueryURL);
249
+ parse_web_socket.WebSocket webSocket = await parse_web_socket.WebSocket .connect (_liveQueryURL);
277
250
_webSocket = webSocket;
278
251
_connecting = false ;
279
252
if (webSocket.readyState == parse_web_socket.WebSocket .open) {
@@ -293,23 +266,16 @@ class LiveQueryClient {
293
266
294
267
chanelStream? .sink.add (message);
295
268
}, onDone: () {
296
- _clientEventStreamController.sink
297
- .add (LiveQueryClientEvent .disconnected);
269
+ _clientEventStreamController.sink.add (LiveQueryClientEvent .disconnected);
298
270
if (_debug) {
299
271
print ('$_printConstLiveQuery : Done' );
300
272
}
301
273
}, onError: (Object error) {
302
- _clientEventStreamController.sink
303
- .add (LiveQueryClientEvent .disconnected);
274
+ _clientEventStreamController.sink.add (LiveQueryClientEvent .disconnected);
304
275
if (_debug) {
305
- print (
306
- '$_printConstLiveQuery : Error: ${error .runtimeType .toString ()}' );
276
+ print ('$_printConstLiveQuery : Error: ${error .runtimeType .toString ()}' );
307
277
}
308
- return Future <ParseResponse >.value (handleException (
309
- Exception (error),
310
- ParseApiRQ .liveQuery,
311
- _debug,
312
- ! parseIsWeb ? 'IOWebSocketChannel' : 'HtmlWebSocketChannel' ));
278
+ return Future <ParseResponse >.value (handleException (Exception (error), ParseApiRQ .liveQuery, _debug, ! parseIsWeb ? 'IOWebSocketChannel' : 'HtmlWebSocketChannel' ));
313
279
});
314
280
} on Exception catch (e) {
315
281
_connecting = false ;
@@ -328,10 +294,7 @@ class LiveQueryClient {
328
294
}
329
295
//The connect message is sent from a client to the LiveQuery server.
330
296
//It should be the first message sent from a client after the WebSocket connection is established.
331
- final Map <String , String > connectMessage = < String , String > {
332
- 'op' : 'connect' ,
333
- 'applicationId' : ParseCoreData ().applicationId
334
- };
297
+ final Map <String , String > connectMessage = < String , String > {'op' : 'connect' , 'applicationId' : ParseCoreData ().applicationId};
335
298
336
299
if (_sendSessionId) {
337
300
String ? sessionId = ParseCoreData ().sessionId;
@@ -350,8 +313,7 @@ class LiveQueryClient {
350
313
connectMessage['masterKey' ] = masterKey;
351
314
}
352
315
353
- String ? parseInstallation =
354
- (await ParseInstallation .currentInstallation ()).installationId;
316
+ String ? parseInstallation = (await ParseInstallation .currentInstallation ()).installationId;
355
317
if (parseInstallation != null ) {
356
318
connectMessage['installationId' ] = parseInstallation;
357
319
}
@@ -381,12 +343,7 @@ class LiveQueryClient {
381
343
final Map <String , dynamic > subscribeMessage = < String , dynamic > {
382
344
'op' : 'subscribe' ,
383
345
'requestId' : subscription.requestId,
384
- 'query' : < String , dynamic > {
385
- 'className' : query.object.parseClassName,
386
- 'where' : whereMap,
387
- if (keysToReturn != null && keysToReturn.isNotEmpty)
388
- 'fields' : keysToReturn
389
- }
346
+ 'query' : < String , dynamic > {'className' : query.object.parseClassName, 'where' : whereMap, if (keysToReturn != null && keysToReturn.isNotEmpty) 'fields' : keysToReturn}
390
347
};
391
348
if (_sendSessionId && ParseCoreData ().sessionId != null ) {
392
349
subscribeMessage['sessionToken' ] = ParseCoreData ().sessionId;
@@ -430,13 +387,9 @@ class LiveQueryClient {
430
387
final String ? className = map['className' ];
431
388
if (className != null ) {
432
389
if (className == keyClassUser) {
433
- eventCallback ((subscription.copyObject ??
434
- ParseCoreData .instance.createParseUser (null , null , null ))
435
- .fromJson (map));
390
+ eventCallback ((subscription.copyObject ?? ParseCoreData .instance.createParseUser (null , null , null )).fromJson (map));
436
391
} else {
437
- eventCallback ((subscription.copyObject ??
438
- ParseCoreData .instance.createObject (className))
439
- .fromJson (map));
392
+ eventCallback ((subscription.copyObject ?? ParseCoreData .instance.createObject (className)).fromJson (map));
440
393
}
441
394
}
442
395
} else {
@@ -451,8 +404,7 @@ class LiveQuery {
451
404
LiveQuery ({bool ? debug, bool ? autoSendSessionId}) {
452
405
_debug = isDebugEnabled (objectLevelDebug: debug);
453
406
_sendSessionId = autoSendSessionId ?? ParseCoreData ().autoSendSessionId;
454
- client = LiveQueryClient ._getInstance (
455
- debug: _debug, autoSendSessionId: _sendSessionId);
407
+ client = LiveQueryClient ._getInstance (debug: _debug, autoSendSessionId: _sendSessionId);
456
408
}
457
409
458
410
bool ? _debug;
0 commit comments