parse 4.0.1 → 4.1.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (251) hide show
  1. package/dist/parse.js +17995 -26193
  2. package/dist/parse.min.js +2 -2
  3. package/dist/parse.weapp.js +8739 -17728
  4. package/dist/parse.weapp.min.js +2 -2
  5. package/lib/browser/Analytics.js +2 -20
  6. package/lib/browser/AnonymousUtils.js +6 -35
  7. package/lib/browser/Cloud.js +12 -70
  8. package/lib/browser/CloudCode.js +0 -3
  9. package/lib/browser/CoreManager.js +46 -184
  10. package/lib/browser/CryptoController.js +2 -16
  11. package/lib/browser/EventEmitter.js +1 -1
  12. package/lib/browser/EventuallyQueue.js +277 -824
  13. package/lib/browser/FacebookUtils.js +6 -35
  14. package/lib/browser/IndexedDBStorageController.js +3 -14
  15. package/lib/browser/InstallationController.js +2 -13
  16. package/lib/browser/LiveQueryClient.js +87 -204
  17. package/lib/browser/LiveQuerySubscription.js +10 -39
  18. package/lib/browser/LocalDatastore.js +451 -1081
  19. package/lib/browser/LocalDatastoreController.js +145 -602
  20. package/lib/browser/LocalDatastoreController.react-native.js +137 -573
  21. package/lib/browser/LocalDatastoreUtils.js +2 -12
  22. package/lib/browser/ObjectStateMutations.js +18 -121
  23. package/lib/browser/OfflineQuery.js +42 -222
  24. package/lib/browser/Parse.js +12 -106
  25. package/lib/browser/ParseACL.js +33 -141
  26. package/lib/browser/ParseCLP.js +96 -314
  27. package/lib/browser/ParseConfig.js +18 -88
  28. package/lib/browser/ParseError.js +63 -90
  29. package/lib/browser/ParseFile.js +129 -685
  30. package/lib/browser/ParseGeoPoint.js +23 -82
  31. package/lib/browser/ParseHooks.js +1 -34
  32. package/lib/browser/ParseInstallation.js +4 -32
  33. package/lib/browser/ParseLiveQuery.js +96 -505
  34. package/lib/browser/ParseObject.js +628 -1840
  35. package/lib/browser/ParseOp.js +41 -287
  36. package/lib/browser/ParsePolygon.js +13 -63
  37. package/lib/browser/ParseQuery.js +404 -1505
  38. package/lib/browser/ParseRelation.js +12 -61
  39. package/lib/browser/ParseRole.js +13 -75
  40. package/lib/browser/ParseSchema.js +49 -201
  41. package/lib/browser/ParseSession.js +11 -69
  42. package/lib/browser/ParseUser.js +274 -1127
  43. package/lib/browser/Push.js +7 -43
  44. package/lib/browser/RESTController.js +18 -140
  45. package/lib/browser/SingleInstanceStateController.js +19 -137
  46. package/lib/browser/Socket.weapp.js +2 -12
  47. package/lib/browser/Storage.js +10 -73
  48. package/lib/browser/StorageController.browser.js +4 -17
  49. package/lib/browser/StorageController.default.js +4 -17
  50. package/lib/browser/StorageController.react-native.js +6 -33
  51. package/lib/browser/StorageController.weapp.js +5 -16
  52. package/lib/browser/TaskQueue.js +1 -22
  53. package/lib/browser/UniqueInstanceStateController.js +19 -142
  54. package/lib/browser/Xhr.weapp.js +0 -17
  55. package/lib/browser/arrayContainsObject.js +2 -16
  56. package/lib/browser/canBeSerialized.js +3 -33
  57. package/lib/browser/decode.js +3 -33
  58. package/lib/browser/encode.js +9 -70
  59. package/lib/browser/equals.js +2 -25
  60. package/lib/browser/escape.js +1 -8
  61. package/lib/browser/isRevocableSession.js +2 -11
  62. package/lib/browser/parseDate.js +2 -13
  63. package/lib/browser/promiseUtils.js +3 -21
  64. package/lib/browser/unique.js +3 -17
  65. package/lib/browser/unsavedChildren.js +6 -50
  66. package/lib/browser/uuid.js +3 -1
  67. package/lib/node/Analytics.js +1 -18
  68. package/lib/node/AnonymousUtils.js +5 -37
  69. package/lib/node/Cloud.js +10 -66
  70. package/lib/node/CloudCode.js +0 -3
  71. package/lib/node/CoreManager.js +47 -220
  72. package/lib/node/CryptoController.js +2 -16
  73. package/lib/node/EventEmitter.js +1 -1
  74. package/lib/node/EventuallyQueue.js +16 -123
  75. package/lib/node/FacebookUtils.js +5 -37
  76. package/lib/node/IndexedDBStorageController.js +3 -19
  77. package/lib/node/InstallationController.js +2 -15
  78. package/lib/node/LiveQueryClient.js +56 -136
  79. package/lib/node/LiveQuerySubscription.js +7 -16
  80. package/lib/node/LocalDatastore.js +19 -175
  81. package/lib/node/LocalDatastoreController.js +6 -38
  82. package/lib/node/LocalDatastoreController.react-native.js +6 -45
  83. package/lib/node/LocalDatastoreUtils.js +1 -6
  84. package/lib/node/ObjectStateMutations.js +13 -93
  85. package/lib/node/OfflineQuery.js +20 -152
  86. package/lib/node/Parse.js +11 -99
  87. package/lib/node/ParseACL.js +32 -150
  88. package/lib/node/ParseCLP.js +69 -262
  89. package/lib/node/ParseConfig.js +16 -84
  90. package/lib/node/ParseError.js +61 -65
  91. package/lib/node/ParseFile.js +50 -205
  92. package/lib/node/ParseGeoPoint.js +21 -82
  93. package/lib/node/ParseHooks.js +0 -35
  94. package/lib/node/ParseInstallation.js +1 -10
  95. package/lib/node/ParseLiveQuery.js +10 -36
  96. package/lib/node/ParseObject.js +193 -939
  97. package/lib/node/ParseOp.js +31 -255
  98. package/lib/node/ParsePolygon.js +12 -62
  99. package/lib/node/ParseQuery.js +170 -882
  100. package/lib/node/ParseRelation.js +11 -61
  101. package/lib/node/ParseRole.js +10 -57
  102. package/lib/node/ParseSchema.js +46 -222
  103. package/lib/node/ParseSession.js +8 -50
  104. package/lib/node/ParseUser.js +107 -576
  105. package/lib/node/Push.js +4 -37
  106. package/lib/node/RESTController.js +8 -110
  107. package/lib/node/SingleInstanceStateController.js +17 -130
  108. package/lib/node/Socket.weapp.js +2 -9
  109. package/lib/node/Storage.js +10 -83
  110. package/lib/node/StorageController.browser.js +4 -23
  111. package/lib/node/StorageController.default.js +5 -21
  112. package/lib/node/StorageController.react-native.js +6 -40
  113. package/lib/node/StorageController.weapp.js +6 -21
  114. package/lib/node/TaskQueue.js +1 -17
  115. package/lib/node/UniqueInstanceStateController.js +16 -133
  116. package/lib/node/Xhr.weapp.js +0 -15
  117. package/lib/node/arrayContainsObject.js +1 -12
  118. package/lib/node/canBeSerialized.js +2 -28
  119. package/lib/node/decode.js +1 -26
  120. package/lib/node/encode.js +3 -54
  121. package/lib/node/equals.js +0 -18
  122. package/lib/node/escape.js +1 -6
  123. package/lib/node/isRevocableSession.js +1 -5
  124. package/lib/node/parseDate.js +1 -7
  125. package/lib/node/promiseUtils.js +2 -14
  126. package/lib/node/unique.js +1 -11
  127. package/lib/node/unsavedChildren.js +2 -38
  128. package/lib/node/uuid.js +6 -1
  129. package/lib/react-native/Analytics.js +0 -9
  130. package/lib/react-native/AnonymousUtils.js +0 -10
  131. package/lib/react-native/Cloud.js +0 -26
  132. package/lib/react-native/CoreManager.js +1 -5
  133. package/lib/react-native/CryptoController.js +0 -2
  134. package/lib/react-native/EventEmitter.js +0 -2
  135. package/lib/react-native/EventuallyQueue.js +4 -52
  136. package/lib/react-native/FacebookUtils.js +0 -22
  137. package/lib/react-native/IndexedDBStorageController.js +0 -1
  138. package/lib/react-native/InstallationController.js +0 -6
  139. package/lib/react-native/LiveQueryClient.js +24 -101
  140. package/lib/react-native/LiveQuerySubscription.js +3 -27
  141. package/lib/react-native/LocalDatastore.js +6 -113
  142. package/lib/react-native/LocalDatastoreController.js +0 -21
  143. package/lib/react-native/LocalDatastoreController.react-native.js +4 -31
  144. package/lib/react-native/LocalDatastoreUtils.js +0 -1
  145. package/lib/react-native/ObjectStateMutations.js +0 -34
  146. package/lib/react-native/OfflineQuery.js +2 -137
  147. package/lib/react-native/Parse.js +0 -65
  148. package/lib/react-native/ParseACL.js +0 -44
  149. package/lib/react-native/ParseCLP.js +32 -96
  150. package/lib/react-native/ParseConfig.js +0 -42
  151. package/lib/react-native/ParseError.js +1 -21
  152. package/lib/react-native/ParseFile.js +4 -72
  153. package/lib/react-native/ParseGeoPoint.js +0 -17
  154. package/lib/react-native/ParseHooks.js +0 -30
  155. package/lib/react-native/ParseInstallation.js +1 -23
  156. package/lib/react-native/ParseLiveQuery.js +3 -25
  157. package/lib/react-native/ParseObject.js +4 -371
  158. package/lib/react-native/ParseOp.js +1 -138
  159. package/lib/react-native/ParsePolygon.js +0 -24
  160. package/lib/react-native/ParseQuery.js +6 -262
  161. package/lib/react-native/ParseRelation.js +0 -25
  162. package/lib/react-native/ParseRole.js +1 -36
  163. package/lib/react-native/ParseSchema.js +0 -51
  164. package/lib/react-native/ParseSession.js +1 -40
  165. package/lib/react-native/ParseUser.js +1 -224
  166. package/lib/react-native/Push.js +0 -16
  167. package/lib/react-native/RESTController.js +1 -70
  168. package/lib/react-native/SingleInstanceStateController.js +0 -44
  169. package/lib/react-native/Socket.weapp.js +2 -12
  170. package/lib/react-native/Storage.js +0 -24
  171. package/lib/react-native/StorageController.browser.js +0 -2
  172. package/lib/react-native/StorageController.default.js +0 -1
  173. package/lib/react-native/StorageController.react-native.js +0 -2
  174. package/lib/react-native/TaskQueue.js +0 -15
  175. package/lib/react-native/UniqueInstanceStateController.js +0 -47
  176. package/lib/react-native/Xhr.weapp.js +0 -16
  177. package/lib/react-native/arrayContainsObject.js +0 -5
  178. package/lib/react-native/canBeSerialized.js +0 -18
  179. package/lib/react-native/decode.js +0 -21
  180. package/lib/react-native/encode.js +0 -25
  181. package/lib/react-native/equals.js +0 -18
  182. package/lib/react-native/escape.js +0 -1
  183. package/lib/react-native/isRevocableSession.js +0 -1
  184. package/lib/react-native/parseDate.js +0 -3
  185. package/lib/react-native/promiseUtils.js +0 -13
  186. package/lib/react-native/unique.js +0 -4
  187. package/lib/react-native/unsavedChildren.js +0 -21
  188. package/lib/react-native/uuid.js +3 -1
  189. package/lib/weapp/Analytics.js +2 -20
  190. package/lib/weapp/AnonymousUtils.js +6 -35
  191. package/lib/weapp/Cloud.js +12 -70
  192. package/lib/weapp/CloudCode.js +0 -3
  193. package/lib/weapp/CoreManager.js +46 -184
  194. package/lib/weapp/CryptoController.js +2 -16
  195. package/lib/weapp/EventEmitter.js +1 -1
  196. package/lib/weapp/EventuallyQueue.js +277 -824
  197. package/lib/weapp/FacebookUtils.js +6 -35
  198. package/lib/weapp/IndexedDBStorageController.js +3 -14
  199. package/lib/weapp/InstallationController.js +2 -13
  200. package/lib/weapp/LiveQueryClient.js +87 -204
  201. package/lib/weapp/LiveQuerySubscription.js +10 -39
  202. package/lib/weapp/LocalDatastore.js +451 -1081
  203. package/lib/weapp/LocalDatastoreController.js +145 -602
  204. package/lib/weapp/LocalDatastoreController.react-native.js +137 -573
  205. package/lib/weapp/LocalDatastoreUtils.js +2 -12
  206. package/lib/weapp/ObjectStateMutations.js +18 -121
  207. package/lib/weapp/OfflineQuery.js +42 -222
  208. package/lib/weapp/Parse.js +12 -106
  209. package/lib/weapp/ParseACL.js +33 -141
  210. package/lib/weapp/ParseCLP.js +96 -314
  211. package/lib/weapp/ParseConfig.js +18 -88
  212. package/lib/weapp/ParseError.js +63 -90
  213. package/lib/weapp/ParseFile.js +130 -686
  214. package/lib/weapp/ParseGeoPoint.js +23 -82
  215. package/lib/weapp/ParseHooks.js +1 -34
  216. package/lib/weapp/ParseInstallation.js +4 -32
  217. package/lib/weapp/ParseLiveQuery.js +96 -505
  218. package/lib/weapp/ParseObject.js +628 -1840
  219. package/lib/weapp/ParseOp.js +41 -287
  220. package/lib/weapp/ParsePolygon.js +13 -63
  221. package/lib/weapp/ParseQuery.js +404 -1505
  222. package/lib/weapp/ParseRelation.js +12 -61
  223. package/lib/weapp/ParseRole.js +13 -75
  224. package/lib/weapp/ParseSchema.js +49 -201
  225. package/lib/weapp/ParseSession.js +11 -69
  226. package/lib/weapp/ParseUser.js +274 -1127
  227. package/lib/weapp/Push.js +7 -43
  228. package/lib/weapp/RESTController.js +18 -140
  229. package/lib/weapp/SingleInstanceStateController.js +19 -137
  230. package/lib/weapp/Socket.weapp.js +2 -12
  231. package/lib/weapp/Storage.js +10 -73
  232. package/lib/weapp/StorageController.browser.js +4 -17
  233. package/lib/weapp/StorageController.default.js +4 -17
  234. package/lib/weapp/StorageController.react-native.js +6 -33
  235. package/lib/weapp/StorageController.weapp.js +5 -16
  236. package/lib/weapp/TaskQueue.js +1 -22
  237. package/lib/weapp/UniqueInstanceStateController.js +19 -142
  238. package/lib/weapp/Xhr.weapp.js +0 -17
  239. package/lib/weapp/arrayContainsObject.js +2 -16
  240. package/lib/weapp/canBeSerialized.js +3 -33
  241. package/lib/weapp/decode.js +3 -33
  242. package/lib/weapp/encode.js +9 -70
  243. package/lib/weapp/equals.js +2 -25
  244. package/lib/weapp/escape.js +1 -8
  245. package/lib/weapp/isRevocableSession.js +2 -11
  246. package/lib/weapp/parseDate.js +2 -13
  247. package/lib/weapp/promiseUtils.js +3 -21
  248. package/lib/weapp/unique.js +3 -17
  249. package/lib/weapp/unsavedChildren.js +6 -50
  250. package/lib/weapp/uuid.js +1 -6
  251. package/package.json +45 -43
@@ -4,28 +4,20 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _CoreManager = _interopRequireDefault(require("./CoreManager"));
9
-
10
8
  var _EventEmitter = _interopRequireDefault(require("./EventEmitter"));
11
-
12
9
  var _ParseObject = _interopRequireDefault(require("./ParseObject"));
13
-
14
10
  var _LiveQuerySubscription = _interopRequireDefault(require("./LiveQuerySubscription"));
15
-
16
11
  var _promiseUtils = require("./promiseUtils");
17
-
18
12
  var _ParseError = _interopRequireDefault(require("./ParseError"));
19
-
20
13
  function _interopRequireDefault(obj) {
21
14
  return obj && obj.__esModule ? obj : {
22
15
  default: obj
23
16
  };
24
17
  }
25
18
  /* global WebSocket */
26
- // The LiveQuery client inner state
27
-
28
19
 
20
+ // The LiveQuery client inner state
29
21
  const CLIENT_STATE = {
30
22
  INITIALIZED: 'initialized',
31
23
  CONNECTING: 'connecting',
@@ -33,15 +25,17 @@ const CLIENT_STATE = {
33
25
  CLOSED: 'closed',
34
26
  RECONNECTING: 'reconnecting',
35
27
  DISCONNECTED: 'disconnected'
36
- }; // The event type the LiveQuery client should sent to server
28
+ };
37
29
 
30
+ // The event type the LiveQuery client should sent to server
38
31
  const OP_TYPES = {
39
32
  CONNECT: 'connect',
40
33
  SUBSCRIBE: 'subscribe',
41
34
  UNSUBSCRIBE: 'unsubscribe',
42
35
  ERROR: 'error'
43
- }; // The event we get back from LiveQuery server
36
+ };
44
37
 
38
+ // The event we get back from LiveQuery server
45
39
  const OP_EVENTS = {
46
40
  CONNECTED: 'connected',
47
41
  SUBSCRIBED: 'subscribed',
@@ -52,14 +46,16 @@ const OP_EVENTS = {
52
46
  ENTER: 'enter',
53
47
  LEAVE: 'leave',
54
48
  DELETE: 'delete'
55
- }; // The event the LiveQuery client should emit
49
+ };
56
50
 
51
+ // The event the LiveQuery client should emit
57
52
  const CLIENT_EMMITER_TYPES = {
58
53
  CLOSE: 'close',
59
54
  ERROR: 'error',
60
55
  OPEN: 'open'
61
- }; // The event the LiveQuery subscription should emit
56
+ };
62
57
 
58
+ // The event the LiveQuery subscription should emit
63
59
  const SUBSCRIPTION_EMMITER_TYPES = {
64
60
  OPEN: 'open',
65
61
  CLOSE: 'close',
@@ -70,10 +66,10 @@ const SUBSCRIPTION_EMMITER_TYPES = {
70
66
  LEAVE: 'leave',
71
67
  DELETE: 'delete'
72
68
  };
73
-
74
69
  const generateInterval = k => {
75
70
  return Math.random() * Math.min(30, Math.pow(2, k) - 1) * 1000;
76
71
  };
72
+
77
73
  /**
78
74
  * Creates a new LiveQueryClient.
79
75
  * Extends events.EventEmitter
@@ -118,37 +114,21 @@ const generateInterval = k => {
118
114
  *
119
115
  * @alias Parse.LiveQueryClient
120
116
  */
121
-
122
-
123
117
  class LiveQueryClient extends _EventEmitter.default {
124
118
  /*:: attempts: number;*/
125
-
126
119
  /*:: id: number;*/
127
-
128
120
  /*:: requestId: number;*/
129
-
130
121
  /*:: applicationId: string;*/
131
-
132
122
  /*:: serverURL: string;*/
133
-
134
123
  /*:: javascriptKey: ?string;*/
135
-
136
124
  /*:: masterKey: ?string;*/
137
-
138
125
  /*:: sessionToken: ?string;*/
139
-
140
126
  /*:: installationId: ?string;*/
141
-
142
127
  /*:: additionalProperties: boolean;*/
143
-
144
128
  /*:: connectPromise: Promise;*/
145
-
146
129
  /*:: subscriptions: Map;*/
147
-
148
130
  /*:: socket: any;*/
149
-
150
131
  /*:: state: string;*/
151
-
152
132
  /**
153
133
  * @param {object} options
154
134
  * @param {string} options.applicationId - applicationId of your Parse app
@@ -167,11 +147,9 @@ class LiveQueryClient extends _EventEmitter.default {
167
147
  installationId
168
148
  }) {
169
149
  super();
170
-
171
150
  if (!serverURL || serverURL.indexOf('ws') !== 0) {
172
151
  throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
173
152
  }
174
-
175
153
  this.reconnectHandle = null;
176
154
  this.attempts = 1;
177
155
  this.id = 0;
@@ -185,17 +163,16 @@ class LiveQueryClient extends _EventEmitter.default {
185
163
  this.additionalProperties = true;
186
164
  this.connectPromise = (0, _promiseUtils.resolvingPromise)();
187
165
  this.subscriptions = new Map();
188
- this.state = CLIENT_STATE.INITIALIZED; // adding listener so process does not crash
189
- // best practice is for developer to register their own listener
166
+ this.state = CLIENT_STATE.INITIALIZED;
190
167
 
168
+ // adding listener so process does not crash
169
+ // best practice is for developer to register their own listener
191
170
  this.on('error', () => {});
192
171
  }
193
-
194
- shouldOpen()
195
- /*: any*/
196
- {
172
+ shouldOpen() /*: any*/{
197
173
  return this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED;
198
174
  }
175
+
199
176
  /**
200
177
  * Subscribes to a ParseQuery
201
178
  *
@@ -208,21 +185,12 @@ class LiveQueryClient extends _EventEmitter.default {
208
185
  *
209
186
  * @param {object} query - the ParseQuery you want to subscribe to
210
187
  * @param {string} sessionToken (optional)
211
- * @returns {LiveQuerySubscription} subscription
188
+ * @returns {LiveQuerySubscription | undefined}
212
189
  */
213
-
214
-
215
- subscribe(query
216
- /*: Object*/
217
- , sessionToken
218
- /*: ?string*/
219
- )
220
- /*: LiveQuerySubscription*/
221
- {
190
+ subscribe(query /*: Object*/, sessionToken /*: ?string*/) /*: LiveQuerySubscription*/{
222
191
  if (!query) {
223
192
  return;
224
193
  }
225
-
226
194
  const className = query.className;
227
195
  const queryJSON = query.toJSON();
228
196
  const where = queryJSON.where;
@@ -236,11 +204,9 @@ class LiveQueryClient extends _EventEmitter.default {
236
204
  fields
237
205
  }
238
206
  };
239
-
240
207
  if (sessionToken) {
241
208
  subscribeRequest.sessionToken = sessionToken;
242
209
  }
243
-
244
210
  const subscription = new _LiveQuerySubscription.default(this.requestId, query, sessionToken);
245
211
  this.subscriptions.set(this.requestId, subscription);
246
212
  this.requestId += 1;
@@ -251,67 +217,60 @@ class LiveQueryClient extends _EventEmitter.default {
251
217
  });
252
218
  return subscription;
253
219
  }
220
+
254
221
  /**
255
222
  * After calling unsubscribe you'll stop receiving events from the subscription object.
256
223
  *
257
224
  * @param {object} subscription - subscription you would like to unsubscribe from.
225
+ * @returns {Promise | undefined}
258
226
  */
259
-
260
-
261
- unsubscribe(subscription
262
- /*: Object*/
263
- ) {
227
+ unsubscribe(subscription /*: Object*/) /*: ?Promise*/{
264
228
  if (!subscription) {
265
229
  return;
266
230
  }
267
-
268
- this.subscriptions.delete(subscription.id);
269
231
  const unsubscribeRequest = {
270
232
  op: OP_TYPES.UNSUBSCRIBE,
271
233
  requestId: subscription.id
272
234
  };
273
- this.connectPromise.then(() => {
274
- this.socket.send(JSON.stringify(unsubscribeRequest));
235
+ return this.connectPromise.then(() => {
236
+ return this.socket.send(JSON.stringify(unsubscribeRequest));
237
+ }).then(() => {
238
+ return subscription.unsubscribePromise;
275
239
  });
276
240
  }
241
+
277
242
  /**
278
243
  * After open is called, the LiveQueryClient will try to send a connect request
279
244
  * to the LiveQuery server.
280
245
  *
281
246
  */
282
-
283
-
284
247
  open() {
285
248
  const WebSocketImplementation = _CoreManager.default.getWebSocketController();
286
-
287
249
  if (!WebSocketImplementation) {
288
250
  this.emit(CLIENT_EMMITER_TYPES.ERROR, 'Can not find WebSocket implementation');
289
251
  return;
290
252
  }
291
-
292
253
  if (this.state !== CLIENT_STATE.RECONNECTING) {
293
254
  this.state = CLIENT_STATE.CONNECTING;
294
255
  }
256
+ this.socket = new WebSocketImplementation(this.serverURL);
257
+ this.socket.closingPromise = (0, _promiseUtils.resolvingPromise)();
295
258
 
296
- this.socket = new WebSocketImplementation(this.serverURL); // Bind WebSocket callbacks
297
-
259
+ // Bind WebSocket callbacks
298
260
  this.socket.onopen = () => {
299
261
  this._handleWebSocketOpen();
300
262
  };
301
-
302
263
  this.socket.onmessage = event => {
303
264
  this._handleWebSocketMessage(event);
304
265
  };
305
-
306
- this.socket.onclose = () => {
266
+ this.socket.onclose = event => {
267
+ this.socket.closingPromise.resolve(event);
307
268
  this._handleWebSocketClose();
308
269
  };
309
-
310
270
  this.socket.onerror = error => {
311
271
  this._handleWebSocketError(error);
312
272
  };
313
273
  }
314
-
315
274
  resubscribe() {
316
275
  this.subscriptions.forEach((subscription, requestId) => {
317
276
  const query = subscription.query;
@@ -329,42 +288,39 @@ class LiveQueryClient extends _EventEmitter.default {
329
288
  fields
330
289
  }
331
290
  };
332
-
333
291
  if (sessionToken) {
334
292
  subscribeRequest.sessionToken = sessionToken;
335
293
  }
336
-
337
294
  this.connectPromise.then(() => {
338
295
  this.socket.send(JSON.stringify(subscribeRequest));
339
296
  });
340
297
  });
341
298
  }
299
+
342
300
  /**
343
301
  * This method will close the WebSocket connection to this LiveQueryClient,
344
302
  * cancel the auto reconnect and unsubscribe all subscriptions based on it.
345
303
  *
304
+ * @returns {Promise | undefined} CloseEvent {@link https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close_event}
346
305
  */
347
-
348
-
349
- close() {
306
+ close() /*: ?Promise*/{
307
+ var _this$socket, _this$socket2;
350
308
  if (this.state === CLIENT_STATE.INITIALIZED || this.state === CLIENT_STATE.DISCONNECTED) {
351
309
  return;
352
310
  }
353
-
354
311
  this.state = CLIENT_STATE.DISCONNECTED;
355
- this.socket.close(); // Notify each subscription about the close
356
-
312
+ (_this$socket = this.socket) === null || _this$socket === void 0 ? void 0 : _this$socket.close();
313
+ // Notify each subscription about the close
357
314
  for (const subscription of this.subscriptions.values()) {
358
315
  subscription.subscribed = false;
359
316
  subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);
360
317
  }
361
-
362
318
  this._handleReset();
363
-
364
319
  this.emit(CLIENT_EMMITER_TYPES.CLOSE);
365
- } // ensure we start with valid state if connect is called again after close
366
-
320
+ return (_this$socket2 = this.socket) === null || _this$socket2 === void 0 ? void 0 : _this$socket2.closingPromise;
321
+ }
367
322
 
323
+ // ensure we start with valid state if connect is called again after close
368
324
  _handleReset() {
369
325
  this.attempts = 1;
370
326
  this.id = 0;
@@ -372,7 +328,6 @@ class LiveQueryClient extends _EventEmitter.default {
372
328
  this.connectPromise = (0, _promiseUtils.resolvingPromise)();
373
329
  this.subscriptions = new Map();
374
330
  }
375
-
376
331
  _handleWebSocketOpen() {
377
332
  this.attempts = 1;
378
333
  const connectRequest = {
@@ -382,64 +337,48 @@ class LiveQueryClient extends _EventEmitter.default {
382
337
  masterKey: this.masterKey,
383
338
  sessionToken: this.sessionToken
384
339
  };
385
-
386
340
  if (this.additionalProperties) {
387
341
  connectRequest.installationId = this.installationId;
388
342
  }
389
-
390
343
  this.socket.send(JSON.stringify(connectRequest));
391
344
  }
392
-
393
- _handleWebSocketMessage(event
394
- /*: any*/
395
- ) {
345
+ _handleWebSocketMessage(event /*: any*/) {
396
346
  let data = event.data;
397
-
398
347
  if (typeof data === 'string') {
399
348
  data = JSON.parse(data);
400
349
  }
401
-
402
350
  let subscription = null;
403
-
404
351
  if (data.requestId) {
405
352
  subscription = this.subscriptions.get(data.requestId);
406
353
  }
407
-
408
354
  const response = {
409
355
  clientId: data.clientId,
410
356
  installationId: data.installationId
411
357
  };
412
-
413
358
  switch (data.op) {
414
359
  case OP_EVENTS.CONNECTED:
415
360
  if (this.state === CLIENT_STATE.RECONNECTING) {
416
361
  this.resubscribe();
417
362
  }
418
-
419
363
  this.emit(CLIENT_EMMITER_TYPES.OPEN);
420
364
  this.id = data.clientId;
421
365
  this.connectPromise.resolve();
422
366
  this.state = CLIENT_STATE.CONNECTED;
423
367
  break;
424
-
425
368
  case OP_EVENTS.SUBSCRIBED:
426
369
  if (subscription) {
427
370
  subscription.subscribed = true;
428
371
  subscription.subscribePromise.resolve();
429
372
  setTimeout(() => subscription.emit(SUBSCRIPTION_EMMITER_TYPES.OPEN, response), 200);
430
373
  }
431
-
432
374
  break;
433
-
434
375
  case OP_EVENTS.ERROR:
435
376
  {
436
377
  const parseError = new _ParseError.default(data.code, data.error);
437
-
438
378
  if (!this.id) {
439
379
  this.connectPromise.reject(parseError);
440
380
  this.state = CLIENT_STATE.DISCONNECTED;
441
381
  }
442
-
443
382
  if (data.requestId) {
444
383
  if (subscription) {
445
384
  subscription.subscribePromise.reject(parseError);
@@ -448,98 +387,83 @@ class LiveQueryClient extends _EventEmitter.default {
448
387
  } else {
449
388
  this.emit(CLIENT_EMMITER_TYPES.ERROR, data.error);
450
389
  }
451
-
452
390
  if (data.error === 'Additional properties not allowed') {
453
391
  this.additionalProperties = false;
454
392
  }
455
-
456
393
  if (data.reconnect) {
457
394
  this._handleReconnect();
458
395
  }
459
-
460
396
  break;
461
397
  }
462
-
463
398
  case OP_EVENTS.UNSUBSCRIBED:
464
- // We have already deleted subscription in unsubscribe(), do nothing here
465
- break;
466
-
399
+ {
400
+ if (subscription) {
401
+ this.subscriptions.delete(data.requestId);
402
+ subscription.subscribed = false;
403
+ subscription.unsubscribePromise.resolve();
404
+ }
405
+ break;
406
+ }
467
407
  default:
468
408
  {
469
409
  // create, update, enter, leave, delete cases
470
410
  if (!subscription) {
471
411
  break;
472
412
  }
473
-
474
413
  let override = false;
475
-
476
414
  if (data.original) {
477
415
  override = true;
478
- delete data.original.__type; // Check for removed fields
479
-
416
+ delete data.original.__type;
417
+ // Check for removed fields
480
418
  for (const field in data.original) {
481
419
  if (!(field in data.object)) {
482
420
  data.object[field] = undefined;
483
421
  }
484
422
  }
485
-
486
423
  data.original = _ParseObject.default.fromJSON(data.original, false);
487
424
  }
488
-
489
425
  delete data.object.__type;
490
-
491
426
  const parseObject = _ParseObject.default.fromJSON(data.object, !(subscription.query && subscription.query._select) ? override : false);
492
-
493
427
  if (data.original) {
494
428
  subscription.emit(data.op, parseObject, data.original, response);
495
429
  } else {
496
430
  subscription.emit(data.op, parseObject, response);
497
431
  }
498
-
499
432
  const localDatastore = _CoreManager.default.getLocalDatastore();
500
-
501
433
  if (override && localDatastore.isEnabled) {
502
434
  localDatastore._updateObjectIfPinned(parseObject).then(() => {});
503
435
  }
504
436
  }
505
437
  }
506
438
  }
507
-
508
439
  _handleWebSocketClose() {
509
440
  if (this.state === CLIENT_STATE.DISCONNECTED) {
510
441
  return;
511
442
  }
512
-
513
443
  this.state = CLIENT_STATE.CLOSED;
514
- this.emit(CLIENT_EMMITER_TYPES.CLOSE); // Notify each subscription about the close
515
-
444
+ this.emit(CLIENT_EMMITER_TYPES.CLOSE);
445
+ // Notify each subscription about the close
516
446
  for (const subscription of this.subscriptions.values()) {
517
447
  subscription.emit(SUBSCRIPTION_EMMITER_TYPES.CLOSE);
518
448
  }
519
-
520
449
  this._handleReconnect();
521
450
  }
522
-
523
- _handleWebSocketError(error
524
- /*: any*/
525
- ) {
451
+ _handleWebSocketError(error /*: any*/) {
526
452
  this.emit(CLIENT_EMMITER_TYPES.ERROR, error);
527
-
528
453
  for (const subscription of this.subscriptions.values()) {
529
454
  subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR, error);
530
455
  }
531
-
532
456
  this._handleReconnect();
533
457
  }
534
-
535
458
  _handleReconnect() {
536
459
  // if closed or currently reconnecting we stop attempting to reconnect
537
460
  if (this.state === CLIENT_STATE.DISCONNECTED) {
538
461
  return;
539
462
  }
540
-
541
463
  this.state = CLIENT_STATE.RECONNECTING;
542
- const time = generateInterval(this.attempts); // handle case when both close/error occur at frequent rates we ensure we do not reconnect unnecessarily.
464
+ const time = generateInterval(this.attempts);
465
+
466
+ // handle case when both close/error occur at frequent rates we ensure we do not reconnect unnecessarily.
543
467
  // we're unable to distinguish different between close/error when we're unable to reconnect therefore
544
468
  // we try to reconnect in both cases
545
469
  // server side ws and browser WebSocket behave differently in when close/error get triggered
@@ -547,17 +471,13 @@ class LiveQueryClient extends _EventEmitter.default {
547
471
  if (this.reconnectHandle) {
548
472
  clearTimeout(this.reconnectHandle);
549
473
  }
550
-
551
474
  this.reconnectHandle = setTimeout((() => {
552
475
  this.attempts++;
553
476
  this.connectPromise = (0, _promiseUtils.resolvingPromise)();
554
477
  this.open();
555
478
  }).bind(this), time);
556
479
  }
557
-
558
480
  }
559
-
560
481
  _CoreManager.default.setWebSocketController(require('ws'));
561
-
562
482
  var _default = LiveQueryClient;
563
483
  exports.default = _default;
@@ -4,13 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _EventEmitter = _interopRequireDefault(require("./EventEmitter"));
9
-
10
8
  var _CoreManager = _interopRequireDefault(require("./CoreManager"));
11
-
12
9
  var _promiseUtils = require("./promiseUtils");
13
-
14
10
  function _interopRequireDefault(obj) {
15
11
  return obj && obj.__esModule ? obj : {
16
12
  default: obj
@@ -101,8 +97,6 @@ function _interopRequireDefault(obj) {
101
97
  *
102
98
  * @alias Parse.LiveQuerySubscription
103
99
  */
104
-
105
-
106
100
  class Subscription extends _EventEmitter.default {
107
101
  /*
108
102
  * @param {string} id - subscription id
@@ -115,28 +109,25 @@ class Subscription extends _EventEmitter.default {
115
109
  this.query = query;
116
110
  this.sessionToken = sessionToken;
117
111
  this.subscribePromise = (0, _promiseUtils.resolvingPromise)();
118
- this.subscribed = false; // adding listener so process does not crash
119
- // best practice is for developer to register their own listener
112
+ this.unsubscribePromise = (0, _promiseUtils.resolvingPromise)();
113
+ this.subscribed = false;
120
114
 
115
+ // adding listener so process does not crash
116
+ // best practice is for developer to register their own listener
121
117
  this.on('error', () => {});
122
118
  }
119
+
123
120
  /**
124
121
  * Close the subscription
125
122
  *
126
123
  * @returns {Promise}
127
124
  */
128
-
129
-
130
- unsubscribe()
131
- /*: Promise*/
132
- {
125
+ unsubscribe() /*: Promise*/{
133
126
  return _CoreManager.default.getLiveQueryController().getDefaultLiveQueryClient().then(liveQueryClient => {
134
- liveQueryClient.unsubscribe(this);
135
127
  this.emit('close');
128
+ return liveQueryClient.unsubscribe(this);
136
129
  });
137
130
  }
138
-
139
131
  }
140
-
141
132
  var _default = Subscription;
142
133
  exports.default = _default;