@@ -61,8 +61,9 @@ class LiveQueryReconnectingController {
61
61
break ;
62
62
case LiveQueryClientEvent .USER_DISCONNECTED :
63
63
_userDisconnected = true ;
64
- if (_currentTimer != null ) {
65
- _currentTimer! .cancel ();
64
+ Timer ? currentTimer = _currentTimer;
65
+ if (currentTimer != null ) {
66
+ currentTimer.cancel ();
66
67
_currentTimer = null ;
67
68
}
68
69
break ;
@@ -126,33 +127,38 @@ class LiveQueryReconnectingController {
126
127
class LiveQueryClient {
127
128
factory LiveQueryClient () => _getInstance ();
128
129
129
- LiveQueryClient ._internal ({bool ? debug, bool ? autoSendSessionId}) {
130
+ LiveQueryClient ._internal (this ._liveQueryURL,
131
+ {bool ? debug, bool ? autoSendSessionId}) {
130
132
_clientEventStreamController = StreamController <LiveQueryClientEvent >();
131
133
_clientEventStream =
132
134
_clientEventStreamController.stream.asBroadcastStream ();
133
135
134
136
_debug = isDebugEnabled (objectLevelDebug: debug);
135
- _sendSessionId =
136
- autoSendSessionId ?? ParseCoreData ().autoSendSessionId;
137
- _liveQueryURL = ParseCoreData ().liveQueryURL;
138
- assert (_liveQueryURL != null ,
139
- '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.' );
140
- if (_liveQueryURL! .contains ('https' )) {
141
- _liveQueryURL = _liveQueryURL! .replaceAll ('https' , 'wss' );
142
- } else if (_liveQueryURL! .contains ('http' )) {
143
- _liveQueryURL = _liveQueryURL! .replaceAll ('http' , 'ws' );
144
- }
137
+ _sendSessionId = autoSendSessionId ?? ParseCoreData ().autoSendSessionId;
145
138
146
139
reconnectingController = LiveQueryReconnectingController (
147
140
() => reconnect (userInitialized: false ), getClientEventStream, _debug);
148
141
}
149
142
static LiveQueryClient get instance => _getInstance ();
150
143
static LiveQueryClient ? _instance;
151
- static LiveQueryClient _getInstance (
152
- {bool ? debug, bool ? autoSendSessionId}) {
153
- _instance ?? = LiveQueryClient ._internal (
154
- debug: debug, autoSendSessionId: autoSendSessionId);
155
- return _instance! ;
144
+ static LiveQueryClient _getInstance ({bool ? debug, bool ? autoSendSessionId}) {
145
+ String ? liveQueryURL = ParseCoreData ().liveQueryURL;
146
+ if (liveQueryURL == null ) {
147
+ assert (false ,
148
+ '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.' );
149
+ liveQueryURL = "" ;
150
+ } else {
151
+ if (liveQueryURL.contains ('https' )) {
152
+ liveQueryURL = liveQueryURL.replaceAll ('https' , 'wss' );
153
+ } else if (liveQueryURL.contains ('http' )) {
154
+ liveQueryURL = liveQueryURL.replaceAll ('http' , 'ws' );
155
+ }
156
+ }
157
+ LiveQueryClient instance = _instance ??
158
+ LiveQueryClient ._internal (liveQueryURL,
159
+ debug: debug, autoSendSessionId: autoSendSessionId);
160
+ _instance ?? = instance;
161
+ return instance;
156
162
}
157
163
158
164
Stream <LiveQueryClientEvent > get getClientEventStream {
@@ -163,7 +169,7 @@ class LiveQueryClient {
163
169
late bool _debug;
164
170
late bool _sendSessionId;
165
171
WebSocketChannel ? _channel;
166
- String ? _liveQueryURL;
172
+ final String _liveQueryURL;
167
173
bool _connecting = false ;
168
174
late StreamController <LiveQueryClientEvent > _clientEventStreamController;
169
175
late Stream <LiveQueryClientEvent > _clientEventStream;
@@ -177,26 +183,29 @@ class LiveQueryClient {
177
183
}
178
184
179
185
int readyState () {
180
- if (_webSocket != null ) {
181
- return _webSocket! .readyState;
186
+ parse_web_socket.WebSocket ? webSocket = _webSocket;
187
+ if (webSocket != null ) {
188
+ return webSocket.readyState;
182
189
}
183
190
return parse_web_socket.WebSocket .CONNECTING ;
184
191
}
185
192
186
193
Future <dynamic > disconnect ({bool userInitialized = false }) async {
187
- if (_webSocket != null &&
188
- _webSocket! .readyState == parse_web_socket.WebSocket .OPEN ) {
194
+ parse_web_socket.WebSocket ? webSocket = _webSocket;
195
+ if (webSocket != null &&
196
+ webSocket.readyState == parse_web_socket.WebSocket .OPEN ) {
189
197
if (_debug) {
190
198
print ('$_printConstLiveQuery : Socket closed' );
191
199
}
192
- await _webSocket ! .close ();
200
+ await webSocket .close ();
193
201
_webSocket = null ;
194
202
}
195
- if (_channel != null && _channel! .sink != null ) {
203
+ WebSocketChannel ? channel = _channel;
204
+ if (channel != null ) {
196
205
if (_debug) {
197
206
print ('$_printConstLiveQuery : close' );
198
207
}
199
- await _channel ! .sink.close ();
208
+ await channel .sink.close ();
200
209
_channel = null ;
201
210
}
202
211
_requestSubscription.values.toList ().forEach ((Subscription subscription) {
@@ -231,11 +240,12 @@ class LiveQueryClient {
231
240
'op' : 'unsubscribe' ,
232
241
'requestId' : subscription.requestId,
233
242
};
234
- if (_channel != null && _channel! .sink != null ) {
243
+ WebSocketChannel ? channel = _channel;
244
+ if (channel != null ) {
235
245
if (_debug) {
236
246
print ('$_printConstLiveQuery : UnsubscribeMessage: $unsubscribeMessage ' );
237
247
}
238
- _channel ! .sink.add (jsonEncode (unsubscribeMessage));
248
+ channel .sink.add (jsonEncode (unsubscribeMessage));
239
249
subscription._enabled = false ;
240
250
_requestSubscription.remove (subscription.requestId);
241
251
}
@@ -256,10 +266,11 @@ class LiveQueryClient {
256
266
_connecting = true ;
257
267
258
268
try {
259
- _webSocket = await parse_web_socket.WebSocket .connect (_liveQueryURL! );
269
+ parse_web_socket.WebSocket webSocket =
270
+ await parse_web_socket.WebSocket .connect (_liveQueryURL);
271
+ _webSocket = webSocket;
260
272
_connecting = false ;
261
- if (_webSocket != null &&
262
- _webSocket! .readyState == parse_web_socket.WebSocket .OPEN ) {
273
+ if (webSocket.readyState == parse_web_socket.WebSocket .OPEN ) {
263
274
if (_debug) {
264
275
print ('$_printConstLiveQuery : Socket opened' );
265
276
}
@@ -269,8 +280,9 @@ class LiveQueryClient {
269
280
}
270
281
return Future <void >.value (null );
271
282
}
272
- _channel = _webSocket! .createWebSocketChannel ();
273
- _channel! .stream.listen ((dynamic message) {
283
+ WebSocketChannel channel = webSocket.createWebSocketChannel ();
284
+ _channel = channel;
285
+ channel.stream.listen ((dynamic message) {
274
286
_handleMessage (message);
275
287
}, onDone: () {
276
288
_clientEventStreamController.sink
@@ -302,7 +314,8 @@ class LiveQueryClient {
302
314
}
303
315
304
316
void _connectLiveQuery () {
305
- if (_channel == null || _channel! .sink == null ) {
317
+ WebSocketChannel ? channel = _channel;
318
+ if (channel == null ) {
306
319
return ;
307
320
}
308
321
//The connect message is sent from a client to the LiveQuery server.
@@ -312,19 +325,21 @@ class LiveQueryClient {
312
325
'applicationId' : ParseCoreData ().applicationId
313
326
};
314
327
315
- if (_sendSessionId && ParseCoreData ().sessionId != null ) {
316
- connectMessage['sessionToken' ] = ParseCoreData ().sessionId! ;
328
+ if (_sendSessionId) {
329
+ String ? sessionId = ParseCoreData ().sessionId;
330
+ if (sessionId != null ) {
331
+ connectMessage['sessionToken' ] = sessionId;
332
+ }
317
333
}
318
-
319
- if (ParseCoreData ().clientKey != null )
320
- connectMessage['clientKey' ] = ParseCoreData ().clientKey! ;
321
- if (ParseCoreData ().masterKey != null )
322
- connectMessage['masterKey' ] = ParseCoreData ().masterKey! ;
334
+ String ? clientKey = ParseCoreData ().clientKey;
335
+ String ? masterKey = ParseCoreData ().masterKey;
336
+ if (clientKey != null ) connectMessage['clientKey' ] = clientKey;
337
+ if (masterKey != null ) connectMessage['masterKey' ] = masterKey;
323
338
324
339
if (_debug) {
325
340
print ('$_printConstLiveQuery : ConnectMessage: $connectMessage ' );
326
341
}
327
- _channel ! .sink.add (jsonEncode (connectMessage));
342
+ channel .sink.add (jsonEncode (connectMessage));
328
343
}
329
344
330
345
void _subscribeLiveQuery (Subscription subscription) {
@@ -347,7 +362,7 @@ class LiveQueryClient {
347
362
'op' : 'subscribe' ,
348
363
'requestId' : subscription.requestId,
349
364
'query' : < String , dynamic > {
350
- 'className' : query.object! .parseClassName,
365
+ 'className' : query.object.parseClassName,
351
366
'where' : _whereMap,
352
367
if (keysToReturn != null && keysToReturn.isNotEmpty)
353
368
'fields' : keysToReturn
@@ -361,7 +376,7 @@ class LiveQueryClient {
361
376
print ('$_printConstLiveQuery : SubscribeMessage: $subscribeMessage ' );
362
377
}
363
378
364
- _channel! .sink.add (jsonEncode (subscribeMessage));
379
+ _channel? .sink.add (jsonEncode (subscribeMessage));
365
380
}
366
381
367
382
void _handleMessage (String message) {
@@ -388,22 +403,25 @@ class LiveQueryClient {
388
403
return ;
389
404
}
390
405
if (subscription.eventCallbacks.containsKey (actionData['op' ])) {
391
- if (actionData.containsKey ('object' )) {
392
- final Map <String , dynamic > map = actionData['object' ];
393
- final String ? className = map['className' ];
394
- if (className == keyClassUser) {
395
- subscription.eventCallbacks[actionData['op' ]]! (
396
- (subscription.copyObject ??
406
+ Function ? eventCallback = subscription.eventCallbacks[actionData['op' ]];
407
+ if (eventCallback != null ) {
408
+ if (actionData.containsKey ('object' )) {
409
+ final Map <String , dynamic > map = actionData['object' ];
410
+ final String ? className = map['className' ];
411
+ if (className != null ) {
412
+ if (className == keyClassUser) {
413
+ eventCallback ((subscription.copyObject ??
397
414
ParseCoreData .instance.createParseUser (null , null , null ))
398
415
.fromJson (map));
399
- } else {
400
- subscription.eventCallbacks[actionData['op' ]]! (
401
- (subscription.copyObject ??
402
- ParseCoreData .instance.createObject (className! ))
416
+ } else {
417
+ eventCallback ((subscription.copyObject ??
418
+ ParseCoreData .instance.createObject (className))
403
419
.fromJson (map));
420
+ }
421
+ }
422
+ } else {
423
+ eventCallback (actionData);
404
424
}
405
- } else {
406
- subscription.eventCallbacks[actionData['op' ]]! (actionData);
407
425
}
408
426
}
409
427
}
0 commit comments