@ngxs/websocket-plugin 3.7.4 → 3.7.5-dev.master-887cc32

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.
@@ -3,514 +3,514 @@ import { ofActionDispatched, getValue, Store, Actions } from '@ngxs/store';
3
3
  import { Subscription } from 'rxjs';
4
4
  import { WebSocketSubject } from 'rxjs/webSocket';
5
5
 
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
- }
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
+ }
200
200
  }
201
201
 
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;
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;
440
440
  }
441
441
 
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 }
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 }
499
499
  ];
500
500
 
501
- /**
502
- * @fileoverview added by tsickle
503
- * @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
504
504
  */
505
505
 
506
- /**
507
- * @fileoverview added by tsickle
508
- * @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
509
509
  */
510
510
 
511
- /**
512
- * @fileoverview added by tsickle
513
- * @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
514
514
  */
515
515
 
516
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 };