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