parse 3.5.1 → 4.0.0-alpha.10

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