@ngxs/websocket-plugin 3.7.3-dev.master-5175b98 → 3.7.3

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.
@@ -1,643 +1,614 @@
1
1
  import { __extends, __assign } from 'tslib';
2
2
  import { InjectionToken, Injectable, Inject, APP_INITIALIZER, NgModule } from '@angular/core';
3
3
  import { ofActionDispatched, getValue, Store, Actions } from '@ngxs/store';
4
- import { Subscription } from 'rxjs';
5
4
  import { WebSocketSubject } from 'rxjs/webSocket';
6
5
 
7
- /**
8
- * @fileoverview added by tsickle
9
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
10
- */
11
- /** @type {?} */
12
- var NGXS_WEBSOCKET_OPTIONS = new InjectionToken('NGXS_WEBSOCKET_OPTIONS');
13
- /**
14
- * @record
15
- */
16
- function NgxsWebsocketPluginOptions() { }
17
- if (false) {
18
- /**
19
- * URL of the websocket.
20
- * @type {?|undefined}
21
- */
22
- NgxsWebsocketPluginOptions.prototype.url;
23
- /**
24
- * Either a single protocol string or an array of protocol strings.
25
- * These strings are used to indicate sub-protocols, so that a single server
26
- * can implement multiple WebSocket sub-protocols (for example, you might want one server to be able
27
- * to handle different types of interactions depending on the specified protocol).
28
- * If you don't specify a protocol string, an empty string is assumed.
29
- * @type {?|undefined}
30
- */
31
- NgxsWebsocketPluginOptions.prototype.protocol;
32
- /**
33
- * Sets the `binaryType` property of the underlying WebSocket.
34
- * @type {?|undefined}
35
- */
36
- NgxsWebsocketPluginOptions.prototype.binaryType;
37
- /**
38
- * The property name to distigunish this type for the store.
39
- * Default: 'type'
40
- * @type {?|undefined}
41
- */
42
- NgxsWebsocketPluginOptions.prototype.typeKey;
43
- /**
44
- * Interval to try and reconnect.
45
- * Default: 5000
46
- * @type {?|undefined}
47
- */
48
- NgxsWebsocketPluginOptions.prototype.reconnectInterval;
49
- /**
50
- * Number of reconnect attemps.
51
- * Default: 10
52
- * @type {?|undefined}
53
- */
54
- NgxsWebsocketPluginOptions.prototype.reconnectAttempts;
55
- /**
56
- * Serializer to call before sending messages
57
- * Default: `json.stringify`
58
- * @type {?|undefined}
59
- */
60
- NgxsWebsocketPluginOptions.prototype.serializer;
61
- /**
62
- * Deseralizer before publishing the message.
63
- * @type {?|undefined}
64
- */
65
- NgxsWebsocketPluginOptions.prototype.deserializer;
66
- }
67
- /**
68
- * @param {...?} _args
69
- * @return {?}
70
- */
71
- function noop() {
72
- var _args = [];
73
- for (var _i = 0; _i < arguments.length; _i++) {
74
- _args[_i] = arguments[_i];
75
- }
76
- return (/**
77
- * @return {?}
78
- */
79
- function () { });
80
- }
81
- /**
82
- * Action to connect to the websocket. Optionally pass a URL.
83
- */
84
- var /**
85
- * Action to connect to the websocket. Optionally pass a URL.
86
- */
87
- ConnectWebSocket = /** @class */ (function () {
88
- function ConnectWebSocket(payload) {
89
- this.payload = payload;
90
- }
91
- Object.defineProperty(ConnectWebSocket, "type", {
92
- get: /**
93
- * @return {?}
94
- */
95
- function () {
96
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
97
- return '[WebSocket] Connect';
98
- },
99
- enumerable: true,
100
- configurable: true
101
- });
102
- return ConnectWebSocket;
103
- }());
104
- if (false) {
105
- /** @type {?} */
106
- ConnectWebSocket.prototype.payload;
107
- }
108
- /**
109
- * Action triggered when a error ocurrs
110
- */
111
- var /**
112
- * Action triggered when a error ocurrs
113
- */
114
- WebsocketMessageError = /** @class */ (function () {
115
- function WebsocketMessageError(payload) {
116
- this.payload = payload;
117
- }
118
- Object.defineProperty(WebsocketMessageError, "type", {
119
- get: /**
120
- * @return {?}
121
- */
122
- function () {
123
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
124
- return '[WebSocket] Message Error';
125
- },
126
- enumerable: true,
127
- configurable: true
128
- });
129
- return WebsocketMessageError;
130
- }());
131
- if (false) {
132
- /** @type {?} */
133
- WebsocketMessageError.prototype.payload;
134
- }
135
- /**
136
- * Action to disconnect the websocket.
137
- */
138
- var /**
139
- * Action to disconnect the websocket.
140
- */
141
- DisconnectWebSocket = /** @class */ (function () {
142
- function DisconnectWebSocket() {
143
- }
144
- Object.defineProperty(DisconnectWebSocket, "type", {
145
- get: /**
146
- * @return {?}
147
- */
148
- function () {
149
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
150
- return '[WebSocket] Disconnect';
151
- },
152
- enumerable: true,
153
- configurable: true
154
- });
155
- return DisconnectWebSocket;
156
- }());
157
- /**
158
- * Action triggered when websocket is connected
159
- */
160
- var /**
161
- * Action triggered when websocket is connected
162
- */
163
- WebSocketConnected = /** @class */ (function () {
164
- function WebSocketConnected() {
165
- }
166
- Object.defineProperty(WebSocketConnected, "type", {
167
- get: /**
168
- * @return {?}
169
- */
170
- function () {
171
- return '[WebSocket] Connected';
172
- },
173
- enumerable: true,
174
- configurable: true
175
- });
176
- return WebSocketConnected;
177
- }());
178
- /**
179
- * Action triggered when websocket is disconnected
180
- */
181
- var /**
182
- * Action triggered when websocket is disconnected
183
- */
184
- WebSocketDisconnected = /** @class */ (function () {
185
- function WebSocketDisconnected() {
186
- }
187
- Object.defineProperty(WebSocketDisconnected, "type", {
188
- get: /**
189
- * @return {?}
190
- */
191
- function () {
192
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
193
- return '[WebSocket] Disconnected';
194
- },
195
- enumerable: true,
196
- configurable: true
197
- });
198
- return WebSocketDisconnected;
199
- }());
200
- /**
201
- * Action to send to the server.
202
- */
203
- var /**
204
- * Action to send to the server.
205
- */
206
- SendWebSocketMessage = /** @class */ (function () {
207
- function SendWebSocketMessage(payload) {
208
- this.payload = payload;
209
- }
210
- Object.defineProperty(SendWebSocketMessage, "type", {
211
- get: /**
212
- * @return {?}
213
- */
214
- function () {
215
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
216
- return '[WebSocket] Send Message';
217
- },
218
- enumerable: true,
219
- configurable: true
220
- });
221
- return SendWebSocketMessage;
222
- }());
223
- if (false) {
224
- /** @type {?} */
225
- SendWebSocketMessage.prototype.payload;
226
- }
227
- /**
228
- * Action dispatched when the user tries to connect if the connection already exists.
229
- */
230
- var /**
231
- * Action dispatched when the user tries to connect if the connection already exists.
232
- */
233
- WebSocketConnectionUpdated = /** @class */ (function () {
234
- function WebSocketConnectionUpdated() {
235
- }
236
- Object.defineProperty(WebSocketConnectionUpdated, "type", {
237
- get: /**
238
- * @return {?}
239
- */
240
- function () {
241
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
242
- return '[WebSocket] Connection Updated';
243
- },
244
- enumerable: true,
245
- configurable: true
246
- });
247
- return WebSocketConnectionUpdated;
248
- }());
249
- /**
250
- * This error is thrown where there is no `type` (or custom `typeKey`) property
251
- * on the message that came from the server side socket
252
- */
253
- var /**
254
- * This error is thrown where there is no `type` (or custom `typeKey`) property
255
- * on the message that came from the server side socket
256
- */
257
- TypeKeyPropertyMissingError = /** @class */ (function (_super) {
258
- __extends(TypeKeyPropertyMissingError, _super);
259
- function TypeKeyPropertyMissingError(typeKey) {
260
- return _super.call(this, "Property " + typeKey + " is missing on the socket message") || this;
261
- }
262
- return TypeKeyPropertyMissingError;
6
+ /**
7
+ * @fileoverview added by tsickle
8
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
9
+ */
10
+ /** @type {?} */
11
+ var NGXS_WEBSOCKET_OPTIONS = new InjectionToken('NGXS_WEBSOCKET_OPTIONS');
12
+ /**
13
+ * @record
14
+ */
15
+ function NgxsWebsocketPluginOptions() { }
16
+ if (false) {
17
+ /**
18
+ * URL of the websocket.
19
+ * @type {?|undefined}
20
+ */
21
+ NgxsWebsocketPluginOptions.prototype.url;
22
+ /**
23
+ * Either a single protocol string or an array of protocol strings.
24
+ * These strings are used to indicate sub-protocols, so that a single server
25
+ * can implement multiple WebSocket sub-protocols (for example, you might want one server to be able
26
+ * to handle different types of interactions depending on the specified protocol).
27
+ * If you don't specify a protocol string, an empty string is assumed.
28
+ * @type {?|undefined}
29
+ */
30
+ NgxsWebsocketPluginOptions.prototype.protocol;
31
+ /**
32
+ * Sets the `binaryType` property of the underlying WebSocket.
33
+ * @type {?|undefined}
34
+ */
35
+ NgxsWebsocketPluginOptions.prototype.binaryType;
36
+ /**
37
+ * The property name to distigunish this type for the store.
38
+ * Default: 'type'
39
+ * @type {?|undefined}
40
+ */
41
+ NgxsWebsocketPluginOptions.prototype.typeKey;
42
+ /**
43
+ * Interval to try and reconnect.
44
+ * Default: 5000
45
+ * @type {?|undefined}
46
+ */
47
+ NgxsWebsocketPluginOptions.prototype.reconnectInterval;
48
+ /**
49
+ * Number of reconnect attemps.
50
+ * Default: 10
51
+ * @type {?|undefined}
52
+ */
53
+ NgxsWebsocketPluginOptions.prototype.reconnectAttempts;
54
+ /**
55
+ * Serializer to call before sending messages
56
+ * Default: `json.stringify`
57
+ * @type {?|undefined}
58
+ */
59
+ NgxsWebsocketPluginOptions.prototype.serializer;
60
+ /**
61
+ * Deseralizer before publishing the message.
62
+ * @type {?|undefined}
63
+ */
64
+ NgxsWebsocketPluginOptions.prototype.deserializer;
65
+ }
66
+ /**
67
+ * @param {...?} _args
68
+ * @return {?}
69
+ */
70
+ function noop() {
71
+ var _args = [];
72
+ for (var _i = 0; _i < arguments.length; _i++) {
73
+ _args[_i] = arguments[_i];
74
+ }
75
+ return (/**
76
+ * @return {?}
77
+ */
78
+ function () { });
79
+ }
80
+ /**
81
+ * Action to connect to the websocket. Optionally pass a URL.
82
+ */
83
+ var /**
84
+ * Action to connect to the websocket. Optionally pass a URL.
85
+ */
86
+ ConnectWebSocket = /** @class */ (function () {
87
+ function ConnectWebSocket(payload) {
88
+ this.payload = payload;
89
+ }
90
+ Object.defineProperty(ConnectWebSocket, "type", {
91
+ get: /**
92
+ * @return {?}
93
+ */
94
+ function () {
95
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
96
+ return '[WebSocket] Connect';
97
+ },
98
+ enumerable: true,
99
+ configurable: true
100
+ });
101
+ return ConnectWebSocket;
102
+ }());
103
+ if (false) {
104
+ /** @type {?} */
105
+ ConnectWebSocket.prototype.payload;
106
+ }
107
+ /**
108
+ * Action triggered when a error ocurrs
109
+ */
110
+ var /**
111
+ * Action triggered when a error ocurrs
112
+ */
113
+ WebsocketMessageError = /** @class */ (function () {
114
+ function WebsocketMessageError(payload) {
115
+ this.payload = payload;
116
+ }
117
+ Object.defineProperty(WebsocketMessageError, "type", {
118
+ get: /**
119
+ * @return {?}
120
+ */
121
+ function () {
122
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
123
+ return '[WebSocket] Message Error';
124
+ },
125
+ enumerable: true,
126
+ configurable: true
127
+ });
128
+ return WebsocketMessageError;
129
+ }());
130
+ if (false) {
131
+ /** @type {?} */
132
+ WebsocketMessageError.prototype.payload;
133
+ }
134
+ /**
135
+ * Action to disconnect the websocket.
136
+ */
137
+ var /**
138
+ * Action to disconnect the websocket.
139
+ */
140
+ DisconnectWebSocket = /** @class */ (function () {
141
+ function DisconnectWebSocket() {
142
+ }
143
+ Object.defineProperty(DisconnectWebSocket, "type", {
144
+ get: /**
145
+ * @return {?}
146
+ */
147
+ function () {
148
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
149
+ return '[WebSocket] Disconnect';
150
+ },
151
+ enumerable: true,
152
+ configurable: true
153
+ });
154
+ return DisconnectWebSocket;
155
+ }());
156
+ /**
157
+ * Action triggered when websocket is connected
158
+ */
159
+ var /**
160
+ * Action triggered when websocket is connected
161
+ */
162
+ WebSocketConnected = /** @class */ (function () {
163
+ function WebSocketConnected() {
164
+ }
165
+ Object.defineProperty(WebSocketConnected, "type", {
166
+ get: /**
167
+ * @return {?}
168
+ */
169
+ function () {
170
+ return '[WebSocket] Connected';
171
+ },
172
+ enumerable: true,
173
+ configurable: true
174
+ });
175
+ return WebSocketConnected;
176
+ }());
177
+ /**
178
+ * Action triggered when websocket is disconnected
179
+ */
180
+ var /**
181
+ * Action triggered when websocket is disconnected
182
+ */
183
+ WebSocketDisconnected = /** @class */ (function () {
184
+ function WebSocketDisconnected() {
185
+ }
186
+ Object.defineProperty(WebSocketDisconnected, "type", {
187
+ get: /**
188
+ * @return {?}
189
+ */
190
+ function () {
191
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
192
+ return '[WebSocket] Disconnected';
193
+ },
194
+ enumerable: true,
195
+ configurable: true
196
+ });
197
+ return WebSocketDisconnected;
198
+ }());
199
+ /**
200
+ * Action to send to the server.
201
+ */
202
+ var /**
203
+ * Action to send to the server.
204
+ */
205
+ SendWebSocketMessage = /** @class */ (function () {
206
+ function SendWebSocketMessage(payload) {
207
+ this.payload = payload;
208
+ }
209
+ Object.defineProperty(SendWebSocketMessage, "type", {
210
+ get: /**
211
+ * @return {?}
212
+ */
213
+ function () {
214
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
215
+ return '[WebSocket] Send Message';
216
+ },
217
+ enumerable: true,
218
+ configurable: true
219
+ });
220
+ return SendWebSocketMessage;
221
+ }());
222
+ if (false) {
223
+ /** @type {?} */
224
+ SendWebSocketMessage.prototype.payload;
225
+ }
226
+ /**
227
+ * Action dispatched when the user tries to connect if the connection already exists.
228
+ */
229
+ var /**
230
+ * Action dispatched when the user tries to connect if the connection already exists.
231
+ */
232
+ WebSocketConnectionUpdated = /** @class */ (function () {
233
+ function WebSocketConnectionUpdated() {
234
+ }
235
+ Object.defineProperty(WebSocketConnectionUpdated, "type", {
236
+ get: /**
237
+ * @return {?}
238
+ */
239
+ function () {
240
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
241
+ return '[WebSocket] Connection Updated';
242
+ },
243
+ enumerable: true,
244
+ configurable: true
245
+ });
246
+ return WebSocketConnectionUpdated;
247
+ }());
248
+ /**
249
+ * This error is thrown where there is no `type` (or custom `typeKey`) property
250
+ * on the message that came from the server side socket
251
+ */
252
+ var /**
253
+ * This error is thrown where there is no `type` (or custom `typeKey`) property
254
+ * on the message that came from the server side socket
255
+ */
256
+ TypeKeyPropertyMissingError = /** @class */ (function (_super) {
257
+ __extends(TypeKeyPropertyMissingError, _super);
258
+ function TypeKeyPropertyMissingError(typeKey) {
259
+ return _super.call(this, "Property " + typeKey + " is missing on the socket message") || this;
260
+ }
261
+ return TypeKeyPropertyMissingError;
263
262
  }(Error));
264
263
 
265
- /**
266
- * @fileoverview added by tsickle
267
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
268
- */
269
- var WebSocketHandler = /** @class */ (function () {
270
- function WebSocketHandler(store, actions$, options) {
271
- var _this = this;
272
- this.store = store;
273
- this.actions$ = actions$;
274
- this.options = options;
275
- this.socket = null;
276
- this.config = {
277
- url: (/** @type {?} */ (this.options.url)),
278
- protocol: this.options.protocol,
279
- // Default binary type is `blob` for the global `WebSocket`
280
- binaryType: this.options.binaryType,
281
- serializer: this.options.serializer,
282
- deserializer: this.options.deserializer,
283
- closeObserver: {
284
- next: (/**
285
- * @return {?}
286
- */
287
- function () {
288
- // ATTENTION!
289
- // See https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/dom/WebSocketSubject.ts#L340
290
- // RxJS socket emits `onComplete` event only if `event.wasClean` is truthy
291
- // and doesn't complete socket subject if it's falsy
292
- _this.disconnect();
293
- })
294
- },
295
- openObserver: {
296
- next: (/**
297
- * @return {?}
298
- */
299
- function () { return _this.store.dispatch(new WebSocketConnected()); })
300
- }
301
- };
302
- this.typeKey = (/** @type {?} */ (this.options.typeKey));
303
- this.subscription = new Subscription();
304
- this.setupActionsListeners();
305
- }
306
- /**
307
- * @return {?}
308
- */
309
- WebSocketHandler.prototype.ngOnDestroy = /**
310
- * @return {?}
311
- */
312
- function () {
313
- this.closeConnection();
314
- this.subscription.unsubscribe();
315
- };
316
- /**
317
- * @private
318
- * @return {?}
319
- */
320
- WebSocketHandler.prototype.setupActionsListeners = /**
321
- * @private
322
- * @return {?}
323
- */
324
- function () {
325
- var _this = this;
326
- this.subscription.add(this.actions$.pipe(ofActionDispatched(ConnectWebSocket)).subscribe((/**
327
- * @param {?} __0
328
- * @return {?}
329
- */
330
- function (_a) {
331
- var payload = _a.payload;
332
- _this.connect(payload);
333
- })));
334
- this.subscription.add(this.actions$.pipe(ofActionDispatched(DisconnectWebSocket)).subscribe((/**
335
- * @return {?}
336
- */
337
- function () {
338
- _this.disconnect();
339
- })));
340
- this.subscription.add(this.actions$.pipe(ofActionDispatched(SendWebSocketMessage)).subscribe((/**
341
- * @param {?} __0
342
- * @return {?}
343
- */
344
- function (_a) {
345
- var payload = _a.payload;
346
- _this.send(payload);
347
- })));
348
- };
349
- /**
350
- * @private
351
- * @param {?=} options
352
- * @return {?}
353
- */
354
- WebSocketHandler.prototype.connect = /**
355
- * @private
356
- * @param {?=} options
357
- * @return {?}
358
- */
359
- function (options) {
360
- var _this = this;
361
- this.updateConnection();
362
- // Users can pass the options in the connect method so
363
- // if options aren't available at DI bootstrap they have access
364
- // to pass them here
365
- if (options) {
366
- this.mergeConfigWithOptions(options);
367
- }
368
- this.socket = new WebSocketSubject(this.config);
369
- this.socket.subscribe({
370
- next: (/**
371
- * @param {?} message
372
- * @return {?}
373
- */
374
- function (message) {
375
- /** @type {?} */
376
- var type = getValue(message, _this.typeKey);
377
- if (!type) {
378
- throw new TypeKeyPropertyMissingError(_this.typeKey);
379
- }
380
- _this.store.dispatch(__assign({}, message, { type: type }));
381
- }),
382
- error: (/**
383
- * @param {?} error
384
- * @return {?}
385
- */
386
- function (error) {
387
- if (error instanceof CloseEvent) {
388
- _this.dispatchWebSocketDisconnected();
389
- }
390
- else {
391
- _this.store.dispatch(new WebsocketMessageError(error));
392
- }
393
- })
394
- });
395
- };
396
- /**
397
- * @private
398
- * @return {?}
399
- */
400
- WebSocketHandler.prototype.disconnect = /**
401
- * @private
402
- * @return {?}
403
- */
404
- function () {
405
- if (this.socket) {
406
- this.closeConnection();
407
- this.dispatchWebSocketDisconnected();
408
- }
409
- };
410
- /**
411
- * @private
412
- * @param {?} data
413
- * @return {?}
414
- */
415
- WebSocketHandler.prototype.send = /**
416
- * @private
417
- * @param {?} data
418
- * @return {?}
419
- */
420
- function (data) {
421
- if (!this.socket) {
422
- throw new Error('You must connect to the socket before sending any data');
423
- }
424
- this.socket.next(data);
425
- };
426
- /**
427
- * Don't enlarge the `connect` method
428
- */
429
- /**
430
- * Don't enlarge the `connect` method
431
- * @private
432
- * @param {?} options
433
- * @return {?}
434
- */
435
- WebSocketHandler.prototype.mergeConfigWithOptions = /**
436
- * Don't enlarge the `connect` method
437
- * @private
438
- * @param {?} options
439
- * @return {?}
440
- */
441
- function (options) {
442
- if (options.url) {
443
- this.config.url = options.url;
444
- }
445
- if (options.serializer) {
446
- this.config.serializer = options.serializer;
447
- }
448
- if (options.deserializer) {
449
- this.config.deserializer = options.deserializer;
450
- }
451
- };
452
- /**
453
- * To ensure we don't have any memory leaks
454
- * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
455
- * then the previous subscription will still live in the memory
456
- * to prevent such behavior - we close the previous connection if it exists
457
- */
458
- /**
459
- * To ensure we don't have any memory leaks
460
- * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
461
- * then the previous subscription will still live in the memory
462
- * to prevent such behavior - we close the previous connection if it exists
463
- * @private
464
- * @return {?}
465
- */
466
- WebSocketHandler.prototype.updateConnection = /**
467
- * To ensure we don't have any memory leaks
468
- * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
469
- * then the previous subscription will still live in the memory
470
- * to prevent such behavior - we close the previous connection if it exists
471
- * @private
472
- * @return {?}
473
- */
474
- function () {
475
- if (this.socket) {
476
- this.closeConnection();
477
- this.store.dispatch(new WebSocketConnectionUpdated());
478
- }
479
- };
480
- /**
481
- * Used in many places so it's better to move the code into function
482
- */
483
- /**
484
- * Used in many places so it's better to move the code into function
485
- * @private
486
- * @return {?}
487
- */
488
- WebSocketHandler.prototype.dispatchWebSocketDisconnected = /**
489
- * Used in many places so it's better to move the code into function
490
- * @private
491
- * @return {?}
492
- */
493
- function () {
494
- this.store.dispatch(new WebSocketDisconnected());
495
- };
496
- /**
497
- * @private
498
- * @return {?}
499
- */
500
- WebSocketHandler.prototype.closeConnection = /**
501
- * @private
502
- * @return {?}
503
- */
504
- function () {
505
- // `socket.complete()` closes the connection
506
- // also it doesn't invoke the `onComplete` callback that we passed
507
- // into `socket.subscribe(...)`
508
- if (this.socket !== null) {
509
- this.socket.complete();
510
- this.socket = null;
511
- }
512
- };
513
- WebSocketHandler.decorators = [
514
- { type: Injectable }
515
- ];
516
- /** @nocollapse */
517
- WebSocketHandler.ctorParameters = function () { return [
518
- { type: Store },
519
- { type: Actions },
520
- { type: undefined, decorators: [{ type: Inject, args: [NGXS_WEBSOCKET_OPTIONS,] }] }
521
- ]; };
522
- return WebSocketHandler;
523
- }());
524
- if (false) {
525
- /**
526
- * @type {?}
527
- * @private
528
- */
529
- WebSocketHandler.prototype.socket;
530
- /**
531
- * @type {?}
532
- * @private
533
- */
534
- WebSocketHandler.prototype.config;
535
- /**
536
- * @type {?}
537
- * @private
538
- */
539
- WebSocketHandler.prototype.typeKey;
540
- /**
541
- * @type {?}
542
- * @private
543
- */
544
- WebSocketHandler.prototype.subscription;
545
- /**
546
- * @type {?}
547
- * @private
548
- */
549
- WebSocketHandler.prototype.store;
550
- /**
551
- * @type {?}
552
- * @private
553
- */
554
- WebSocketHandler.prototype.actions$;
555
- /**
556
- * @type {?}
557
- * @private
558
- */
559
- WebSocketHandler.prototype.options;
264
+ /**
265
+ * @fileoverview added by tsickle
266
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
267
+ */
268
+ var WebSocketHandler = /** @class */ (function () {
269
+ function WebSocketHandler(store, actions$, options) {
270
+ var _this = this;
271
+ this.store = store;
272
+ this.actions$ = actions$;
273
+ this.options = options;
274
+ this.socket = null;
275
+ this.config = {
276
+ url: (/** @type {?} */ (this.options.url)),
277
+ protocol: this.options.protocol,
278
+ // Default binary type is `blob` for the global `WebSocket`
279
+ binaryType: this.options.binaryType,
280
+ serializer: this.options.serializer,
281
+ deserializer: this.options.deserializer,
282
+ closeObserver: {
283
+ next: (/**
284
+ * @return {?}
285
+ */
286
+ function () {
287
+ // ATTENTION!
288
+ // See https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/dom/WebSocketSubject.ts#L340
289
+ // RxJS socket emits `onComplete` event only if `event.wasClean` is truthy
290
+ // and doesn't complete socket subject if it's falsy
291
+ _this.disconnect();
292
+ })
293
+ },
294
+ openObserver: {
295
+ next: (/**
296
+ * @return {?}
297
+ */
298
+ function () { return _this.store.dispatch(new WebSocketConnected()); })
299
+ }
300
+ };
301
+ this.typeKey = (/** @type {?} */ (this.options.typeKey));
302
+ this.setupActionsListeners();
303
+ }
304
+ /**
305
+ * @private
306
+ * @return {?}
307
+ */
308
+ WebSocketHandler.prototype.setupActionsListeners = /**
309
+ * @private
310
+ * @return {?}
311
+ */
312
+ function () {
313
+ var _this = this;
314
+ this.actions$.pipe(ofActionDispatched(ConnectWebSocket)).subscribe((/**
315
+ * @param {?} __0
316
+ * @return {?}
317
+ */
318
+ function (_a) {
319
+ var payload = _a.payload;
320
+ _this.connect(payload);
321
+ }));
322
+ this.actions$.pipe(ofActionDispatched(DisconnectWebSocket)).subscribe((/**
323
+ * @return {?}
324
+ */
325
+ function () {
326
+ _this.disconnect();
327
+ }));
328
+ this.actions$.pipe(ofActionDispatched(SendWebSocketMessage)).subscribe((/**
329
+ * @param {?} __0
330
+ * @return {?}
331
+ */
332
+ function (_a) {
333
+ var payload = _a.payload;
334
+ _this.send(payload);
335
+ }));
336
+ };
337
+ /**
338
+ * @private
339
+ * @param {?=} options
340
+ * @return {?}
341
+ */
342
+ WebSocketHandler.prototype.connect = /**
343
+ * @private
344
+ * @param {?=} options
345
+ * @return {?}
346
+ */
347
+ function (options) {
348
+ var _this = this;
349
+ this.updateConnection();
350
+ // Users can pass the options in the connect method so
351
+ // if options aren't available at DI bootstrap they have access
352
+ // to pass them here
353
+ if (options) {
354
+ this.mergeConfigWithOptions(options);
355
+ }
356
+ this.socket = new WebSocketSubject(this.config);
357
+ this.socket.subscribe({
358
+ next: (/**
359
+ * @param {?} message
360
+ * @return {?}
361
+ */
362
+ function (message) {
363
+ /** @type {?} */
364
+ var type = getValue(message, _this.typeKey);
365
+ if (!type) {
366
+ throw new TypeKeyPropertyMissingError(_this.typeKey);
367
+ }
368
+ _this.store.dispatch(__assign({}, message, { type: type }));
369
+ }),
370
+ error: (/**
371
+ * @param {?} error
372
+ * @return {?}
373
+ */
374
+ function (error) {
375
+ if (error instanceof CloseEvent) {
376
+ _this.dispatchWebSocketDisconnected();
377
+ }
378
+ else {
379
+ _this.store.dispatch(new WebsocketMessageError(error));
380
+ }
381
+ })
382
+ });
383
+ };
384
+ /**
385
+ * @private
386
+ * @return {?}
387
+ */
388
+ WebSocketHandler.prototype.disconnect = /**
389
+ * @private
390
+ * @return {?}
391
+ */
392
+ function () {
393
+ if (this.socket) {
394
+ // `socket.complete()` closes the connection
395
+ // also it doesn't invoke the `onComplete` callback that we passed
396
+ // into `socket.subscribe(...)`
397
+ this.socket.complete();
398
+ this.socket = null;
399
+ this.dispatchWebSocketDisconnected();
400
+ }
401
+ };
402
+ /**
403
+ * @private
404
+ * @param {?} data
405
+ * @return {?}
406
+ */
407
+ WebSocketHandler.prototype.send = /**
408
+ * @private
409
+ * @param {?} data
410
+ * @return {?}
411
+ */
412
+ function (data) {
413
+ if (!this.socket) {
414
+ throw new Error('You must connect to the socket before sending any data');
415
+ }
416
+ this.socket.next(data);
417
+ };
418
+ /**
419
+ * Don't enlarge the `connect` method
420
+ */
421
+ /**
422
+ * Don't enlarge the `connect` method
423
+ * @private
424
+ * @param {?} options
425
+ * @return {?}
426
+ */
427
+ WebSocketHandler.prototype.mergeConfigWithOptions = /**
428
+ * Don't enlarge the `connect` method
429
+ * @private
430
+ * @param {?} options
431
+ * @return {?}
432
+ */
433
+ function (options) {
434
+ if (options.url) {
435
+ this.config.url = options.url;
436
+ }
437
+ if (options.serializer) {
438
+ this.config.serializer = options.serializer;
439
+ }
440
+ if (options.deserializer) {
441
+ this.config.deserializer = options.deserializer;
442
+ }
443
+ };
444
+ /**
445
+ * To ensure we don't have any memory leaks
446
+ * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
447
+ * then the previous subscription will still live in the memory
448
+ * to prevent such behavior - we close the previous connection if it exists
449
+ */
450
+ /**
451
+ * To ensure we don't have any memory leaks
452
+ * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
453
+ * then the previous subscription will still live in the memory
454
+ * to prevent such behavior - we close the previous connection if it exists
455
+ * @private
456
+ * @return {?}
457
+ */
458
+ WebSocketHandler.prototype.updateConnection = /**
459
+ * To ensure we don't have any memory leaks
460
+ * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
461
+ * then the previous subscription will still live in the memory
462
+ * to prevent such behavior - we close the previous connection if it exists
463
+ * @private
464
+ * @return {?}
465
+ */
466
+ function () {
467
+ if (this.socket) {
468
+ this.socket.complete();
469
+ this.socket = null;
470
+ this.store.dispatch(new WebSocketConnectionUpdated());
471
+ }
472
+ };
473
+ /**
474
+ * Used in many places so it's better to move the code into function
475
+ */
476
+ /**
477
+ * Used in many places so it's better to move the code into function
478
+ * @private
479
+ * @return {?}
480
+ */
481
+ WebSocketHandler.prototype.dispatchWebSocketDisconnected = /**
482
+ * Used in many places so it's better to move the code into function
483
+ * @private
484
+ * @return {?}
485
+ */
486
+ function () {
487
+ this.store.dispatch(new WebSocketDisconnected());
488
+ };
489
+ WebSocketHandler.decorators = [
490
+ { type: Injectable }
491
+ ];
492
+ /** @nocollapse */
493
+ WebSocketHandler.ctorParameters = function () { return [
494
+ { type: Store },
495
+ { type: Actions },
496
+ { type: undefined, decorators: [{ type: Inject, args: [NGXS_WEBSOCKET_OPTIONS,] }] }
497
+ ]; };
498
+ return WebSocketHandler;
499
+ }());
500
+ if (false) {
501
+ /**
502
+ * @type {?}
503
+ * @private
504
+ */
505
+ WebSocketHandler.prototype.socket;
506
+ /**
507
+ * @type {?}
508
+ * @private
509
+ */
510
+ WebSocketHandler.prototype.config;
511
+ /**
512
+ * @type {?}
513
+ * @private
514
+ */
515
+ WebSocketHandler.prototype.typeKey;
516
+ /**
517
+ * @type {?}
518
+ * @private
519
+ */
520
+ WebSocketHandler.prototype.store;
521
+ /**
522
+ * @type {?}
523
+ * @private
524
+ */
525
+ WebSocketHandler.prototype.actions$;
526
+ /**
527
+ * @type {?}
528
+ * @private
529
+ */
530
+ WebSocketHandler.prototype.options;
560
531
  }
561
532
 
562
- /**
563
- * @fileoverview added by tsickle
564
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
565
- */
566
- /**
567
- * @param {?} options
568
- * @return {?}
569
- */
570
- function websocketOptionsFactory(options) {
571
- return __assign({ reconnectInterval: 5000, reconnectAttempts: 10, typeKey: 'type', deserializer: /**
572
- * @param {?} e
573
- * @return {?}
574
- */
575
- function (e) {
576
- return JSON.parse(e.data);
577
- },
578
- serializer: /**
579
- * @param {?} value
580
- * @return {?}
581
- */
582
- function (value) {
583
- return JSON.stringify(value);
584
- } }, options);
585
- }
586
- /** @type {?} */
587
- var USER_OPTIONS = new InjectionToken('USER_OPTIONS');
588
- var NgxsWebsocketPluginModule = /** @class */ (function () {
589
- function NgxsWebsocketPluginModule() {
590
- }
591
- /**
592
- * @param {?=} options
593
- * @return {?}
594
- */
595
- NgxsWebsocketPluginModule.forRoot = /**
596
- * @param {?=} options
597
- * @return {?}
598
- */
599
- function (options) {
600
- return {
601
- ngModule: NgxsWebsocketPluginModule,
602
- providers: [
603
- WebSocketHandler,
604
- {
605
- provide: USER_OPTIONS,
606
- useValue: options
607
- },
608
- {
609
- provide: NGXS_WEBSOCKET_OPTIONS,
610
- useFactory: websocketOptionsFactory,
611
- deps: [USER_OPTIONS]
612
- },
613
- {
614
- provide: APP_INITIALIZER,
615
- useFactory: noop,
616
- deps: [WebSocketHandler],
617
- multi: true
618
- }
619
- ]
620
- };
621
- };
622
- NgxsWebsocketPluginModule.decorators = [
623
- { type: NgModule }
624
- ];
625
- return NgxsWebsocketPluginModule;
533
+ /**
534
+ * @fileoverview added by tsickle
535
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
536
+ */
537
+ /**
538
+ * @param {?} options
539
+ * @return {?}
540
+ */
541
+ function websocketOptionsFactory(options) {
542
+ return __assign({ reconnectInterval: 5000, reconnectAttempts: 10, typeKey: 'type', deserializer: /**
543
+ * @param {?} e
544
+ * @return {?}
545
+ */
546
+ function (e) {
547
+ return JSON.parse(e.data);
548
+ },
549
+ serializer: /**
550
+ * @param {?} value
551
+ * @return {?}
552
+ */
553
+ function (value) {
554
+ return JSON.stringify(value);
555
+ } }, options);
556
+ }
557
+ /** @type {?} */
558
+ var USER_OPTIONS = new InjectionToken('USER_OPTIONS');
559
+ var NgxsWebsocketPluginModule = /** @class */ (function () {
560
+ function NgxsWebsocketPluginModule() {
561
+ }
562
+ /**
563
+ * @param {?=} options
564
+ * @return {?}
565
+ */
566
+ NgxsWebsocketPluginModule.forRoot = /**
567
+ * @param {?=} options
568
+ * @return {?}
569
+ */
570
+ function (options) {
571
+ return {
572
+ ngModule: NgxsWebsocketPluginModule,
573
+ providers: [
574
+ WebSocketHandler,
575
+ {
576
+ provide: USER_OPTIONS,
577
+ useValue: options
578
+ },
579
+ {
580
+ provide: NGXS_WEBSOCKET_OPTIONS,
581
+ useFactory: websocketOptionsFactory,
582
+ deps: [USER_OPTIONS]
583
+ },
584
+ {
585
+ provide: APP_INITIALIZER,
586
+ useFactory: noop,
587
+ deps: [WebSocketHandler],
588
+ multi: true
589
+ }
590
+ ]
591
+ };
592
+ };
593
+ NgxsWebsocketPluginModule.decorators = [
594
+ { type: NgModule }
595
+ ];
596
+ return NgxsWebsocketPluginModule;
626
597
  }());
627
598
 
628
- /**
629
- * @fileoverview added by tsickle
630
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
599
+ /**
600
+ * @fileoverview added by tsickle
601
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
631
602
  */
632
603
 
633
- /**
634
- * @fileoverview added by tsickle
635
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
604
+ /**
605
+ * @fileoverview added by tsickle
606
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
636
607
  */
637
608
 
638
- /**
639
- * @fileoverview added by tsickle
640
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
609
+ /**
610
+ * @fileoverview added by tsickle
611
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
641
612
  */
642
613
 
643
614
  export { ConnectWebSocket, DisconnectWebSocket, NGXS_WEBSOCKET_OPTIONS, NgxsWebsocketPluginModule, SendWebSocketMessage, WebSocketConnected, WebSocketConnectionUpdated, WebSocketDisconnected, WebsocketMessageError, websocketOptionsFactory as ɵa, USER_OPTIONS as ɵb, noop as ɵc, WebSocketHandler as ɵd };