@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,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@ngxs/store'), require('rxjs/webSocket')) :
3
- typeof define === 'function' && define.amd ? define('@ngxs/websocket-plugin', ['exports', '@angular/core', '@ngxs/store', 'rxjs/webSocket'], factory) :
4
- (global = global || self, factory((global.ngxs = global.ngxs || {}, global.ngxs['websocket-plugin'] = {}), global.ng.core, global['ngxs-store'], global.rxjs.webSocket));
5
- }(this, function (exports, core, store, webSocket) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@ngxs/store'), require('rxjs'), require('rxjs/webSocket')) :
3
+ typeof define === 'function' && define.amd ? define('@ngxs/websocket-plugin', ['exports', '@angular/core', '@ngxs/store', 'rxjs', 'rxjs/webSocket'], factory) :
4
+ (global = global || self, factory((global.ngxs = global.ngxs || {}, global.ngxs['websocket-plugin'] = {}), global.ng.core, global['ngxs-store'], global.rxjs, global.rxjs.webSocket));
5
+ }(this, function (exports, core, store, rxjs, webSocket) { 'use strict';
6
6
 
7
7
  /*! *****************************************************************************
8
8
  Copyright (c) Microsoft Corporation.
@@ -223,597 +223,625 @@
223
223
  return value;
224
224
  }
225
225
 
226
- /**
227
- * @fileoverview added by tsickle
228
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
229
- */
230
- /** @type {?} */
231
- var NGXS_WEBSOCKET_OPTIONS = new core.InjectionToken('NGXS_WEBSOCKET_OPTIONS');
232
- /**
233
- * @record
234
- */
235
- function NgxsWebsocketPluginOptions() { }
236
- if (false) {
237
- /**
238
- * URL of the websocket.
239
- * @type {?|undefined}
240
- */
241
- NgxsWebsocketPluginOptions.prototype.url;
242
- /**
243
- * Either a single protocol string or an array of protocol strings.
244
- * These strings are used to indicate sub-protocols, so that a single server
245
- * can implement multiple WebSocket sub-protocols (for example, you might want one server to be able
246
- * to handle different types of interactions depending on the specified protocol).
247
- * If you don't specify a protocol string, an empty string is assumed.
248
- * @type {?|undefined}
249
- */
250
- NgxsWebsocketPluginOptions.prototype.protocol;
251
- /**
252
- * Sets the `binaryType` property of the underlying WebSocket.
253
- * @type {?|undefined}
254
- */
255
- NgxsWebsocketPluginOptions.prototype.binaryType;
256
- /**
257
- * The property name to distigunish this type for the store.
258
- * Default: 'type'
259
- * @type {?|undefined}
260
- */
261
- NgxsWebsocketPluginOptions.prototype.typeKey;
262
- /**
263
- * Interval to try and reconnect.
264
- * Default: 5000
265
- * @type {?|undefined}
266
- */
267
- NgxsWebsocketPluginOptions.prototype.reconnectInterval;
268
- /**
269
- * Number of reconnect attemps.
270
- * Default: 10
271
- * @type {?|undefined}
272
- */
273
- NgxsWebsocketPluginOptions.prototype.reconnectAttempts;
274
- /**
275
- * Serializer to call before sending messages
276
- * Default: `json.stringify`
277
- * @type {?|undefined}
278
- */
279
- NgxsWebsocketPluginOptions.prototype.serializer;
280
- /**
281
- * Deseralizer before publishing the message.
282
- * @type {?|undefined}
283
- */
284
- NgxsWebsocketPluginOptions.prototype.deserializer;
285
- }
286
- /**
287
- * @param {...?} _args
288
- * @return {?}
289
- */
290
- function noop() {
291
- var _args = [];
292
- for (var _i = 0; _i < arguments.length; _i++) {
293
- _args[_i] = arguments[_i];
294
- }
295
- return (/**
296
- * @return {?}
297
- */
298
- function () { });
299
- }
300
- /**
301
- * Action to connect to the websocket. Optionally pass a URL.
302
- */
303
- var /**
304
- * Action to connect to the websocket. Optionally pass a URL.
305
- */
306
- ConnectWebSocket = /** @class */ (function () {
307
- function ConnectWebSocket(payload) {
308
- this.payload = payload;
309
- }
310
- Object.defineProperty(ConnectWebSocket, "type", {
311
- get: /**
312
- * @return {?}
313
- */
314
- function () {
315
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
316
- return '[WebSocket] Connect';
317
- },
318
- enumerable: true,
319
- configurable: true
320
- });
321
- return ConnectWebSocket;
322
- }());
323
- if (false) {
324
- /** @type {?} */
325
- ConnectWebSocket.prototype.payload;
326
- }
327
- /**
328
- * Action triggered when a error ocurrs
329
- */
330
- var /**
331
- * Action triggered when a error ocurrs
332
- */
333
- WebsocketMessageError = /** @class */ (function () {
334
- function WebsocketMessageError(payload) {
335
- this.payload = payload;
336
- }
337
- Object.defineProperty(WebsocketMessageError, "type", {
338
- get: /**
339
- * @return {?}
340
- */
341
- function () {
342
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
343
- return '[WebSocket] Message Error';
344
- },
345
- enumerable: true,
346
- configurable: true
347
- });
348
- return WebsocketMessageError;
349
- }());
350
- if (false) {
351
- /** @type {?} */
352
- WebsocketMessageError.prototype.payload;
353
- }
354
- /**
355
- * Action to disconnect the websocket.
356
- */
357
- var /**
358
- * Action to disconnect the websocket.
359
- */
360
- DisconnectWebSocket = /** @class */ (function () {
361
- function DisconnectWebSocket() {
362
- }
363
- Object.defineProperty(DisconnectWebSocket, "type", {
364
- get: /**
365
- * @return {?}
366
- */
367
- function () {
368
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
369
- return '[WebSocket] Disconnect';
370
- },
371
- enumerable: true,
372
- configurable: true
373
- });
374
- return DisconnectWebSocket;
375
- }());
376
- /**
377
- * Action triggered when websocket is connected
378
- */
379
- var /**
380
- * Action triggered when websocket is connected
381
- */
382
- WebSocketConnected = /** @class */ (function () {
383
- function WebSocketConnected() {
384
- }
385
- Object.defineProperty(WebSocketConnected, "type", {
386
- get: /**
387
- * @return {?}
388
- */
389
- function () {
390
- return '[WebSocket] Connected';
391
- },
392
- enumerable: true,
393
- configurable: true
394
- });
395
- return WebSocketConnected;
396
- }());
397
- /**
398
- * Action triggered when websocket is disconnected
399
- */
400
- var /**
401
- * Action triggered when websocket is disconnected
402
- */
403
- WebSocketDisconnected = /** @class */ (function () {
404
- function WebSocketDisconnected() {
405
- }
406
- Object.defineProperty(WebSocketDisconnected, "type", {
407
- get: /**
408
- * @return {?}
409
- */
410
- function () {
411
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
412
- return '[WebSocket] Disconnected';
413
- },
414
- enumerable: true,
415
- configurable: true
416
- });
417
- return WebSocketDisconnected;
418
- }());
419
- /**
420
- * Action to send to the server.
421
- */
422
- var /**
423
- * Action to send to the server.
424
- */
425
- SendWebSocketMessage = /** @class */ (function () {
426
- function SendWebSocketMessage(payload) {
427
- this.payload = payload;
428
- }
429
- Object.defineProperty(SendWebSocketMessage, "type", {
430
- get: /**
431
- * @return {?}
432
- */
433
- function () {
434
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
435
- return '[WebSocket] Send Message';
436
- },
437
- enumerable: true,
438
- configurable: true
439
- });
440
- return SendWebSocketMessage;
441
- }());
442
- if (false) {
443
- /** @type {?} */
444
- SendWebSocketMessage.prototype.payload;
445
- }
446
- /**
447
- * Action dispatched when the user tries to connect if the connection already exists.
448
- */
449
- var /**
450
- * Action dispatched when the user tries to connect if the connection already exists.
451
- */
452
- WebSocketConnectionUpdated = /** @class */ (function () {
453
- function WebSocketConnectionUpdated() {
454
- }
455
- Object.defineProperty(WebSocketConnectionUpdated, "type", {
456
- get: /**
457
- * @return {?}
458
- */
459
- function () {
460
- // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
461
- return '[WebSocket] Connection Updated';
462
- },
463
- enumerable: true,
464
- configurable: true
465
- });
466
- return WebSocketConnectionUpdated;
467
- }());
468
- /**
469
- * This error is thrown where there is no `type` (or custom `typeKey`) property
470
- * on the message that came from the server side socket
471
- */
472
- var /**
473
- * This error is thrown where there is no `type` (or custom `typeKey`) property
474
- * on the message that came from the server side socket
475
- */
476
- TypeKeyPropertyMissingError = /** @class */ (function (_super) {
477
- __extends(TypeKeyPropertyMissingError, _super);
478
- function TypeKeyPropertyMissingError(typeKey) {
479
- return _super.call(this, "Property " + typeKey + " is missing on the socket message") || this;
480
- }
481
- return TypeKeyPropertyMissingError;
226
+ /**
227
+ * @fileoverview added by tsickle
228
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
229
+ */
230
+ /** @type {?} */
231
+ var NGXS_WEBSOCKET_OPTIONS = new core.InjectionToken('NGXS_WEBSOCKET_OPTIONS');
232
+ /**
233
+ * @record
234
+ */
235
+ function NgxsWebsocketPluginOptions() { }
236
+ if (false) {
237
+ /**
238
+ * URL of the websocket.
239
+ * @type {?|undefined}
240
+ */
241
+ NgxsWebsocketPluginOptions.prototype.url;
242
+ /**
243
+ * Either a single protocol string or an array of protocol strings.
244
+ * These strings are used to indicate sub-protocols, so that a single server
245
+ * can implement multiple WebSocket sub-protocols (for example, you might want one server to be able
246
+ * to handle different types of interactions depending on the specified protocol).
247
+ * If you don't specify a protocol string, an empty string is assumed.
248
+ * @type {?|undefined}
249
+ */
250
+ NgxsWebsocketPluginOptions.prototype.protocol;
251
+ /**
252
+ * Sets the `binaryType` property of the underlying WebSocket.
253
+ * @type {?|undefined}
254
+ */
255
+ NgxsWebsocketPluginOptions.prototype.binaryType;
256
+ /**
257
+ * The property name to distigunish this type for the store.
258
+ * Default: 'type'
259
+ * @type {?|undefined}
260
+ */
261
+ NgxsWebsocketPluginOptions.prototype.typeKey;
262
+ /**
263
+ * Interval to try and reconnect.
264
+ * Default: 5000
265
+ * @type {?|undefined}
266
+ */
267
+ NgxsWebsocketPluginOptions.prototype.reconnectInterval;
268
+ /**
269
+ * Number of reconnect attemps.
270
+ * Default: 10
271
+ * @type {?|undefined}
272
+ */
273
+ NgxsWebsocketPluginOptions.prototype.reconnectAttempts;
274
+ /**
275
+ * Serializer to call before sending messages
276
+ * Default: `json.stringify`
277
+ * @type {?|undefined}
278
+ */
279
+ NgxsWebsocketPluginOptions.prototype.serializer;
280
+ /**
281
+ * Deseralizer before publishing the message.
282
+ * @type {?|undefined}
283
+ */
284
+ NgxsWebsocketPluginOptions.prototype.deserializer;
285
+ }
286
+ /**
287
+ * @param {...?} _args
288
+ * @return {?}
289
+ */
290
+ function noop() {
291
+ var _args = [];
292
+ for (var _i = 0; _i < arguments.length; _i++) {
293
+ _args[_i] = arguments[_i];
294
+ }
295
+ return (/**
296
+ * @return {?}
297
+ */
298
+ function () { });
299
+ }
300
+ /**
301
+ * Action to connect to the websocket. Optionally pass a URL.
302
+ */
303
+ var /**
304
+ * Action to connect to the websocket. Optionally pass a URL.
305
+ */
306
+ ConnectWebSocket = /** @class */ (function () {
307
+ function ConnectWebSocket(payload) {
308
+ this.payload = payload;
309
+ }
310
+ Object.defineProperty(ConnectWebSocket, "type", {
311
+ get: /**
312
+ * @return {?}
313
+ */
314
+ function () {
315
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
316
+ return '[WebSocket] Connect';
317
+ },
318
+ enumerable: true,
319
+ configurable: true
320
+ });
321
+ return ConnectWebSocket;
322
+ }());
323
+ if (false) {
324
+ /** @type {?} */
325
+ ConnectWebSocket.prototype.payload;
326
+ }
327
+ /**
328
+ * Action triggered when a error ocurrs
329
+ */
330
+ var /**
331
+ * Action triggered when a error ocurrs
332
+ */
333
+ WebsocketMessageError = /** @class */ (function () {
334
+ function WebsocketMessageError(payload) {
335
+ this.payload = payload;
336
+ }
337
+ Object.defineProperty(WebsocketMessageError, "type", {
338
+ get: /**
339
+ * @return {?}
340
+ */
341
+ function () {
342
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
343
+ return '[WebSocket] Message Error';
344
+ },
345
+ enumerable: true,
346
+ configurable: true
347
+ });
348
+ return WebsocketMessageError;
349
+ }());
350
+ if (false) {
351
+ /** @type {?} */
352
+ WebsocketMessageError.prototype.payload;
353
+ }
354
+ /**
355
+ * Action to disconnect the websocket.
356
+ */
357
+ var /**
358
+ * Action to disconnect the websocket.
359
+ */
360
+ DisconnectWebSocket = /** @class */ (function () {
361
+ function DisconnectWebSocket() {
362
+ }
363
+ Object.defineProperty(DisconnectWebSocket, "type", {
364
+ get: /**
365
+ * @return {?}
366
+ */
367
+ function () {
368
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
369
+ return '[WebSocket] Disconnect';
370
+ },
371
+ enumerable: true,
372
+ configurable: true
373
+ });
374
+ return DisconnectWebSocket;
375
+ }());
376
+ /**
377
+ * Action triggered when websocket is connected
378
+ */
379
+ var /**
380
+ * Action triggered when websocket is connected
381
+ */
382
+ WebSocketConnected = /** @class */ (function () {
383
+ function WebSocketConnected() {
384
+ }
385
+ Object.defineProperty(WebSocketConnected, "type", {
386
+ get: /**
387
+ * @return {?}
388
+ */
389
+ function () {
390
+ return '[WebSocket] Connected';
391
+ },
392
+ enumerable: true,
393
+ configurable: true
394
+ });
395
+ return WebSocketConnected;
396
+ }());
397
+ /**
398
+ * Action triggered when websocket is disconnected
399
+ */
400
+ var /**
401
+ * Action triggered when websocket is disconnected
402
+ */
403
+ WebSocketDisconnected = /** @class */ (function () {
404
+ function WebSocketDisconnected() {
405
+ }
406
+ Object.defineProperty(WebSocketDisconnected, "type", {
407
+ get: /**
408
+ * @return {?}
409
+ */
410
+ function () {
411
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
412
+ return '[WebSocket] Disconnected';
413
+ },
414
+ enumerable: true,
415
+ configurable: true
416
+ });
417
+ return WebSocketDisconnected;
418
+ }());
419
+ /**
420
+ * Action to send to the server.
421
+ */
422
+ var /**
423
+ * Action to send to the server.
424
+ */
425
+ SendWebSocketMessage = /** @class */ (function () {
426
+ function SendWebSocketMessage(payload) {
427
+ this.payload = payload;
428
+ }
429
+ Object.defineProperty(SendWebSocketMessage, "type", {
430
+ get: /**
431
+ * @return {?}
432
+ */
433
+ function () {
434
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
435
+ return '[WebSocket] Send Message';
436
+ },
437
+ enumerable: true,
438
+ configurable: true
439
+ });
440
+ return SendWebSocketMessage;
441
+ }());
442
+ if (false) {
443
+ /** @type {?} */
444
+ SendWebSocketMessage.prototype.payload;
445
+ }
446
+ /**
447
+ * Action dispatched when the user tries to connect if the connection already exists.
448
+ */
449
+ var /**
450
+ * Action dispatched when the user tries to connect if the connection already exists.
451
+ */
452
+ WebSocketConnectionUpdated = /** @class */ (function () {
453
+ function WebSocketConnectionUpdated() {
454
+ }
455
+ Object.defineProperty(WebSocketConnectionUpdated, "type", {
456
+ get: /**
457
+ * @return {?}
458
+ */
459
+ function () {
460
+ // NOTE: Not necessary to declare the type in this way in your code. See https://github.com/ngxs/store/pull/644#issuecomment-436003138
461
+ return '[WebSocket] Connection Updated';
462
+ },
463
+ enumerable: true,
464
+ configurable: true
465
+ });
466
+ return WebSocketConnectionUpdated;
467
+ }());
468
+ /**
469
+ * This error is thrown where there is no `type` (or custom `typeKey`) property
470
+ * on the message that came from the server side socket
471
+ */
472
+ var /**
473
+ * This error is thrown where there is no `type` (or custom `typeKey`) property
474
+ * on the message that came from the server side socket
475
+ */
476
+ TypeKeyPropertyMissingError = /** @class */ (function (_super) {
477
+ __extends(TypeKeyPropertyMissingError, _super);
478
+ function TypeKeyPropertyMissingError(typeKey) {
479
+ return _super.call(this, "Property " + typeKey + " is missing on the socket message") || this;
480
+ }
481
+ return TypeKeyPropertyMissingError;
482
482
  }(Error));
483
483
 
484
- /**
485
- * @fileoverview added by tsickle
486
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
487
- */
488
- var WebSocketHandler = /** @class */ (function () {
489
- function WebSocketHandler(store, actions$, options) {
490
- var _this = this;
491
- this.store = store;
492
- this.actions$ = actions$;
493
- this.options = options;
494
- this.socket = null;
495
- this.config = {
496
- url: (/** @type {?} */ (this.options.url)),
497
- protocol: this.options.protocol,
498
- // Default binary type is `blob` for the global `WebSocket`
499
- binaryType: this.options.binaryType,
500
- serializer: this.options.serializer,
501
- deserializer: this.options.deserializer,
502
- closeObserver: {
503
- next: (/**
504
- * @return {?}
505
- */
506
- function () {
507
- // ATTENTION!
508
- // See https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/dom/WebSocketSubject.ts#L340
509
- // RxJS socket emits `onComplete` event only if `event.wasClean` is truthy
510
- // and doesn't complete socket subject if it's falsy
511
- _this.disconnect();
512
- })
513
- },
514
- openObserver: {
515
- next: (/**
516
- * @return {?}
517
- */
518
- function () { return _this.store.dispatch(new WebSocketConnected()); })
519
- }
520
- };
521
- this.typeKey = (/** @type {?} */ (this.options.typeKey));
522
- this.setupActionsListeners();
523
- }
524
- /**
525
- * @private
526
- * @return {?}
527
- */
528
- WebSocketHandler.prototype.setupActionsListeners = /**
529
- * @private
530
- * @return {?}
531
- */
532
- function () {
533
- var _this = this;
534
- this.actions$.pipe(store.ofActionDispatched(ConnectWebSocket)).subscribe((/**
535
- * @param {?} __0
536
- * @return {?}
537
- */
538
- function (_a) {
539
- var payload = _a.payload;
540
- _this.connect(payload);
541
- }));
542
- this.actions$.pipe(store.ofActionDispatched(DisconnectWebSocket)).subscribe((/**
543
- * @return {?}
544
- */
545
- function () {
546
- _this.disconnect();
547
- }));
548
- this.actions$.pipe(store.ofActionDispatched(SendWebSocketMessage)).subscribe((/**
549
- * @param {?} __0
550
- * @return {?}
551
- */
552
- function (_a) {
553
- var payload = _a.payload;
554
- _this.send(payload);
555
- }));
556
- };
557
- /**
558
- * @private
559
- * @param {?=} options
560
- * @return {?}
561
- */
562
- WebSocketHandler.prototype.connect = /**
563
- * @private
564
- * @param {?=} options
565
- * @return {?}
566
- */
567
- function (options) {
568
- var _this = this;
569
- this.updateConnection();
570
- // Users can pass the options in the connect method so
571
- // if options aren't available at DI bootstrap they have access
572
- // to pass them here
573
- if (options) {
574
- this.mergeConfigWithOptions(options);
575
- }
576
- this.socket = new webSocket.WebSocketSubject(this.config);
577
- this.socket.subscribe({
578
- next: (/**
579
- * @param {?} message
580
- * @return {?}
581
- */
582
- function (message) {
583
- /** @type {?} */
584
- var type = store.getValue(message, _this.typeKey);
585
- if (!type) {
586
- throw new TypeKeyPropertyMissingError(_this.typeKey);
587
- }
588
- _this.store.dispatch(__assign({}, message, { type: type }));
589
- }),
590
- error: (/**
591
- * @param {?} error
592
- * @return {?}
593
- */
594
- function (error) {
595
- if (error instanceof CloseEvent) {
596
- _this.dispatchWebSocketDisconnected();
597
- }
598
- else {
599
- _this.store.dispatch(new WebsocketMessageError(error));
600
- }
601
- })
602
- });
603
- };
604
- /**
605
- * @private
606
- * @return {?}
607
- */
608
- WebSocketHandler.prototype.disconnect = /**
609
- * @private
610
- * @return {?}
611
- */
612
- function () {
613
- if (this.socket) {
614
- // `socket.complete()` closes the connection
615
- // also it doesn't invoke the `onComplete` callback that we passed
616
- // into `socket.subscribe(...)`
617
- this.socket.complete();
618
- this.socket = null;
619
- this.dispatchWebSocketDisconnected();
620
- }
621
- };
622
- /**
623
- * @private
624
- * @param {?} data
625
- * @return {?}
626
- */
627
- WebSocketHandler.prototype.send = /**
628
- * @private
629
- * @param {?} data
630
- * @return {?}
631
- */
632
- function (data) {
633
- if (!this.socket) {
634
- throw new Error('You must connect to the socket before sending any data');
635
- }
636
- this.socket.next(data);
637
- };
638
- /**
639
- * Don't enlarge the `connect` method
640
- */
641
- /**
642
- * Don't enlarge the `connect` method
643
- * @private
644
- * @param {?} options
645
- * @return {?}
646
- */
647
- WebSocketHandler.prototype.mergeConfigWithOptions = /**
648
- * Don't enlarge the `connect` method
649
- * @private
650
- * @param {?} options
651
- * @return {?}
652
- */
653
- function (options) {
654
- if (options.url) {
655
- this.config.url = options.url;
656
- }
657
- if (options.serializer) {
658
- this.config.serializer = options.serializer;
659
- }
660
- if (options.deserializer) {
661
- this.config.deserializer = options.deserializer;
662
- }
663
- };
664
- /**
665
- * To ensure we don't have any memory leaks
666
- * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
667
- * then the previous subscription will still live in the memory
668
- * to prevent such behavior - we close the previous connection if it exists
669
- */
670
- /**
671
- * To ensure we don't have any memory leaks
672
- * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
673
- * then the previous subscription will still live in the memory
674
- * to prevent such behavior - we close the previous connection if it exists
675
- * @private
676
- * @return {?}
677
- */
678
- WebSocketHandler.prototype.updateConnection = /**
679
- * To ensure we don't have any memory leaks
680
- * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
681
- * then the previous subscription will still live in the memory
682
- * to prevent such behavior - we close the previous connection if it exists
683
- * @private
684
- * @return {?}
685
- */
686
- function () {
687
- if (this.socket) {
688
- this.socket.complete();
689
- this.socket = null;
690
- this.store.dispatch(new WebSocketConnectionUpdated());
691
- }
692
- };
693
- /**
694
- * Used in many places so it's better to move the code into function
695
- */
696
- /**
697
- * Used in many places so it's better to move the code into function
698
- * @private
699
- * @return {?}
700
- */
701
- WebSocketHandler.prototype.dispatchWebSocketDisconnected = /**
702
- * Used in many places so it's better to move the code into function
703
- * @private
704
- * @return {?}
705
- */
706
- function () {
707
- this.store.dispatch(new WebSocketDisconnected());
708
- };
709
- WebSocketHandler.decorators = [
710
- { type: core.Injectable }
711
- ];
712
- /** @nocollapse */
713
- WebSocketHandler.ctorParameters = function () { return [
714
- { type: store.Store },
715
- { type: store.Actions },
716
- { type: undefined, decorators: [{ type: core.Inject, args: [NGXS_WEBSOCKET_OPTIONS,] }] }
717
- ]; };
718
- return WebSocketHandler;
719
- }());
720
- if (false) {
721
- /**
722
- * @type {?}
723
- * @private
724
- */
725
- WebSocketHandler.prototype.socket;
726
- /**
727
- * @type {?}
728
- * @private
729
- */
730
- WebSocketHandler.prototype.config;
731
- /**
732
- * @type {?}
733
- * @private
734
- */
735
- WebSocketHandler.prototype.typeKey;
736
- /**
737
- * @type {?}
738
- * @private
739
- */
740
- WebSocketHandler.prototype.store;
741
- /**
742
- * @type {?}
743
- * @private
744
- */
745
- WebSocketHandler.prototype.actions$;
746
- /**
747
- * @type {?}
748
- * @private
749
- */
750
- WebSocketHandler.prototype.options;
484
+ /**
485
+ * @fileoverview added by tsickle
486
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
487
+ */
488
+ var WebSocketHandler = /** @class */ (function () {
489
+ function WebSocketHandler(store, actions$, options) {
490
+ var _this = this;
491
+ this.store = store;
492
+ this.actions$ = actions$;
493
+ this.options = options;
494
+ this.socket = null;
495
+ this.config = {
496
+ url: (/** @type {?} */ (this.options.url)),
497
+ protocol: this.options.protocol,
498
+ // Default binary type is `blob` for the global `WebSocket`
499
+ binaryType: this.options.binaryType,
500
+ serializer: this.options.serializer,
501
+ deserializer: this.options.deserializer,
502
+ closeObserver: {
503
+ next: (/**
504
+ * @return {?}
505
+ */
506
+ function () {
507
+ // ATTENTION!
508
+ // See https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/dom/WebSocketSubject.ts#L340
509
+ // RxJS socket emits `onComplete` event only if `event.wasClean` is truthy
510
+ // and doesn't complete socket subject if it's falsy
511
+ _this.disconnect();
512
+ })
513
+ },
514
+ openObserver: {
515
+ next: (/**
516
+ * @return {?}
517
+ */
518
+ function () { return _this.store.dispatch(new WebSocketConnected()); })
519
+ }
520
+ };
521
+ this.typeKey = (/** @type {?} */ (this.options.typeKey));
522
+ this.subscription = new rxjs.Subscription();
523
+ this.setupActionsListeners();
524
+ }
525
+ /**
526
+ * @return {?}
527
+ */
528
+ WebSocketHandler.prototype.ngOnDestroy = /**
529
+ * @return {?}
530
+ */
531
+ function () {
532
+ this.closeConnection();
533
+ this.subscription.unsubscribe();
534
+ };
535
+ /**
536
+ * @private
537
+ * @return {?}
538
+ */
539
+ WebSocketHandler.prototype.setupActionsListeners = /**
540
+ * @private
541
+ * @return {?}
542
+ */
543
+ function () {
544
+ var _this = this;
545
+ this.subscription.add(this.actions$.pipe(store.ofActionDispatched(ConnectWebSocket)).subscribe((/**
546
+ * @param {?} __0
547
+ * @return {?}
548
+ */
549
+ function (_a) {
550
+ var payload = _a.payload;
551
+ _this.connect(payload);
552
+ })));
553
+ this.subscription.add(this.actions$.pipe(store.ofActionDispatched(DisconnectWebSocket)).subscribe((/**
554
+ * @return {?}
555
+ */
556
+ function () {
557
+ _this.disconnect();
558
+ })));
559
+ this.subscription.add(this.actions$.pipe(store.ofActionDispatched(SendWebSocketMessage)).subscribe((/**
560
+ * @param {?} __0
561
+ * @return {?}
562
+ */
563
+ function (_a) {
564
+ var payload = _a.payload;
565
+ _this.send(payload);
566
+ })));
567
+ };
568
+ /**
569
+ * @private
570
+ * @param {?=} options
571
+ * @return {?}
572
+ */
573
+ WebSocketHandler.prototype.connect = /**
574
+ * @private
575
+ * @param {?=} options
576
+ * @return {?}
577
+ */
578
+ function (options) {
579
+ var _this = this;
580
+ this.updateConnection();
581
+ // Users can pass the options in the connect method so
582
+ // if options aren't available at DI bootstrap they have access
583
+ // to pass them here
584
+ if (options) {
585
+ this.mergeConfigWithOptions(options);
586
+ }
587
+ this.socket = new webSocket.WebSocketSubject(this.config);
588
+ this.socket.subscribe({
589
+ next: (/**
590
+ * @param {?} message
591
+ * @return {?}
592
+ */
593
+ function (message) {
594
+ /** @type {?} */
595
+ var type = store.getValue(message, _this.typeKey);
596
+ if (!type) {
597
+ throw new TypeKeyPropertyMissingError(_this.typeKey);
598
+ }
599
+ _this.store.dispatch(__assign({}, message, { type: type }));
600
+ }),
601
+ error: (/**
602
+ * @param {?} error
603
+ * @return {?}
604
+ */
605
+ function (error) {
606
+ if (error instanceof CloseEvent) {
607
+ _this.dispatchWebSocketDisconnected();
608
+ }
609
+ else {
610
+ _this.store.dispatch(new WebsocketMessageError(error));
611
+ }
612
+ })
613
+ });
614
+ };
615
+ /**
616
+ * @private
617
+ * @return {?}
618
+ */
619
+ WebSocketHandler.prototype.disconnect = /**
620
+ * @private
621
+ * @return {?}
622
+ */
623
+ function () {
624
+ if (this.socket) {
625
+ this.closeConnection();
626
+ this.dispatchWebSocketDisconnected();
627
+ }
628
+ };
629
+ /**
630
+ * @private
631
+ * @param {?} data
632
+ * @return {?}
633
+ */
634
+ WebSocketHandler.prototype.send = /**
635
+ * @private
636
+ * @param {?} data
637
+ * @return {?}
638
+ */
639
+ function (data) {
640
+ if (!this.socket) {
641
+ throw new Error('You must connect to the socket before sending any data');
642
+ }
643
+ this.socket.next(data);
644
+ };
645
+ /**
646
+ * Don't enlarge the `connect` method
647
+ */
648
+ /**
649
+ * Don't enlarge the `connect` method
650
+ * @private
651
+ * @param {?} options
652
+ * @return {?}
653
+ */
654
+ WebSocketHandler.prototype.mergeConfigWithOptions = /**
655
+ * Don't enlarge the `connect` method
656
+ * @private
657
+ * @param {?} options
658
+ * @return {?}
659
+ */
660
+ function (options) {
661
+ if (options.url) {
662
+ this.config.url = options.url;
663
+ }
664
+ if (options.serializer) {
665
+ this.config.serializer = options.serializer;
666
+ }
667
+ if (options.deserializer) {
668
+ this.config.deserializer = options.deserializer;
669
+ }
670
+ };
671
+ /**
672
+ * To ensure we don't have any memory leaks
673
+ * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
674
+ * then the previous subscription will still live in the memory
675
+ * to prevent such behavior - we close the previous connection if it exists
676
+ */
677
+ /**
678
+ * To ensure we don't have any memory leaks
679
+ * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
680
+ * then the previous subscription will still live in the memory
681
+ * to prevent such behavior - we close the previous connection if it exists
682
+ * @private
683
+ * @return {?}
684
+ */
685
+ WebSocketHandler.prototype.updateConnection = /**
686
+ * To ensure we don't have any memory leaks
687
+ * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
688
+ * then the previous subscription will still live in the memory
689
+ * to prevent such behavior - we close the previous connection if it exists
690
+ * @private
691
+ * @return {?}
692
+ */
693
+ function () {
694
+ if (this.socket) {
695
+ this.closeConnection();
696
+ this.store.dispatch(new WebSocketConnectionUpdated());
697
+ }
698
+ };
699
+ /**
700
+ * Used in many places so it's better to move the code into function
701
+ */
702
+ /**
703
+ * Used in many places so it's better to move the code into function
704
+ * @private
705
+ * @return {?}
706
+ */
707
+ WebSocketHandler.prototype.dispatchWebSocketDisconnected = /**
708
+ * Used in many places so it's better to move the code into function
709
+ * @private
710
+ * @return {?}
711
+ */
712
+ function () {
713
+ this.store.dispatch(new WebSocketDisconnected());
714
+ };
715
+ /**
716
+ * @private
717
+ * @return {?}
718
+ */
719
+ WebSocketHandler.prototype.closeConnection = /**
720
+ * @private
721
+ * @return {?}
722
+ */
723
+ function () {
724
+ // `socket.complete()` closes the connection
725
+ // also it doesn't invoke the `onComplete` callback that we passed
726
+ // into `socket.subscribe(...)`
727
+ if (this.socket !== null) {
728
+ this.socket.complete();
729
+ this.socket = null;
730
+ }
731
+ };
732
+ WebSocketHandler.decorators = [
733
+ { type: core.Injectable }
734
+ ];
735
+ /** @nocollapse */
736
+ WebSocketHandler.ctorParameters = function () { return [
737
+ { type: store.Store },
738
+ { type: store.Actions },
739
+ { type: undefined, decorators: [{ type: core.Inject, args: [NGXS_WEBSOCKET_OPTIONS,] }] }
740
+ ]; };
741
+ return WebSocketHandler;
742
+ }());
743
+ if (false) {
744
+ /**
745
+ * @type {?}
746
+ * @private
747
+ */
748
+ WebSocketHandler.prototype.socket;
749
+ /**
750
+ * @type {?}
751
+ * @private
752
+ */
753
+ WebSocketHandler.prototype.config;
754
+ /**
755
+ * @type {?}
756
+ * @private
757
+ */
758
+ WebSocketHandler.prototype.typeKey;
759
+ /**
760
+ * @type {?}
761
+ * @private
762
+ */
763
+ WebSocketHandler.prototype.subscription;
764
+ /**
765
+ * @type {?}
766
+ * @private
767
+ */
768
+ WebSocketHandler.prototype.store;
769
+ /**
770
+ * @type {?}
771
+ * @private
772
+ */
773
+ WebSocketHandler.prototype.actions$;
774
+ /**
775
+ * @type {?}
776
+ * @private
777
+ */
778
+ WebSocketHandler.prototype.options;
751
779
  }
752
780
 
753
- /**
754
- * @fileoverview added by tsickle
755
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
756
- */
757
- /**
758
- * @param {?} options
759
- * @return {?}
760
- */
761
- function websocketOptionsFactory(options) {
762
- return __assign({ reconnectInterval: 5000, reconnectAttempts: 10, typeKey: 'type', deserializer: /**
763
- * @param {?} e
764
- * @return {?}
765
- */
766
- function (e) {
767
- return JSON.parse(e.data);
768
- },
769
- serializer: /**
770
- * @param {?} value
771
- * @return {?}
772
- */
773
- function (value) {
774
- return JSON.stringify(value);
775
- } }, options);
776
- }
777
- /** @type {?} */
778
- var USER_OPTIONS = new core.InjectionToken('USER_OPTIONS');
779
- var NgxsWebsocketPluginModule = /** @class */ (function () {
780
- function NgxsWebsocketPluginModule() {
781
- }
782
- /**
783
- * @param {?=} options
784
- * @return {?}
785
- */
786
- NgxsWebsocketPluginModule.forRoot = /**
787
- * @param {?=} options
788
- * @return {?}
789
- */
790
- function (options) {
791
- return {
792
- ngModule: NgxsWebsocketPluginModule,
793
- providers: [
794
- WebSocketHandler,
795
- {
796
- provide: USER_OPTIONS,
797
- useValue: options
798
- },
799
- {
800
- provide: NGXS_WEBSOCKET_OPTIONS,
801
- useFactory: websocketOptionsFactory,
802
- deps: [USER_OPTIONS]
803
- },
804
- {
805
- provide: core.APP_INITIALIZER,
806
- useFactory: noop,
807
- deps: [WebSocketHandler],
808
- multi: true
809
- }
810
- ]
811
- };
812
- };
813
- NgxsWebsocketPluginModule.decorators = [
814
- { type: core.NgModule }
815
- ];
816
- return NgxsWebsocketPluginModule;
781
+ /**
782
+ * @fileoverview added by tsickle
783
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
784
+ */
785
+ /**
786
+ * @param {?} options
787
+ * @return {?}
788
+ */
789
+ function websocketOptionsFactory(options) {
790
+ return __assign({ reconnectInterval: 5000, reconnectAttempts: 10, typeKey: 'type', deserializer: /**
791
+ * @param {?} e
792
+ * @return {?}
793
+ */
794
+ function (e) {
795
+ return JSON.parse(e.data);
796
+ },
797
+ serializer: /**
798
+ * @param {?} value
799
+ * @return {?}
800
+ */
801
+ function (value) {
802
+ return JSON.stringify(value);
803
+ } }, options);
804
+ }
805
+ /** @type {?} */
806
+ var USER_OPTIONS = new core.InjectionToken('USER_OPTIONS');
807
+ var NgxsWebsocketPluginModule = /** @class */ (function () {
808
+ function NgxsWebsocketPluginModule() {
809
+ }
810
+ /**
811
+ * @param {?=} options
812
+ * @return {?}
813
+ */
814
+ NgxsWebsocketPluginModule.forRoot = /**
815
+ * @param {?=} options
816
+ * @return {?}
817
+ */
818
+ function (options) {
819
+ return {
820
+ ngModule: NgxsWebsocketPluginModule,
821
+ providers: [
822
+ WebSocketHandler,
823
+ {
824
+ provide: USER_OPTIONS,
825
+ useValue: options
826
+ },
827
+ {
828
+ provide: NGXS_WEBSOCKET_OPTIONS,
829
+ useFactory: websocketOptionsFactory,
830
+ deps: [USER_OPTIONS]
831
+ },
832
+ {
833
+ provide: core.APP_INITIALIZER,
834
+ useFactory: noop,
835
+ deps: [WebSocketHandler],
836
+ multi: true
837
+ }
838
+ ]
839
+ };
840
+ };
841
+ NgxsWebsocketPluginModule.decorators = [
842
+ { type: core.NgModule }
843
+ ];
844
+ return NgxsWebsocketPluginModule;
817
845
  }());
818
846
 
819
847
  exports.ConnectWebSocket = ConnectWebSocket;