@ngxs/websocket-plugin 3.8.1-dev.master-07536db → 3.8.1-dev.master-4a74d16

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.
@@ -2,13 +2,12 @@ import * as i0 from '@angular/core';
2
2
  import { InjectionToken, Injectable, Inject, APP_INITIALIZER, NgModule, makeEnvironmentProviders } from '@angular/core';
3
3
  import * as i1 from '@ngxs/store';
4
4
  import { ofActionDispatched, getValue } from '@ngxs/store';
5
- import { Subscription } from 'rxjs';
6
- import { WebSocketSubject } from 'rxjs/webSocket';
5
+ import { Subject, ReplaySubject, fromEvent } from 'rxjs';
6
+ import { takeUntil } from 'rxjs/operators';
7
7
 
8
- const NGXS_WEBSOCKET_OPTIONS = new InjectionToken('NGXS_WEBSOCKET_OPTIONS');
9
- function noop(..._args) {
10
- return function () { };
11
- }
8
+ const NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode;
9
+ const NGXS_WEBSOCKET_OPTIONS = new InjectionToken(NG_DEV_MODE ? 'NGXS_WEBSOCKET_OPTIONS' : '');
10
+ const USER_OPTIONS = new InjectionToken(NG_DEV_MODE ? 'USER_OPTIONS' : '');
12
11
  /**
13
12
  * Action to connect to the websocket. Optionally pass a URL.
14
13
  */
@@ -71,143 +70,150 @@ class TypeKeyPropertyMissingError extends Error {
71
70
  }
72
71
 
73
72
  class WebSocketHandler {
74
- constructor(store, actions$, options) {
75
- this.store = store;
76
- this.actions$ = actions$;
77
- this.options = options;
78
- this.socket = null;
79
- this.config = {
80
- url: this.options.url,
81
- protocol: this.options.protocol,
82
- // Default binary type is `blob` for the global `WebSocket`
83
- binaryType: this.options.binaryType,
84
- serializer: this.options.serializer,
85
- deserializer: this.options.deserializer,
86
- closeObserver: {
87
- next: () => {
88
- // ATTENTION!
89
- // See https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/dom/WebSocketSubject.ts#L340
90
- // RxJS socket emits `onComplete` event only if `event.wasClean` is truthy
91
- // and doesn't complete socket subject if it's falsy
92
- this.disconnect();
93
- }
94
- },
95
- openObserver: {
96
- next: () => this.store.dispatch(new WebSocketConnected())
97
- }
98
- };
99
- this.typeKey = this.options.typeKey;
100
- this.subscription = new Subscription();
101
- this.setupActionsListeners();
73
+ constructor(_store, _ngZone, _actions$, _options) {
74
+ this._store = _store;
75
+ this._ngZone = _ngZone;
76
+ this._actions$ = _actions$;
77
+ this._options = _options;
78
+ this._socket = null;
79
+ this._socketClosed$ = new Subject();
80
+ this._typeKey = this._options.typeKey;
81
+ this._destroy$ = new ReplaySubject(1);
82
+ this._setupActionsListeners();
102
83
  }
103
84
  ngOnDestroy() {
104
- this.closeConnection();
105
- this.subscription.unsubscribe();
85
+ this._disconnect(/* forcelyCloseSocket */ true);
86
+ this._destroy$.next();
106
87
  }
107
- setupActionsListeners() {
108
- this.subscription.add(this.actions$.pipe(ofActionDispatched(ConnectWebSocket)).subscribe(({ payload }) => {
88
+ _setupActionsListeners() {
89
+ this._actions$
90
+ .pipe(ofActionDispatched(ConnectWebSocket), takeUntil(this._destroy$))
91
+ .subscribe(({ payload }) => {
109
92
  this.connect(payload);
110
- }));
111
- this.subscription.add(this.actions$.pipe(ofActionDispatched(DisconnectWebSocket)).subscribe(() => {
112
- this.disconnect();
113
- }));
114
- this.subscription.add(this.actions$.pipe(ofActionDispatched(SendWebSocketMessage)).subscribe(({ payload }) => {
93
+ });
94
+ this._actions$
95
+ .pipe(ofActionDispatched(DisconnectWebSocket), takeUntil(this._destroy$))
96
+ .subscribe(() => {
97
+ this._disconnect(/* forcelyCloseSocket */ true);
98
+ });
99
+ this._actions$
100
+ .pipe(ofActionDispatched(SendWebSocketMessage), takeUntil(this._destroy$))
101
+ .subscribe(({ payload }) => {
115
102
  this.send(payload);
116
- }));
103
+ });
117
104
  }
118
105
  connect(options) {
119
- this.updateConnection();
120
- // Users can pass the options in the connect method so
121
- // if options aren't available at DI bootstrap they have access
122
- // to pass them here
106
+ if (this._socket) {
107
+ this._closeConnection(/* forcelyCloseSocket */ true);
108
+ this._store.dispatch(new WebSocketConnectionUpdated());
109
+ }
110
+ // TODO(arturovt): we should not override default config values because this breaks support for having multiple socket connections.
123
111
  if (options) {
124
- this.mergeConfigWithOptions(options);
112
+ if (options.serializer) {
113
+ this._options.serializer = options.serializer;
114
+ }
115
+ if (options.deserializer) {
116
+ this._options.deserializer = options.deserializer;
117
+ }
125
118
  }
126
- this.socket = new WebSocketSubject(this.config);
127
- this.socket.subscribe({
128
- next: (message) => {
129
- const type = getValue(message, this.typeKey);
119
+ this._ngZone.runOutsideAngular(() => {
120
+ // We either use options provided in the `ConnectWebSocket` action
121
+ // or fallback to default config values.
122
+ const url = (options === null || options === void 0 ? void 0 : options.url) || this._options.url;
123
+ const protocol = (options === null || options === void 0 ? void 0 : options.protocol) || this._options.protocol;
124
+ const binaryType = (options === null || options === void 0 ? void 0 : options.binaryType) || this._options.binaryType;
125
+ const socket = (this._socket = protocol
126
+ ? new WebSocket(url, protocol)
127
+ : new WebSocket(url));
128
+ if (binaryType) {
129
+ socket.binaryType = binaryType;
130
+ }
131
+ fromEvent(socket, 'open')
132
+ .pipe(takeUntil(this._socketClosed$))
133
+ .subscribe(() => this._store.dispatch(new WebSocketConnected()));
134
+ fromEvent(socket, 'message')
135
+ .pipe(takeUntil(this._socketClosed$))
136
+ .subscribe(event => {
137
+ const message = this._options.deserializer(event);
138
+ const type = getValue(message, this._typeKey);
130
139
  if (!type) {
131
- throw new TypeKeyPropertyMissingError(this.typeKey);
140
+ throw new TypeKeyPropertyMissingError(this._typeKey);
132
141
  }
133
- this.store.dispatch(Object.assign(Object.assign({}, message), { type }));
134
- },
135
- error: (error) => {
136
- if (error instanceof CloseEvent) {
137
- this.dispatchWebSocketDisconnected();
142
+ this._store.dispatch(Object.assign(Object.assign({}, message), { type }));
143
+ });
144
+ fromEvent(socket, 'error')
145
+ .pipe(takeUntil(this._socketClosed$))
146
+ .subscribe(error => {
147
+ // The error event indicates that an error has occurred during the
148
+ // WebSocket communication, and it is often appropriate to close the
149
+ // WebSocket connection when such an error occurs.
150
+ // We need to call `_disconnect()` after the error event has been fired.
151
+ // This ensures that the WebSocket connection is properly closed to prevent
152
+ // potential resource leaks.
153
+ this._disconnect(/* forcelyCloseSocket */ true);
154
+ this._store.dispatch(new WebsocketMessageError(error));
155
+ });
156
+ fromEvent(socket, 'close')
157
+ .pipe(takeUntil(this._socketClosed$))
158
+ .subscribe(event => {
159
+ if (event.wasClean) {
160
+ // It is not necessary to call `socket.close()` after the `close` event
161
+ // has been fired. In fact, calling `socket.close()` within the `close`
162
+ // event handler or immediately after the event has been fired can lead
163
+ // to unexpected behavior.
164
+ this._disconnect(/* forcelyCloseSocket */ false);
138
165
  }
139
166
  else {
140
- this.store.dispatch(new WebsocketMessageError(error));
167
+ // If the WebSocket `close` event has been fired and its `wasClean`
168
+ // property is falsy, it indicates that the WebSocket connection was
169
+ // closed in an unexpected or abnormal manner.
170
+ // We should call `socket.close()` in this scenario, we can ensure that
171
+ // the WebSocket connection is properly closed.
172
+ this._disconnect(/* forcelyCloseSocket */ true);
173
+ this._store.dispatch(new WebsocketMessageError(event));
141
174
  }
142
- }
175
+ });
143
176
  });
144
177
  }
145
- disconnect() {
146
- if (this.socket) {
147
- this.closeConnection();
148
- this.dispatchWebSocketDisconnected();
178
+ _disconnect(forcelyCloseSocket) {
179
+ if (this._socket) {
180
+ this._closeConnection(forcelyCloseSocket);
181
+ this._store.dispatch(new WebSocketDisconnected());
149
182
  }
150
183
  }
151
184
  send(data) {
152
- if (!this.socket) {
185
+ if (!this._socket) {
153
186
  throw new Error('You must connect to the socket before sending any data');
154
187
  }
155
- this.socket.next(data);
156
- }
157
- /**
158
- * Don't enlarge the `connect` method
159
- */
160
- mergeConfigWithOptions(options) {
161
- if (options.url) {
162
- this.config.url = options.url;
163
- }
164
- if (options.serializer) {
165
- this.config.serializer = options.serializer;
166
- }
167
- if (options.deserializer) {
168
- this.config.deserializer = options.deserializer;
188
+ try {
189
+ this._socket.send(this._options.serializer(data));
169
190
  }
170
- }
171
- /**
172
- * To ensure we don't have any memory leaks
173
- * e.g. if the user occasionally dispatched `ConnectWebSocket` twice
174
- * then the previous subscription will still live in the memory
175
- * to prevent such behavior - we close the previous connection if it exists
176
- */
177
- updateConnection() {
178
- if (this.socket) {
179
- this.closeConnection();
180
- this.store.dispatch(new WebSocketConnectionUpdated());
191
+ catch (error) {
192
+ this._store.dispatch(new WebsocketMessageError(error));
181
193
  }
182
194
  }
183
- /**
184
- * Used in many places so it's better to move the code into function
185
- */
186
- dispatchWebSocketDisconnected() {
187
- this.store.dispatch(new WebSocketDisconnected());
188
- }
189
- closeConnection() {
190
- // `socket.complete()` closes the connection
191
- // also it doesn't invoke the `onComplete` callback that we passed
192
- // into `socket.subscribe(...)`
193
- if (this.socket !== null) {
194
- this.socket.complete();
195
- this.socket = null;
195
+ _closeConnection(forcelyCloseSocket) {
196
+ var _a;
197
+ if (forcelyCloseSocket) {
198
+ (_a = this._socket) === null || _a === void 0 ? void 0 : _a.close();
196
199
  }
200
+ this._socket = null;
201
+ this._socketClosed$.next();
197
202
  }
198
203
  }
199
- /** @nocollapse */ WebSocketHandler.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: WebSocketHandler, deps: [{ token: i1.Store }, { token: i1.Actions }, { token: NGXS_WEBSOCKET_OPTIONS }], target: i0.ɵɵFactoryTarget.Injectable });
200
- /** @nocollapse */ WebSocketHandler.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: WebSocketHandler });
204
+ /** @nocollapse */ WebSocketHandler.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: WebSocketHandler, deps: [{ token: i1.Store }, { token: i0.NgZone }, { token: i1.Actions }, { token: NGXS_WEBSOCKET_OPTIONS }], target: i0.ɵɵFactoryTarget.Injectable });
205
+ /** @nocollapse */ WebSocketHandler.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: WebSocketHandler, providedIn: 'root' });
201
206
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: WebSocketHandler, decorators: [{
202
- type: Injectable
207
+ type: Injectable,
208
+ args: [{ providedIn: 'root' }]
203
209
  }], ctorParameters: function () {
204
- return [{ type: i1.Store }, { type: i1.Actions }, { type: undefined, decorators: [{
210
+ return [{ type: i1.Store }, { type: i0.NgZone }, { type: i1.Actions }, { type: undefined, decorators: [{
205
211
  type: Inject,
206
212
  args: [NGXS_WEBSOCKET_OPTIONS]
207
213
  }] }];
208
214
  } });
209
215
 
210
- function websocketOptionsFactory(options) {
216
+ function ɵwebsocketOptionsFactory(options) {
211
217
  return Object.assign({ reconnectInterval: 5000, reconnectAttempts: 10, typeKey: 'type', deserializer(e) {
212
218
  return JSON.parse(e.data);
213
219
  },
@@ -215,29 +221,28 @@ function websocketOptionsFactory(options) {
215
221
  return JSON.stringify(value);
216
222
  } }, options);
217
223
  }
218
- const USER_OPTIONS = new InjectionToken('USER_OPTIONS');
224
+ function ɵgetProviders(options) {
225
+ return [
226
+ { provide: USER_OPTIONS, useValue: options },
227
+ {
228
+ provide: NGXS_WEBSOCKET_OPTIONS,
229
+ useFactory: ɵwebsocketOptionsFactory,
230
+ deps: [USER_OPTIONS]
231
+ },
232
+ {
233
+ provide: APP_INITIALIZER,
234
+ useFactory: () => () => { },
235
+ deps: [WebSocketHandler],
236
+ multi: true
237
+ }
238
+ ];
239
+ }
240
+
219
241
  class NgxsWebsocketPluginModule {
220
242
  static forRoot(options) {
221
243
  return {
222
244
  ngModule: NgxsWebsocketPluginModule,
223
- providers: [
224
- WebSocketHandler,
225
- {
226
- provide: USER_OPTIONS,
227
- useValue: options
228
- },
229
- {
230
- provide: NGXS_WEBSOCKET_OPTIONS,
231
- useFactory: websocketOptionsFactory,
232
- deps: [USER_OPTIONS]
233
- },
234
- {
235
- provide: APP_INITIALIZER,
236
- useFactory: noop,
237
- deps: [WebSocketHandler],
238
- multi: true
239
- }
240
- ]
245
+ providers: ɵgetProviders(options)
241
246
  };
242
247
  }
243
248
  }
@@ -248,20 +253,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.4", ngImpor
248
253
  type: NgModule
249
254
  }] });
250
255
  function withNgxsWebSocketPlugin(options) {
251
- return makeEnvironmentProviders([
252
- { provide: USER_OPTIONS, useValue: options },
253
- {
254
- provide: NGXS_WEBSOCKET_OPTIONS,
255
- useFactory: websocketOptionsFactory,
256
- deps: [USER_OPTIONS]
257
- },
258
- {
259
- provide: APP_INITIALIZER,
260
- useFactory: noop,
261
- deps: [WebSocketHandler],
262
- multi: true
263
- }
264
- ]);
256
+ return makeEnvironmentProviders(ɵgetProviders(options));
265
257
  }
266
258
 
267
259
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"ngxs-websocket-plugin.mjs","sources":["../../../packages/websocket-plugin/src/symbols.ts","../../../packages/websocket-plugin/src/websocket-handler.ts","../../../packages/websocket-plugin/src/websocket.module.ts","../../../packages/websocket-plugin/index.ts","../../../packages/websocket-plugin/ngxs-websocket-plugin.ts"],"sourcesContent":["import { InjectionToken } from '@angular/core';\n\nexport const NGXS_WEBSOCKET_OPTIONS = new InjectionToken('NGXS_WEBSOCKET_OPTIONS');\n\nexport interface NgxsWebsocketPluginOptions {\n /**\n * URL of the websocket.\n */\n url?: string;\n\n /**\n * Either a single protocol string or an array of protocol strings.\n * These strings are used to indicate sub-protocols, so that a single server\n * can implement multiple WebSocket sub-protocols (for example, you might want one server to be able\n * to handle different types of interactions depending on the specified protocol).\n * If you don't specify a protocol string, an empty string is assumed.\n */\n protocol?: string | string[];\n\n /**\n * Sets the `binaryType` property of the underlying WebSocket.\n */\n binaryType?: 'blob' | 'arraybuffer';\n\n /**\n * The property name to distigunish this type for the store.\n * Default: 'type'\n */\n typeKey?: string;\n\n /**\n * Interval to try and reconnect.\n * Default: 5000\n */\n reconnectInterval?: number;\n\n /**\n * Number of reconnect attemps.\n * Default: 10\n */\n reconnectAttempts?: number;\n\n /**\n * Serializer to call before sending messages\n * Default: `json.stringify`\n */\n serializer?: (data: any) => string;\n\n /**\n * Deseralizer before publishing the message.\n */\n deserializer?: (e: MessageEvent) => any;\n}\n\nexport function noop(..._args: any[]) {\n return function () {};\n}\n\n/**\n * Action to connect to the websocket. Optionally pass a URL.\n */\nexport class ConnectWebSocket {\n static readonly type = '[WebSocket] Connect';\n\n constructor(public payload?: NgxsWebsocketPluginOptions) {}\n}\n\n/**\n * Action triggered when a error ocurrs\n */\nexport class WebsocketMessageError {\n static readonly type = '[WebSocket] Message Error';\n\n constructor(public payload: any) {}\n}\n\n/**\n * Action to disconnect the websocket.\n */\nexport class DisconnectWebSocket {\n static readonly type = '[WebSocket] Disconnect';\n}\n\n/**\n * Action triggered when websocket is connected\n */\nexport class WebSocketConnected {\n static readonly type = '[WebSocket] Connected';\n}\n\n/**\n * Action triggered when websocket is disconnected\n */\nexport class WebSocketDisconnected {\n static readonly type = '[WebSocket] Disconnected';\n}\n\n/**\n * Action to send to the server.\n */\nexport class SendWebSocketMessage {\n static readonly type = '[WebSocket] Send Message';\n\n constructor(public payload: any) {}\n}\n\n/**\n * Action dispatched when the user tries to connect if the connection already exists.\n */\nexport class WebSocketConnectionUpdated {\n static readonly type = '[WebSocket] Connection Updated';\n}\n\n/**\n * This error is thrown where there is no `type` (or custom `typeKey`) property\n * on the message that came from the server side socket\n */\nexport class TypeKeyPropertyMissingError extends Error {\n constructor(typeKey: string) {\n super(`Property ${typeKey} is missing on the socket message`);\n }\n}\n","import { Injectable, Inject, OnDestroy } from '@angular/core';\nimport { Actions, Store, getValue, ofActionDispatched } from '@ngxs/store';\nimport { Subscription } from 'rxjs';\n\nimport { WebSocketSubject, WebSocketSubjectConfig } from 'rxjs/webSocket';\n\nimport {\n ConnectWebSocket,\n DisconnectWebSocket,\n SendWebSocketMessage,\n NGXS_WEBSOCKET_OPTIONS,\n NgxsWebsocketPluginOptions,\n WebsocketMessageError,\n WebSocketDisconnected,\n TypeKeyPropertyMissingError,\n WebSocketConnectionUpdated,\n WebSocketConnected\n} from './symbols';\n\n@Injectable()\nexport class WebSocketHandler implements OnDestroy {\n private socket: WebSocketSubject<any> | null = null;\n\n private config: WebSocketSubjectConfig<any> = {\n url: this.options.url!,\n protocol: this.options.protocol,\n // Default binary type is `blob` for the global `WebSocket`\n binaryType: this.options.binaryType,\n serializer: this.options.serializer,\n deserializer: this.options.deserializer,\n closeObserver: {\n next: () => {\n // ATTENTION!\n // See https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/dom/WebSocketSubject.ts#L340\n // RxJS socket emits `onComplete` event only if `event.wasClean` is truthy\n // and doesn't complete socket subject if it's falsy\n this.disconnect();\n }\n },\n openObserver: {\n next: () => this.store.dispatch(new WebSocketConnected())\n }\n };\n\n private typeKey = this.options.typeKey!;\n\n private subscription = new Subscription();\n\n constructor(\n private store: Store,\n private actions$: Actions,\n @Inject(NGXS_WEBSOCKET_OPTIONS) private options: NgxsWebsocketPluginOptions\n ) {\n this.setupActionsListeners();\n }\n\n ngOnDestroy(): void {\n this.closeConnection();\n this.subscription.unsubscribe();\n }\n\n private setupActionsListeners(): void {\n this.subscription.add(\n this.actions$.pipe(ofActionDispatched(ConnectWebSocket)).subscribe(({ payload }) => {\n this.connect(payload);\n })\n );\n\n this.subscription.add(\n this.actions$.pipe(ofActionDispatched(DisconnectWebSocket)).subscribe(() => {\n this.disconnect();\n })\n );\n\n this.subscription.add(\n this.actions$.pipe(ofActionDispatched(SendWebSocketMessage)).subscribe(({ payload }) => {\n this.send(payload);\n })\n );\n }\n\n private connect(options?: NgxsWebsocketPluginOptions): void {\n this.updateConnection();\n\n // Users can pass the options in the connect method so\n // if options aren't available at DI bootstrap they have access\n // to pass them here\n if (options) {\n this.mergeConfigWithOptions(options);\n }\n\n this.socket = new WebSocketSubject(this.config);\n\n this.socket.subscribe({\n next: (message: any) => {\n const type = getValue(message, this.typeKey);\n if (!type) {\n throw new TypeKeyPropertyMissingError(this.typeKey);\n }\n this.store.dispatch({ ...message, type });\n },\n error: (error: any) => {\n if (error instanceof CloseEvent) {\n this.dispatchWebSocketDisconnected();\n } else {\n this.store.dispatch(new WebsocketMessageError(error));\n }\n }\n });\n }\n\n private disconnect(): void {\n if (this.socket) {\n this.closeConnection();\n this.dispatchWebSocketDisconnected();\n }\n }\n\n private send(data: any): void {\n if (!this.socket) {\n throw new Error('You must connect to the socket before sending any data');\n }\n\n this.socket.next(data);\n }\n\n /**\n * Don't enlarge the `connect` method\n */\n private mergeConfigWithOptions(options: NgxsWebsocketPluginOptions): void {\n if (options.url) {\n this.config.url = options.url;\n }\n\n if (options.serializer) {\n this.config.serializer = options.serializer;\n }\n\n if (options.deserializer) {\n this.config.deserializer = options.deserializer;\n }\n }\n\n /**\n * To ensure we don't have any memory leaks\n * e.g. if the user occasionally dispatched `ConnectWebSocket` twice\n * then the previous subscription will still live in the memory\n * to prevent such behavior - we close the previous connection if it exists\n */\n private updateConnection(): void {\n if (this.socket) {\n this.closeConnection();\n this.store.dispatch(new WebSocketConnectionUpdated());\n }\n }\n\n /**\n * Used in many places so it's better to move the code into function\n */\n private dispatchWebSocketDisconnected(): void {\n this.store.dispatch(new WebSocketDisconnected());\n }\n\n private closeConnection(): void {\n // `socket.complete()` closes the connection\n // also it doesn't invoke the `onComplete` callback that we passed\n // into `socket.subscribe(...)`\n if (this.socket !== null) {\n this.socket.complete();\n this.socket = null;\n }\n }\n}\n","import {\n NgModule,\n ModuleWithProviders,\n APP_INITIALIZER,\n InjectionToken,\n EnvironmentProviders,\n makeEnvironmentProviders\n} from '@angular/core';\n\nimport { WebSocketHandler } from './websocket-handler';\nimport { NgxsWebsocketPluginOptions, NGXS_WEBSOCKET_OPTIONS, noop } from './symbols';\n\nexport function websocketOptionsFactory(options: NgxsWebsocketPluginOptions) {\n return {\n reconnectInterval: 5000,\n reconnectAttempts: 10,\n typeKey: 'type',\n deserializer(e: MessageEvent) {\n return JSON.parse(e.data);\n },\n serializer(value: any) {\n return JSON.stringify(value);\n },\n ...options\n };\n}\n\nexport const USER_OPTIONS = new InjectionToken('USER_OPTIONS');\n\n@NgModule()\nexport class NgxsWebsocketPluginModule {\n static forRoot(\n options?: NgxsWebsocketPluginOptions\n ): ModuleWithProviders<NgxsWebsocketPluginModule> {\n return {\n ngModule: NgxsWebsocketPluginModule,\n providers: [\n WebSocketHandler,\n {\n provide: USER_OPTIONS,\n useValue: options\n },\n {\n provide: NGXS_WEBSOCKET_OPTIONS,\n useFactory: websocketOptionsFactory,\n deps: [USER_OPTIONS]\n },\n {\n provide: APP_INITIALIZER,\n useFactory: noop,\n deps: [WebSocketHandler],\n multi: true\n }\n ]\n };\n }\n}\n\nexport function withNgxsWebSocketPlugin(\n options?: NgxsWebsocketPluginOptions\n): EnvironmentProviders {\n return makeEnvironmentProviders([\n { provide: USER_OPTIONS, useValue: options },\n {\n provide: NGXS_WEBSOCKET_OPTIONS,\n useFactory: websocketOptionsFactory,\n deps: [USER_OPTIONS]\n },\n {\n provide: APP_INITIALIZER,\n useFactory: noop,\n deps: [WebSocketHandler],\n multi: true\n }\n ]);\n}\n","/**\n * The public api for consumers of @ngxs/websocket-plugin\n */\nexport * from './src/public_api';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;MAEa,sBAAsB,GAAG,IAAI,cAAc,CAAC,wBAAwB,EAAE;AAoDnE,SAAA,IAAI,CAAC,GAAG,KAAY,EAAA;IAClC,OAAO,YAAA,GAAc,CAAC;AACxB,CAAC;AAED;;AAEG;MACU,gBAAgB,CAAA;AAG3B,IAAA,WAAA,CAAmB,OAAoC,EAAA;AAApC,QAAA,IAAO,CAAA,OAAA,GAAP,OAAO,CAA6B;KAAI;;AAF3C,gBAAI,CAAA,IAAA,GAAG,qBAAqB,CAAC;AAK/C;;AAEG;MACU,qBAAq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AAI;oBAChB,IAAI,EAAE,CAAC,gBAAgB,CAAC;AACxB,oBAAA,KAAK,EAAE,IAAI;AACZ,iBAAA;AACF,aAAA;SACF,CAAC;KACH;;yIAzBU,yBAAyB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,QAAA,EAAA,CAAA,CAAA;0IAAzB,yBAAyB,EAAA,CAAA,CAAA;0IAAzB,yBAAyB,EAAA,CAAA,CAAA;2FAAzB,yBAAyB,EAAA,UAAA,EAAA,CAAA;kBADrC,QAAQ;;AA6BH,SAAU,uBAAuB,CACrC,OAAoC,EAAA;AAEpC,IAAA,OAAO,wBAAwB,CAAC;AAC9B,QAAA,EAAE,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,OAAO,EAAE;AAC5C,QAAA;AACE,YAAA,OAAO,EAAE,sBAAsB;AAC/B,YAAA,UAAU,EAAE,uBAAuB;YACnC,IAAI,EAAE,CAAC,YAAY,CAAC;AACrB,SAAA;AACD,QAAA;AACE,YAAA,OAAO,EAAE,eAAe;AACxB,YAAA,UAAU,EAAE,IAAI;YAChB,IAAI,EAAE,CAAC,gBAAgB,CAAC;AACxB,YAAA,KAAK,EAAE,IAAI;AACZ,SAAA;AACF,KAAA,CAAC,CAAC;AACL;;AC3EA;;AAEG;;ACFH;;AAEG;;;;"}
1
+ {"version":3,"file":"ngxs-websocket-plugin.mjs","sources":["../../../packages/websocket-plugin/src/symbols.ts","../../../packages/websocket-plugin/src/websocket-handler.ts","../../../packages/websocket-plugin/src/providers.ts","../../../packages/websocket-plugin/src/websocket.module.ts","../../../packages/websocket-plugin/index.ts","../../../packages/websocket-plugin/ngxs-websocket-plugin.ts"],"sourcesContent":["import { InjectionToken } from '@angular/core';\n\ndeclare const ngDevMode: boolean;\n\nconst NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode;\n\nexport const NGXS_WEBSOCKET_OPTIONS = new InjectionToken<NgxsWebsocketPluginOptions>(\n NG_DEV_MODE ? 'NGXS_WEBSOCKET_OPTIONS' : ''\n);\n\nexport const USER_OPTIONS = new InjectionToken(NG_DEV_MODE ? 'USER_OPTIONS' : '');\n\nexport interface NgxsWebsocketPluginOptions {\n /**\n * URL of the websocket.\n */\n url?: string;\n\n /**\n * Either a single protocol string or an array of protocol strings.\n * These strings are used to indicate sub-protocols, so that a single server\n * can implement multiple WebSocket sub-protocols (for example, you might want one server to be able\n * to handle different types of interactions depending on the specified protocol).\n * If you don't specify a protocol string, an empty string is assumed.\n */\n protocol?: string | string[];\n\n /**\n * Sets the `binaryType` property of the underlying WebSocket.\n */\n binaryType?: 'blob' | 'arraybuffer';\n\n /**\n * The property name to distigunish this type for the store.\n * Default: 'type'\n */\n typeKey?: string;\n\n /**\n * Interval to try and reconnect.\n * Default: 5000\n */\n reconnectInterval?: number;\n\n /**\n * Number of reconnect attemps.\n * Default: 10\n */\n reconnectAttempts?: number;\n\n /**\n * Serializer to call before sending messages\n * Default: `json.stringify`\n */\n serializer?: (data: any) => string;\n\n /**\n * Deseralizer before publishing the message.\n */\n deserializer?: (e: MessageEvent) => any;\n}\n\n/**\n * Action to connect to the websocket. Optionally pass a URL.\n */\nexport class ConnectWebSocket {\n static readonly type = '[WebSocket] Connect';\n\n constructor(public payload?: NgxsWebsocketPluginOptions) {}\n}\n\n/**\n * Action triggered when a error ocurrs\n */\nexport class WebsocketMessageError {\n static readonly type = '[WebSocket] Message Error';\n\n constructor(public payload: any) {}\n}\n\n/**\n * Action to disconnect the websocket.\n */\nexport class DisconnectWebSocket {\n static readonly type = '[WebSocket] Disconnect';\n}\n\n/**\n * Action triggered when websocket is connected\n */\nexport class WebSocketConnected {\n static readonly type = '[WebSocket] Connected';\n}\n\n/**\n * Action triggered when websocket is disconnected\n */\nexport class WebSocketDisconnected {\n static readonly type = '[WebSocket] Disconnected';\n}\n\n/**\n * Action to send to the server.\n */\nexport class SendWebSocketMessage {\n static readonly type = '[WebSocket] Send Message';\n\n constructor(public payload: any) {}\n}\n\n/**\n * Action dispatched when the user tries to connect if the connection already exists.\n */\nexport class WebSocketConnectionUpdated {\n static readonly type = '[WebSocket] Connection Updated';\n}\n\n/**\n * This error is thrown where there is no `type` (or custom `typeKey`) property\n * on the message that came from the server side socket\n */\nexport class TypeKeyPropertyMissingError extends Error {\n constructor(typeKey: string) {\n super(`Property ${typeKey} is missing on the socket message`);\n }\n}\n","import { Injectable, Inject, OnDestroy, NgZone } from '@angular/core';\nimport { Actions, Store, getValue, ofActionDispatched } from '@ngxs/store';\nimport { ReplaySubject, Subject, fromEvent } from 'rxjs';\nimport { takeUntil } from 'rxjs/operators';\n\nimport {\n ConnectWebSocket,\n DisconnectWebSocket,\n SendWebSocketMessage,\n NGXS_WEBSOCKET_OPTIONS,\n NgxsWebsocketPluginOptions,\n WebsocketMessageError,\n WebSocketDisconnected,\n TypeKeyPropertyMissingError,\n WebSocketConnectionUpdated,\n WebSocketConnected\n} from './symbols';\n\n@Injectable({ providedIn: 'root' })\nexport class WebSocketHandler implements OnDestroy {\n private _socket: WebSocket | null = null;\n\n private readonly _socketClosed$ = new Subject<void>();\n\n private readonly _typeKey = this._options.typeKey!;\n\n private readonly _destroy$ = new ReplaySubject<void>(1);\n\n constructor(\n private _store: Store,\n private _ngZone: NgZone,\n private _actions$: Actions,\n @Inject(NGXS_WEBSOCKET_OPTIONS) private _options: NgxsWebsocketPluginOptions\n ) {\n this._setupActionsListeners();\n }\n\n ngOnDestroy(): void {\n this._disconnect(/* forcelyCloseSocket */ true);\n this._destroy$.next();\n }\n\n private _setupActionsListeners(): void {\n this._actions$\n .pipe(ofActionDispatched(ConnectWebSocket), takeUntil(this._destroy$))\n .subscribe(({ payload }) => {\n this.connect(payload);\n });\n\n this._actions$\n .pipe(ofActionDispatched(DisconnectWebSocket), takeUntil(this._destroy$))\n .subscribe(() => {\n this._disconnect(/* forcelyCloseSocket */ true);\n });\n\n this._actions$\n .pipe(ofActionDispatched(SendWebSocketMessage), takeUntil(this._destroy$))\n .subscribe(({ payload }) => {\n this.send(payload);\n });\n }\n\n private connect(options?: NgxsWebsocketPluginOptions): void {\n if (this._socket) {\n this._closeConnection(/* forcelyCloseSocket */ true);\n this._store.dispatch(new WebSocketConnectionUpdated());\n }\n\n // TODO(arturovt): we should not override default config values because this breaks support for having multiple socket connections.\n if (options) {\n if (options.serializer) {\n this._options.serializer = options.serializer;\n }\n\n if (options.deserializer) {\n this._options.deserializer = options.deserializer;\n }\n }\n\n this._ngZone.runOutsideAngular(() => {\n // We either use options provided in the `ConnectWebSocket` action\n // or fallback to default config values.\n const url = options?.url || this._options.url!;\n const protocol = options?.protocol || this._options.protocol;\n const binaryType = options?.binaryType || this._options.binaryType;\n\n const socket = (this._socket = protocol\n ? new WebSocket(url, protocol)\n : new WebSocket(url));\n\n if (binaryType) {\n socket.binaryType = binaryType;\n }\n\n fromEvent(socket, 'open')\n .pipe(takeUntil(this._socketClosed$))\n .subscribe(() => this._store.dispatch(new WebSocketConnected()));\n\n fromEvent<MessageEvent>(socket, 'message')\n .pipe(takeUntil(this._socketClosed$))\n .subscribe(event => {\n const message = this._options.deserializer!(event);\n const type = getValue(message, this._typeKey);\n if (!type) {\n throw new TypeKeyPropertyMissingError(this._typeKey);\n }\n this._store.dispatch({ ...message, type });\n });\n\n fromEvent(socket, 'error')\n .pipe(takeUntil(this._socketClosed$))\n .subscribe(error => {\n // The error event indicates that an error has occurred during the\n // WebSocket communication, and it is often appropriate to close the\n // WebSocket connection when such an error occurs.\n // We need to call `_disconnect()` after the error event has been fired.\n // This ensures that the WebSocket connection is properly closed to prevent\n // potential resource leaks.\n this._disconnect(/* forcelyCloseSocket */ true);\n this._store.dispatch(new WebsocketMessageError(error));\n });\n\n fromEvent<CloseEvent>(socket, 'close')\n .pipe(takeUntil(this._socketClosed$))\n .subscribe(event => {\n if (event.wasClean) {\n // It is not necessary to call `socket.close()` after the `close` event\n // has been fired. In fact, calling `socket.close()` within the `close`\n // event handler or immediately after the event has been fired can lead\n // to unexpected behavior.\n this._disconnect(/* forcelyCloseSocket */ false);\n } else {\n // If the WebSocket `close` event has been fired and its `wasClean`\n // property is falsy, it indicates that the WebSocket connection was\n // closed in an unexpected or abnormal manner.\n // We should call `socket.close()` in this scenario, we can ensure that\n // the WebSocket connection is properly closed.\n this._disconnect(/* forcelyCloseSocket */ true);\n this._store.dispatch(new WebsocketMessageError(event));\n }\n });\n });\n }\n\n private _disconnect(forcelyCloseSocket: boolean): void {\n if (this._socket) {\n this._closeConnection(forcelyCloseSocket);\n this._store.dispatch(new WebSocketDisconnected());\n }\n }\n\n private send(data: any): void {\n if (!this._socket) {\n throw new Error('You must connect to the socket before sending any data');\n }\n\n try {\n this._socket.send(this._options.serializer!(data));\n } catch (error) {\n this._store.dispatch(new WebsocketMessageError(error));\n }\n }\n\n private _closeConnection(forcelyCloseSocket: boolean): void {\n if (forcelyCloseSocket) {\n this._socket?.close();\n }\n this._socket = null;\n this._socketClosed$.next();\n }\n}\n","import { APP_INITIALIZER } from '@angular/core';\n\nimport { WebSocketHandler } from './websocket-handler';\nimport { USER_OPTIONS, NGXS_WEBSOCKET_OPTIONS, NgxsWebsocketPluginOptions } from './symbols';\n\nexport function ɵwebsocketOptionsFactory(options: NgxsWebsocketPluginOptions) {\n return {\n reconnectInterval: 5000,\n reconnectAttempts: 10,\n typeKey: 'type',\n deserializer(e: MessageEvent) {\n return JSON.parse(e.data);\n },\n serializer(value: any) {\n return JSON.stringify(value);\n },\n ...options\n };\n}\n\nexport function ɵgetProviders(options?: NgxsWebsocketPluginOptions) {\n return [\n { provide: USER_OPTIONS, useValue: options },\n {\n provide: NGXS_WEBSOCKET_OPTIONS,\n useFactory: ɵwebsocketOptionsFactory,\n deps: [USER_OPTIONS]\n },\n {\n provide: APP_INITIALIZER,\n useFactory: () => () => {},\n deps: [WebSocketHandler],\n multi: true\n }\n ];\n}\n","import {\n NgModule,\n ModuleWithProviders,\n EnvironmentProviders,\n makeEnvironmentProviders\n} from '@angular/core';\n\nimport { ɵgetProviders } from './providers';\nimport { NgxsWebsocketPluginOptions } from './symbols';\n\n@NgModule()\nexport class NgxsWebsocketPluginModule {\n static forRoot(\n options?: NgxsWebsocketPluginOptions\n ): ModuleWithProviders<NgxsWebsocketPluginModule> {\n return {\n ngModule: NgxsWebsocketPluginModule,\n providers: ɵgetProviders(options)\n };\n }\n}\n\nexport function withNgxsWebSocketPlugin(\n options?: NgxsWebsocketPluginOptions\n): EnvironmentProviders {\n return makeEnvironmentProviders(ɵgetProviders(options));\n}\n","/**\n * The public api for consumers of @ngxs/websocket-plugin\n */\nexport * from './src/public_api';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;AAIA,MAAM,WAAW,GAAG,OAAO,SAAS,KAAK,WAAW,IAAI,SAAS,CAAC;AAErD,MAAA,sBAAsB,GAAG,IAAI,cAAc,CACtD,WAAW,GAAG,wBAAwB,GAAG,EAAE,EAC3C;AAEK,MAAM,YAAY,GAAG,IAAI,cAAc,CAAC,WAAW,GAAG,cAAc,GAAG,EAAE,CAAC,CAAC;AAoDlF;;AAEG;MACU,gBAAgB,CAAA;AAG3B,IAAA,WAAA,CAAmB,OAAoC,EAAA;AAApC,QAAA,IAAO,CAAA,OAAA,GAAP,OAAO,CAA6B;KAAI;;AAF3C,gBAAI,CAAA,IAAA,GAAG,qBAAqB,CAAC;AAK/C;;AAEG;MACU,qBAAqB,CAAA;AAGhC,IAAA,WAAA,CAAmB,OAAY,EAAA;AAAZ,QAAA,IAAO,CAAA,OAAA,GAAP,OAAO,CAAK;KAAI;;AAFnB,qBAAI,CAAA,IAAA,GAAG,2BAA2B,CAAC;AAKrD;;AAEG;MACU,mBAAmB,CAAA;;AACd,mBAAI,CAAA,IAAA,GAAG,wBAAwB,CAAC;AAGlD;;AAEG;MACU,kBAAkB,CAAA;;AACb,kBAAI,CAAA,IAAA,GAAG,uBAAuB,CAAC;AAGjD;;AAEG;MACU,qBAAqB,CAAA;;AAChB,qBAAI,CAAA,IAAA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