@@ -13,9 +13,12 @@ enum LiveQueryEvent { create, enter, update, leave, delete, error }
13
13
14
14
const String _printConstLiveQuery = 'LiveQuery: ' ;
15
15
16
- class Subscription {
17
- Subscription (this .query, this .requestId);
18
- QueryBuilder query;
16
+ class Subscription <T extends ParseObject > {
17
+ Subscription (this .query, this .requestId, {T copyObject}) {
18
+ _copyObject = copyObject;
19
+ }
20
+ QueryBuilder <T > query;
21
+ T _copyObject;
19
22
int requestId;
20
23
bool _enabled = false ;
21
24
final List <String > _liveQueryEvent = < String > [
@@ -30,6 +33,10 @@ class Subscription {
30
33
void on (LiveQueryEvent op, Function callback) {
31
34
eventCallbacks[_liveQueryEvent[op.index]] = callback;
32
35
}
36
+
37
+ T get copyObject {
38
+ return _copyObject;
39
+ }
33
40
}
34
41
35
42
enum LiveQueryClientEvent { CONNECTED , DISCONNECTED , USER_DISCONNECTED }
@@ -52,7 +59,6 @@ class LiveQueryReconnectingController with WidgetsBindingObserver {
52
59
53
60
LiveQueryReconnectingController (
54
61
this ._reconnect, this ._eventStream, this .debug) {
55
-
56
62
Connectivity ().checkConnectivity ().then (_connectivityChanged);
57
63
Connectivity ().onConnectivityChanged.listen (_connectivityChanged);
58
64
@@ -81,7 +87,7 @@ class LiveQueryReconnectingController with WidgetsBindingObserver {
81
87
WidgetsBinding .instance.addObserver (this );
82
88
}
83
89
84
- void _connectivityChanged (ConnectivityResult state){
90
+ void _connectivityChanged (ConnectivityResult state) {
85
91
if (! _isOnline && state != ConnectivityResult .none) _retryState = 0 ;
86
92
_isOnline = state != ConnectivityResult .none;
87
93
if (debug) print ('$DEBUG_TAG : $state ' );
@@ -107,9 +113,9 @@ class LiveQueryReconnectingController with WidgetsBindingObserver {
107
113
retryInterval[_retryState] >= 0 ) {
108
114
_currentTimer =
109
115
Timer (Duration (milliseconds: retryInterval[_retryState]), () {
110
- _currentTimer = null ;
111
- _reconnect ();
112
- });
116
+ _currentTimer = null ;
117
+ _reconnect ();
118
+ });
113
119
if (debug)
114
120
print ('$DEBUG_TAG : Retrytimer set to ${retryInterval [_retryState ]}ms' );
115
121
if (_retryState < retryInterval.length - 1 ) _retryState++ ;
@@ -128,7 +134,7 @@ class Client {
128
134
_client = client ??
129
135
ParseHTTPClient (
130
136
sendSessionId:
131
- autoSendSessionId ?? ParseCoreData ().autoSendSessionId,
137
+ autoSendSessionId ?? ParseCoreData ().autoSendSessionId,
132
138
securityContext: ParseCoreData ().securityContext);
133
139
134
140
_debug = isDebugEnabled (objectLevelDebug: debug);
@@ -142,7 +148,7 @@ class Client {
142
148
}
143
149
144
150
reconnectingController = LiveQueryReconnectingController (
145
- () => reconnect (userInitialized: false ), getClientEventStream, _debug);
151
+ () => reconnect (userInitialized: false ), getClientEventStream, _debug);
146
152
}
147
153
static Client get instance => _getInstance ();
148
154
static Client _instance;
@@ -206,21 +212,24 @@ class Client {
206
212
.add (LiveQueryClientEvent .USER_DISCONNECTED );
207
213
}
208
214
209
- Future <Subscription > subscribe (QueryBuilder query) async {
215
+ Future <Subscription <T >> subscribe <T extends ParseObject >(
216
+ QueryBuilder <T > query,
217
+ {T copyObject}) async {
210
218
if (_webSocket == null ) {
211
219
await _clientEventStream.any ((LiveQueryClientEvent event) =>
212
- event == LiveQueryClientEvent .CONNECTED );
220
+ event == LiveQueryClientEvent .CONNECTED );
213
221
}
214
222
final int requestId = _requestIdGenerator ();
215
- final Subscription subscription = Subscription (query, requestId);
223
+ final Subscription <T > subscription =
224
+ Subscription <T >(query, requestId, copyObject: copyObject);
216
225
_requestSubScription[requestId] = subscription;
217
226
//After a client connects to the LiveQuery server,
218
227
//it can send a subscribe message to subscribe a ParseQuery.
219
228
_subscribeLiveQuery (subscription);
220
229
return subscription;
221
230
}
222
231
223
- void unSubscribe (Subscription subscription) {
232
+ void unSubscribe < T extends ParseObject > (Subscription < T > subscription) {
224
233
//Mount message for Unsubscribe
225
234
final Map <String , dynamic > unsubscribeMessage = < String , dynamic > {
226
235
'op' : 'unsubscribe' ,
@@ -382,10 +391,12 @@ class Client {
382
391
final String className = map['className' ];
383
392
if (className == '_User' ) {
384
393
subscription.eventCallbacks[actionData['op' ]](
385
- ParseUser (null , null , null ).fromJson (map));
394
+ (subscription.copyObject ?? ParseUser (null , null , null ))
395
+ .fromJson (map));
386
396
} else {
387
397
subscription.eventCallbacks[actionData['op' ]](
388
- ParseObject (className).fromJson (map));
398
+ (subscription.copyObject ?? ParseObject (className))
399
+ .fromJson (map));
389
400
}
390
401
} else {
391
402
subscription.eventCallbacks[actionData['op' ]](actionData);
@@ -399,7 +410,7 @@ class LiveQuery {
399
410
_client = client ??
400
411
ParseHTTPClient (
401
412
sendSessionId:
402
- autoSendSessionId ?? ParseCoreData ().autoSendSessionId,
413
+ autoSendSessionId ?? ParseCoreData ().autoSendSessionId,
403
414
securityContext: ParseCoreData ().securityContext);
404
415
405
416
_debug = isDebugEnabled (objectLevelDebug: debug);
0 commit comments