@ngxs/websocket-plugin 3.7.3 → 3.7.4-dev.master-327646e

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