@@ -17,6 +17,7 @@ class Subscription<T extends ParseObject> {
17
17
Subscription (this .query, this .requestId, {T copyObject}) {
18
18
_copyObject = copyObject;
19
19
}
20
+
20
21
QueryBuilder <T > query;
21
22
T _copyObject;
22
23
int requestId;
@@ -30,6 +31,7 @@ class Subscription<T extends ParseObject> {
30
31
'error'
31
32
];
32
33
Map <String , Function > eventCallbacks = < String , Function > {};
34
+
33
35
void on (LiveQueryEvent op, Function callback) {
34
36
eventCallbacks[_liveQueryEvent[op.index]] = callback;
35
37
}
@@ -42,21 +44,6 @@ class Subscription<T extends ParseObject> {
42
44
enum LiveQueryClientEvent { CONNECTED , DISCONNECTED , USER_DISCONNECTED }
43
45
44
46
class LiveQueryReconnectingController with WidgetsBindingObserver {
45
- // -1 means "do not try to reconnect",
46
- static const List <int > retryInterval = [0 , 500 , 1000 , 2000 , 5000 , 10000 ];
47
- static const String DEBUG_TAG = 'LiveQueryReconnectingController' ;
48
-
49
- final Function _reconnect;
50
- final Stream <LiveQueryClientEvent > _eventStream;
51
- final bool debug;
52
-
53
- int _retryState = 0 ;
54
- bool _isOnline = false ;
55
- bool _isConnected = false ;
56
- bool _userDisconnected = false ;
57
-
58
- Timer _currentTimer;
59
-
60
47
LiveQueryReconnectingController (
61
48
this ._reconnect, this ._eventStream, this .debug) {
62
49
Connectivity ().checkConnectivity ().then (_connectivityChanged);
@@ -82,15 +69,36 @@ class LiveQueryReconnectingController with WidgetsBindingObserver {
82
69
break ;
83
70
}
84
71
85
- if (debug) print ('$DEBUG_TAG : $event ' );
72
+ if (debug) {
73
+ print ('$DEBUG_TAG : $event ' );
74
+ }
86
75
});
87
76
WidgetsBinding .instance.addObserver (this );
88
77
}
89
78
79
+ // -1 means "do not try to reconnect",
80
+ static const List <int > retryInterval = < int > [0 , 500 , 1000 , 2000 , 5000 , 10000 ];
81
+ static const String DEBUG_TAG = 'LiveQueryReconnectingController' ;
82
+
83
+ final Function _reconnect;
84
+ final Stream <LiveQueryClientEvent > _eventStream;
85
+ final bool debug;
86
+
87
+ int _retryState = 0 ;
88
+ bool _isOnline = false ;
89
+ bool _isConnected = false ;
90
+ bool _userDisconnected = false ;
91
+
92
+ Timer _currentTimer;
93
+
90
94
void _connectivityChanged (ConnectivityResult state) {
91
- if (! _isOnline && state != ConnectivityResult .none) _retryState = 0 ;
95
+ if (! _isOnline && state != ConnectivityResult .none) {
96
+ _retryState = 0 ;
97
+ }
92
98
_isOnline = state != ConnectivityResult .none;
93
- if (debug) print ('$DEBUG_TAG : $state ' );
99
+ if (debug) {
100
+ print ('$DEBUG_TAG : $state ' );
101
+ }
94
102
_setReconnect ();
95
103
}
96
104
@@ -118,13 +126,16 @@ class LiveQueryReconnectingController with WidgetsBindingObserver {
118
126
});
119
127
if (debug)
120
128
print ('$DEBUG_TAG : Retrytimer set to ${retryInterval [_retryState ]}ms' );
121
- if (_retryState < retryInterval.length - 1 ) _retryState++ ;
129
+ if (_retryState < retryInterval.length - 1 ) {
130
+ _retryState++ ;
131
+ }
122
132
}
123
133
}
124
134
}
125
135
126
136
class Client {
127
137
factory Client () => _getInstance ();
138
+
128
139
Client ._internal (
129
140
{bool debug, ParseHTTPClient client, bool autoSendSessionId}) {
130
141
_clientEventStreamController = StreamController <LiveQueryClientEvent >();
@@ -150,8 +161,10 @@ class Client {
150
161
reconnectingController = LiveQueryReconnectingController (
151
162
() => reconnect (userInitialized: false ), getClientEventStream, _debug);
152
163
}
164
+
153
165
static Client get instance => _getInstance ();
154
166
static Client _instance;
167
+
155
168
static Client _getInstance (
156
169
{bool debug, ParseHTTPClient client, bool autoSendSessionId}) {
157
170
_instance ?? = Client ._internal (
@@ -174,6 +187,7 @@ class Client {
174
187
Stream <LiveQueryClientEvent > _clientEventStream;
175
188
LiveQueryReconnectingController reconnectingController;
176
189
190
+ // ignore: always_specify_types
177
191
final Map <int , Subscription > _requestSubScription = < int , Subscription > {};
178
192
179
193
Future <void > reconnect ({bool userInitialized = false }) async {
@@ -203,8 +217,9 @@ class Client {
203
217
await _channel.sink.close ();
204
218
_channel = null ;
205
219
}
206
- _requestSubScription.values.toList ().forEach ((Subscription subcription) {
207
- subcription._enabled = false ;
220
+ // ignore: always_specify_types
221
+ _requestSubScription.values.toList ().forEach ((Subscription subscription) {
222
+ subscription._enabled = false ;
208
223
});
209
224
_connecting = false ;
210
225
if (userInitialized)
@@ -327,12 +342,14 @@ class Client {
327
342
_channel.sink.add (jsonEncode (connectMessage));
328
343
}
329
344
345
+ // ignore: always_specify_types
330
346
void _subscribeLiveQuery (Subscription subscription) {
331
347
if (subscription._enabled) {
332
348
return ;
333
349
}
334
350
subscription._enabled = true ;
335
- QueryBuilder query = subscription.query;
351
+ // ignore: always_specify_types
352
+ final QueryBuilder query = subscription.query;
336
353
final List <String > keysToReturn = query.limiters['keys' ]? .split (',' );
337
354
query.limiters.clear (); //Remove limits in LiveQuery
338
355
final String _where = query.buildQuery ().replaceAll ('where=' , '' );
@@ -370,9 +387,11 @@ class Client {
370
387
}
371
388
372
389
final Map <String , dynamic > actionData = jsonDecode (message);
390
+ // ignore: always_specify_types
373
391
Subscription subscription;
374
392
if (actionData.containsKey ('op' ) && actionData['op' ] == 'connected' ) {
375
393
print ('ReSubScription:$_requestSubScription ' );
394
+ // ignore: always_specify_types
376
395
_requestSubScription.values.toList ().forEach ((Subscription subcription) {
377
396
_subscribeLiveQuery (subcription);
378
397
});
@@ -423,11 +442,14 @@ class LiveQuery {
423
442
ParseHTTPClient _client;
424
443
bool _debug;
425
444
bool _sendSessionId;
445
+
446
+ // ignore: always_specify_types
426
447
Subscription _latestSubscription;
427
448
Client client;
428
449
429
450
// ignore: always_specify_types
430
451
@deprecated
452
+ // ignore: always_specify_types
431
453
Future <dynamic > subscribe (QueryBuilder query) async {
432
454
_latestSubscription = await client.subscribe (query);
433
455
return _latestSubscription;
0 commit comments