@valtimo/sse 10.5.0

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.
@@ -0,0 +1,385 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable, NgModule } from '@angular/core';
3
+ import { Subject, filter, Observable } from 'rxjs';
4
+ import * as i1 from '@valtimo/config';
5
+ import * as i2 from 'ngx-logger';
6
+
7
+ /*
8
+ * Copyright 2015-2020 Ritense BV, the Netherlands.
9
+ *
10
+ * Licensed under EUPL, Version 1.2 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" basis,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ class SseSubscriptionBucket {
23
+ constructor() {
24
+ this.listeners = [];
25
+ }
26
+ on(listener) {
27
+ this.listeners.push(listener);
28
+ }
29
+ off(listener) {
30
+ const index = this.listeners.indexOf(listener);
31
+ if (index > -1) {
32
+ this.listeners.splice(index, 1);
33
+ }
34
+ }
35
+ offAll() {
36
+ this.listeners.length = 0;
37
+ }
38
+ sendEvent(event) {
39
+ this.listeners.forEach(listener => {
40
+ listener(event);
41
+ });
42
+ }
43
+ }
44
+ // error bucket for error listeners
45
+ class SseErrorBucket extends SseSubscriptionBucket {
46
+ }
47
+ // implicit type bucket with event for filtering
48
+ class SseEventSubscriptionBucket extends SseSubscriptionBucket {
49
+ constructor(event) {
50
+ super();
51
+ this.event = event;
52
+ }
53
+ }
54
+
55
+ /*
56
+ * Copyright 2015-2020 Ritense BV, the Netherlands.
57
+ *
58
+ * Licensed under EUPL, Version 1.2 (the "License");
59
+ * you may not use this file except in compliance with the License.
60
+ * You may obtain a copy of the License at
61
+ *
62
+ * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
63
+ *
64
+ * Unless required by applicable law or agreed to in writing, software
65
+ * distributed under the License is distributed on an "AS IS" basis,
66
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
67
+ * See the License for the specific language governing permissions and
68
+ * limitations under the License.
69
+ */
70
+
71
+ /*
72
+ * Copyright 2015-2020 Ritense BV, the Netherlands.
73
+ *
74
+ * Licensed under EUPL, Version 1.2 (the "License");
75
+ * you may not use this file except in compliance with the License.
76
+ * You may obtain a copy of the License at
77
+ *
78
+ * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
79
+ *
80
+ * Unless required by applicable law or agreed to in writing, software
81
+ * distributed under the License is distributed on an "AS IS" basis,
82
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
83
+ * See the License for the specific language governing permissions and
84
+ * limitations under the License.
85
+ */
86
+
87
+ /*
88
+ * Copyright 2015-2020 Ritense BV, the Netherlands.
89
+ *
90
+ * Licensed under EUPL, Version 1.2 (the "License");
91
+ * you may not use this file except in compliance with the License.
92
+ * You may obtain a copy of the License at
93
+ *
94
+ * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
95
+ *
96
+ * Unless required by applicable law or agreed to in writing, software
97
+ * distributed under the License is distributed on an "AS IS" basis,
98
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
99
+ * See the License for the specific language governing permissions and
100
+ * limitations under the License.
101
+ */
102
+ /**
103
+ * Server-side events service, for connecting and reconnecting to SSE,
104
+ * and a translation layer between SSE json string data and TS typed events.
105
+ *
106
+ * The service is always present, and so events can be registered with {@link onMessage} and {@link onEvent},
107
+ * and will not be lost on disconnect or reconnect of the SSE connection itself.
108
+ * It is expected to also unregister events using the off-method variants of the on-method listeners.
109
+ *
110
+ * To ensure messages are received the connection must be explicitly opened with {@link connect},
111
+ * likewise the connection should also be closed with {@link disconnect} to stop receiving messages.
112
+ *
113
+ * This service can reconnect itself with the backend, and might use the {@link subscriptionId} to ask for its
114
+ * previous connection state. This state currently exists in the backend as the "Subscriber" class, and might
115
+ * have a state containing queues of items that need to be sent, so you can reconnect and receive the rest.
116
+ *
117
+ * At this time no state data is kept, so reconnection without a subscriptionId does not give a different result
118
+ */
119
+ class SseService {
120
+ constructor(configService, logger) {
121
+ this.configService = configService;
122
+ this.logger = logger;
123
+ this.connectionCount = 0; // amount of times we have connected sequentially, no concurrent connections
124
+ this.establishedDataHandler = null;
125
+ this.establishedConnection = null;
126
+ this.establishedConnectionObservable = null;
127
+ this.subscriptionId = null;
128
+ this.sequentialConnectionAttemptFailCount = 0;
129
+ this.errorBucket = new SseErrorBucket();
130
+ this.anySubscribersBucket = new SseSubscriptionBucket();
131
+ this.eventSubscribersBuckets = [];
132
+ this.state = SseService.NOT_CONNECTED;
133
+ this._sseMessages$ = new Subject();
134
+ this.VALTIMO_ENDPOINT_URL = configService.config.valtimoApi.endpointUri;
135
+ this.connect();
136
+ }
137
+ get sseMessages$() {
138
+ return this._sseMessages$.asObservable().pipe(filter(message => !!message));
139
+ }
140
+ getSseMessagesObservableByEventType(eventTypes) {
141
+ return this._sseMessages$.asObservable().pipe(filter(message => !!message), filter(message => eventTypes.includes(message?.data?.eventType)));
142
+ }
143
+ /**
144
+ * Start receiving SSE events
145
+ */
146
+ connect() {
147
+ this.ensureConnection();
148
+ return this;
149
+ }
150
+ onMessage(listener) {
151
+ this.ensureConnection(); // ensure connection
152
+ this.anySubscribersBucket.on(listener);
153
+ return this;
154
+ }
155
+ onEvent(event, listener) {
156
+ this.ensureConnection(); // ensure connection
157
+ let found = false;
158
+ this.eventSubscribersBuckets.forEach(bucket => {
159
+ if (bucket.event === event) {
160
+ bucket.on(listener);
161
+ found = true;
162
+ }
163
+ });
164
+ if (!found) {
165
+ const bucket = new SseEventSubscriptionBucket(event);
166
+ bucket.on(listener);
167
+ this.eventSubscribersBuckets.push(bucket);
168
+ }
169
+ return this;
170
+ }
171
+ offMessage(listener) {
172
+ this.anySubscribersBucket.off(listener);
173
+ }
174
+ offEvent(event, listener) {
175
+ this.eventSubscribersBuckets.forEach(bucket => {
176
+ if (bucket.event === event) {
177
+ bucket.off(listener);
178
+ }
179
+ });
180
+ }
181
+ offEvents(type) {
182
+ this.eventSubscribersBuckets.forEach(bucket => {
183
+ if (type === null || type === bucket.event) {
184
+ bucket.offAll();
185
+ }
186
+ });
187
+ }
188
+ offMessages() {
189
+ this.anySubscribersBucket.offAll();
190
+ }
191
+ offAll() {
192
+ this.offEvents();
193
+ this.offMessages();
194
+ }
195
+ disconnect(keepSubscriptionId = false) {
196
+ this.disconnectWith(SseService.NOT_CONNECTED, keepSubscriptionId);
197
+ }
198
+ disconnectWith(state, keepSubscriptionId = false) {
199
+ this.state = state;
200
+ if (this.establishedConnection?.readyState !== EventSource.CLOSED) {
201
+ this.establishedConnection?.close();
202
+ }
203
+ this.establishedDataHandler.unsubscribe();
204
+ this.establishedConnection = null;
205
+ this.establishedConnectionObservable = null;
206
+ this.establishedDataHandler = null;
207
+ if (!keepSubscriptionId) {
208
+ this.subscriptionId = null;
209
+ }
210
+ }
211
+ ensureConnection(retry = false) {
212
+ if (this.establishedConnection !== null && this.establishedConnectionObservable !== null) {
213
+ if (this.establishedConnection.readyState !== EventSource.CLOSED) {
214
+ return; // found
215
+ }
216
+ }
217
+ if (this.state === SseService.CONNECTING || this.state === SseService.RECONNECTING) {
218
+ return; // already connecting
219
+ }
220
+ this.establishedConnection = null;
221
+ this.establishedConnectionObservable = null;
222
+ this.constructNewSse(retry); // create new
223
+ }
224
+ constructNewSse(retry) {
225
+ this.logger.debug('subscribing to sse events');
226
+ if (this.connectionCount > 0) {
227
+ this.state = SseService.RECONNECTING;
228
+ }
229
+ else {
230
+ this.state = SseService.CONNECTING;
231
+ }
232
+ const observable = new Observable(observer => {
233
+ const eventSource = this.getEventSource();
234
+ eventSource.onopen = () => {
235
+ this.establishedConnection = eventSource;
236
+ this.logger.debug('connected to sse');
237
+ this.connectionCount++;
238
+ this.sequentialConnectionAttemptFailCount = 0; // reset retry count
239
+ this.state = SseService.CONNECTED;
240
+ };
241
+ eventSource.onmessage = event => {
242
+ observer.next({
243
+ ...event,
244
+ data: JSON.parse(event.data), // parse JSON string to JSON object
245
+ });
246
+ };
247
+ eventSource.onerror = () => {
248
+ eventSource.close();
249
+ observer.complete();
250
+ if (retry) {
251
+ this.sequentialConnectionAttemptFailCount++;
252
+ this.logger.debug(`retry failed: ${this.sequentialConnectionAttemptFailCount}`);
253
+ if (this.sequentialConnectionAttemptFailCount > 3) {
254
+ this.disconnectWith(SseService.CONNECTION_RETRIES_EXCEEDED, false);
255
+ this.err(`Failed to connect to SSE after ${this.sequentialConnectionAttemptFailCount} retries`);
256
+ return;
257
+ }
258
+ }
259
+ this.disconnect(true);
260
+ this.ensureConnection(true);
261
+ };
262
+ return () => eventSource.close();
263
+ });
264
+ this.establishedConnectionObservable = observable;
265
+ this.registerSseEventHandling(observable);
266
+ return observable;
267
+ }
268
+ getEventSource() {
269
+ let suffix = '';
270
+ if (this.subscriptionId != null) {
271
+ suffix = '/' + this.subscriptionId;
272
+ }
273
+ // subscribe to /sse or /sse/<subscriptionId>
274
+ return new EventSource(this.VALTIMO_ENDPOINT_URL + 'v1/sse' + suffix);
275
+ }
276
+ registerSseEventHandling(observable) {
277
+ this.establishedDataHandler = observable.subscribe(event => {
278
+ this._sseMessages$.next(event);
279
+ this.internalListenerEstablishConnection(event);
280
+ // notify all generic listeners
281
+ this.anySubscribersBucket.sendEvent(event.data);
282
+ // notify the specific event listener bucket
283
+ this.eventSubscribersBuckets.forEach(bucket => {
284
+ if (bucket.event === event?.data?.eventType) {
285
+ bucket.sendEvent(event.data);
286
+ }
287
+ });
288
+ });
289
+ }
290
+ internalListenerEstablishConnection(event) {
291
+ if (event?.data?.eventType !== 'ESTABLISHED_CONNECTION') {
292
+ return;
293
+ }
294
+ this.logger.debug(`established connection: ${event}`);
295
+ this.subscriptionId = event.data.subscriptionId;
296
+ }
297
+ err(message, ...data) {
298
+ this.errorBucket.sendEvent({
299
+ state: this.state,
300
+ message,
301
+ data,
302
+ });
303
+ }
304
+ }
305
+ SseService.CONNECTION_RETRIES_EXCEEDED = -1;
306
+ SseService.NOT_CONNECTED = 0;
307
+ SseService.CONNECTING = 1;
308
+ SseService.RECONNECTING = 2;
309
+ SseService.CONNECTED = 10;
310
+ SseService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: SseService, deps: [{ token: i1.ConfigService }, { token: i2.NGXLogger }], target: i0.ɵɵFactoryTarget.Injectable });
311
+ SseService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: SseService, providedIn: 'root' });
312
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: SseService, decorators: [{
313
+ type: Injectable,
314
+ args: [{
315
+ providedIn: 'root',
316
+ }]
317
+ }], ctorParameters: function () { return [{ type: i1.ConfigService }, { type: i2.NGXLogger }]; } });
318
+
319
+ /*
320
+ * Copyright 2015-2020 Ritense BV, the Netherlands.
321
+ *
322
+ * Licensed under EUPL, Version 1.2 (the "License");
323
+ * you may not use this file except in compliance with the License.
324
+ * You may obtain a copy of the License at
325
+ *
326
+ * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
327
+ *
328
+ * Unless required by applicable law or agreed to in writing, software
329
+ * distributed under the License is distributed on an "AS IS" basis,
330
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
331
+ * See the License for the specific language governing permissions and
332
+ * limitations under the License.
333
+ */
334
+
335
+ /*
336
+ * Copyright 2015-2020 Ritense BV, the Netherlands.
337
+ *
338
+ * Licensed under EUPL, Version 1.2 (the "License");
339
+ * you may not use this file except in compliance with the License.
340
+ * You may obtain a copy of the License at
341
+ *
342
+ * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
343
+ *
344
+ * Unless required by applicable law or agreed to in writing, software
345
+ * distributed under the License is distributed on an "AS IS" basis,
346
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
347
+ * See the License for the specific language governing permissions and
348
+ * limitations under the License.
349
+ */
350
+ class SseModule {
351
+ }
352
+ SseModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: SseModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
353
+ SseModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.12", ngImport: i0, type: SseModule });
354
+ SseModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: SseModule });
355
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.12", ngImport: i0, type: SseModule, decorators: [{
356
+ type: NgModule,
357
+ args: [{
358
+ declarations: [],
359
+ imports: [],
360
+ exports: [],
361
+ }]
362
+ }] });
363
+
364
+ /*
365
+ * Copyright 2015-2020 Ritense BV, the Netherlands.
366
+ *
367
+ * Licensed under EUPL, Version 1.2 (the "License");
368
+ * you may not use this file except in compliance with the License.
369
+ * You may obtain a copy of the License at
370
+ *
371
+ * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
372
+ *
373
+ * Unless required by applicable law or agreed to in writing, software
374
+ * distributed under the License is distributed on an "AS IS" basis,
375
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
376
+ * See the License for the specific language governing permissions and
377
+ * limitations under the License.
378
+ */
379
+
380
+ /**
381
+ * Generated bundle index. Do not edit.
382
+ */
383
+
384
+ export { SseErrorBucket, SseEventSubscriptionBucket, SseModule, SseService, SseSubscriptionBucket };
385
+ //# sourceMappingURL=valtimo-sse.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valtimo-sse.mjs","sources":["../../../../projects/valtimo/sse/src/lib/models/sse-bucket.model.ts","../../../../projects/valtimo/sse/src/lib/models/sse-events.model.ts","../../../../projects/valtimo/sse/src/lib/models/index.ts","../../../../projects/valtimo/sse/src/lib/services/sse.service.ts","../../../../projects/valtimo/sse/src/lib/services/index.ts","../../../../projects/valtimo/sse/src/lib/sse.module.ts","../../../../projects/valtimo/sse/src/public-api.ts","../../../../projects/valtimo/sse/src/valtimo-sse.ts"],"sourcesContent":["/*\n * Copyright 2015-2020 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// generic bucket for listeners of any type\nimport {BaseSseEvent, SseError, SseEventListener} from './sse-events.model';\n\nexport class SseSubscriptionBucket<T> {\n readonly listeners: SseEventListener<T>[] = [];\n\n on(listener: SseEventListener<T>) {\n this.listeners.push(listener);\n }\n\n off(listener: SseEventListener<T>) {\n const index = this.listeners.indexOf(listener);\n if (index > -1) {\n this.listeners.splice(index, 1);\n }\n }\n\n offAll() {\n this.listeners.length = 0;\n }\n\n sendEvent(event: T) {\n this.listeners.forEach(listener => {\n listener(event);\n });\n }\n}\n\n// error bucket for error listeners\nexport class SseErrorBucket extends SseSubscriptionBucket<SseError> {}\n\n// implicit type bucket with event for filtering\nexport class SseEventSubscriptionBucket<T extends BaseSseEvent> extends SseSubscriptionBucket<T> {\n readonly event: string;\n\n constructor(event: string) {\n super();\n this.event = event;\n }\n}\n","/*\n * Copyright 2015-2020 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\ntype SseEventType =\n | 'CASE_CREATED'\n | 'TASK_UPDATE'\n | 'PROCESS_END'\n | 'CASE_ASSIGNED'\n | 'CASE_UNASSIGNED'\n | 'ESTABLISHED_CONNECTION';\n\ntype SseEventListener<T> = (event: T) => void;\n\n// base event containing the event type\ninterface BaseSseEvent {\n eventType?: SseEventType;\n processInstanceId?: string;\n}\n\n// event implementations for json mapping objects in:\ninterface TaskUpdateSseEvent extends BaseSseEvent {\n processInstanceId: string;\n}\n\ninterface EstablishedConnectionSseEvent extends BaseSseEvent {\n subscriptionId: string;\n}\n\ninterface SseError {\n state: number;\n message: string;\n data?: any;\n}\n\nexport {\n SseError,\n EstablishedConnectionSseEvent,\n TaskUpdateSseEvent,\n BaseSseEvent,\n SseEventListener,\n SseEventType,\n};\n","/*\n * Copyright 2015-2020 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport * from './sse-bucket.model';\nexport * from './sse-events.model';\n","/*\n * Copyright 2015-2020 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {Injectable} from '@angular/core';\nimport {filter, Observable, Subject, Subscription} from 'rxjs';\nimport {\n BaseSseEvent,\n EstablishedConnectionSseEvent,\n SseEventListener,\n SseEventType,\n} from '../models/sse-events.model';\nimport {ConfigService} from '@valtimo/config';\nimport {\n SseErrorBucket,\n SseEventSubscriptionBucket,\n SseSubscriptionBucket,\n} from '../models/sse-bucket.model';\nimport {NGXLogger} from 'ngx-logger';\n\n/**\n * Server-side events service, for connecting and reconnecting to SSE,\n * and a translation layer between SSE json string data and TS typed events.\n *\n * The service is always present, and so events can be registered with {@link onMessage} and {@link onEvent},\n * and will not be lost on disconnect or reconnect of the SSE connection itself.\n * It is expected to also unregister events using the off-method variants of the on-method listeners.\n *\n * To ensure messages are received the connection must be explicitly opened with {@link connect},\n * likewise the connection should also be closed with {@link disconnect} to stop receiving messages.\n *\n * This service can reconnect itself with the backend, and might use the {@link subscriptionId} to ask for its\n * previous connection state. This state currently exists in the backend as the \"Subscriber\" class, and might\n * have a state containing queues of items that need to be sent, so you can reconnect and receive the rest.\n *\n * At this time no state data is kept, so reconnection without a subscriptionId does not give a different result\n */\n@Injectable({\n providedIn: 'root',\n})\nexport class SseService {\n private static readonly CONNECTION_RETRIES_EXCEEDED = -1;\n private static readonly NOT_CONNECTED = 0;\n private static readonly CONNECTING = 1;\n private static readonly RECONNECTING = 2;\n private static readonly CONNECTED = 10;\n\n private readonly VALTIMO_ENDPOINT_URL: string;\n private connectionCount = 0; // amount of times we have connected sequentially, no concurrent connections\n private establishedDataHandler?: Subscription = null;\n private establishedConnection?: EventSource = null;\n private establishedConnectionObservable: Observable<MessageEvent<BaseSseEvent>> = null;\n private subscriptionId?: string = null;\n private sequentialConnectionAttemptFailCount = 0;\n\n private errorBucket = new SseErrorBucket();\n private anySubscribersBucket = new SseSubscriptionBucket<BaseSseEvent>();\n private eventSubscribersBuckets: SseEventSubscriptionBucket<BaseSseEvent>[] = [];\n\n private state: number = SseService.NOT_CONNECTED;\n\n private _sseMessages$ = new Subject<MessageEvent<BaseSseEvent>>();\n\n get sseMessages$(): Observable<MessageEvent<BaseSseEvent>> {\n return this._sseMessages$.asObservable().pipe(filter(message => !!message));\n }\n\n constructor(private readonly configService: ConfigService, private readonly logger: NGXLogger) {\n this.VALTIMO_ENDPOINT_URL = configService.config.valtimoApi.endpointUri;\n this.connect();\n }\n\n getSseMessagesObservableByEventType(\n eventTypes: Array<SseEventType>\n ): Observable<MessageEvent<BaseSseEvent>> {\n return this._sseMessages$.asObservable().pipe(\n filter(message => !!message),\n filter(message => eventTypes.includes(message?.data?.eventType))\n );\n }\n\n /**\n * Start receiving SSE events\n */\n private connect() {\n this.ensureConnection();\n return this;\n }\n\n private onMessage(listener: SseEventListener<BaseSseEvent>) {\n this.ensureConnection(); // ensure connection\n this.anySubscribersBucket.on(listener);\n return this;\n }\n\n private onEvent<T extends BaseSseEvent>(event: string, listener: SseEventListener<T>) {\n this.ensureConnection(); // ensure connection\n let found = false;\n this.eventSubscribersBuckets.forEach(bucket => {\n if (bucket.event === event) {\n bucket.on(listener);\n found = true;\n }\n });\n if (!found) {\n const bucket = new SseEventSubscriptionBucket(event);\n bucket.on(listener);\n this.eventSubscribersBuckets.push(bucket);\n }\n return this;\n }\n\n private offMessage(listener: SseEventListener<BaseSseEvent>) {\n this.anySubscribersBucket.off(listener);\n }\n\n private offEvent(event: string, listener: SseEventListener<any>) {\n this.eventSubscribersBuckets.forEach(bucket => {\n if (bucket.event === event) {\n bucket.off(listener);\n }\n });\n }\n\n private offEvents(type?: string) {\n this.eventSubscribersBuckets.forEach(bucket => {\n if (type === null || type === bucket.event) {\n bucket.offAll();\n }\n });\n }\n\n private offMessages() {\n this.anySubscribersBucket.offAll();\n }\n\n private offAll() {\n this.offEvents();\n this.offMessages();\n }\n\n private disconnect(keepSubscriptionId: boolean = false) {\n this.disconnectWith(SseService.NOT_CONNECTED, keepSubscriptionId);\n }\n\n private disconnectWith(state: number, keepSubscriptionId: boolean = false) {\n this.state = state;\n if (this.establishedConnection?.readyState !== EventSource.CLOSED) {\n this.establishedConnection?.close();\n }\n this.establishedDataHandler.unsubscribe();\n this.establishedConnection = null;\n this.establishedConnectionObservable = null;\n this.establishedDataHandler = null;\n if (!keepSubscriptionId) {\n this.subscriptionId = null;\n }\n }\n\n private ensureConnection(retry: boolean = false) {\n if (this.establishedConnection !== null && this.establishedConnectionObservable !== null) {\n if (this.establishedConnection.readyState !== EventSource.CLOSED) {\n return; // found\n }\n }\n if (this.state === SseService.CONNECTING || this.state === SseService.RECONNECTING) {\n return; // already connecting\n }\n this.establishedConnection = null;\n this.establishedConnectionObservable = null;\n\n this.constructNewSse(retry); // create new\n }\n\n private constructNewSse(retry: boolean) {\n this.logger.debug('subscribing to sse events');\n if (this.connectionCount > 0) {\n this.state = SseService.RECONNECTING;\n } else {\n this.state = SseService.CONNECTING;\n }\n const observable = new Observable<MessageEvent<BaseSseEvent>>(observer => {\n const eventSource = this.getEventSource();\n eventSource.onopen = () => {\n this.establishedConnection = eventSource;\n this.logger.debug('connected to sse');\n this.connectionCount++;\n this.sequentialConnectionAttemptFailCount = 0; // reset retry count\n this.state = SseService.CONNECTED;\n };\n eventSource.onmessage = event => {\n observer.next({\n ...event, // forward event object but replace data field\n data: JSON.parse(event.data), // parse JSON string to JSON object\n });\n };\n eventSource.onerror = () => {\n eventSource.close();\n observer.complete();\n if (retry) {\n this.sequentialConnectionAttemptFailCount++;\n this.logger.debug(`retry failed: ${this.sequentialConnectionAttemptFailCount}`);\n if (this.sequentialConnectionAttemptFailCount > 3) {\n this.disconnectWith(SseService.CONNECTION_RETRIES_EXCEEDED, false);\n this.err(\n `Failed to connect to SSE after ${this.sequentialConnectionAttemptFailCount} retries`\n );\n return;\n }\n }\n this.disconnect(true);\n this.ensureConnection(true);\n };\n return () => eventSource.close();\n });\n this.establishedConnectionObservable = observable;\n this.registerSseEventHandling(observable);\n return observable;\n }\n\n private getEventSource() {\n let suffix = '';\n if (this.subscriptionId != null) {\n suffix = '/' + this.subscriptionId;\n }\n // subscribe to /sse or /sse/<subscriptionId>\n return new EventSource(this.VALTIMO_ENDPOINT_URL + 'v1/sse' + suffix);\n }\n\n private registerSseEventHandling(observable: Observable<MessageEvent<BaseSseEvent>>) {\n this.establishedDataHandler = observable.subscribe(event => {\n this._sseMessages$.next(event);\n this.internalListenerEstablishConnection(event);\n // notify all generic listeners\n this.anySubscribersBucket.sendEvent(event.data);\n // notify the specific event listener bucket\n this.eventSubscribersBuckets.forEach(bucket => {\n if (bucket.event === event?.data?.eventType) {\n bucket.sendEvent(event.data);\n }\n });\n });\n }\n\n private internalListenerEstablishConnection(event: MessageEvent<BaseSseEvent>) {\n if (event?.data?.eventType !== 'ESTABLISHED_CONNECTION') {\n return;\n }\n this.logger.debug(`established connection: ${event}`);\n this.subscriptionId = (event.data as EstablishedConnectionSseEvent).subscriptionId;\n }\n\n private err(message: string, ...data: any) {\n this.errorBucket.sendEvent({\n state: this.state,\n message,\n data,\n });\n }\n}\n","/*\n * Copyright 2015-2020 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport * from './sse.service';\n","/*\n * Copyright 2015-2020 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {NgModule} from '@angular/core';\n\n@NgModule({\n declarations: [],\n imports: [],\n exports: [],\n})\nexport class SseModule {}\n","/*\n * Copyright 2015-2020 Ritense BV, the Netherlands.\n *\n * Licensed under EUPL, Version 1.2 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" basis,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/*\n * Public API Surface of sse\n */\n\nexport * from './lib/models';\nexport * from './lib/services';\nexport * from './lib/sse.module';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;;AAAA;;;;;;;;;;;;;;AAcG;MAKU,qBAAqB,CAAA;AAAlC,IAAA,WAAA,GAAA;QACW,IAAS,CAAA,SAAA,GAA0B,EAAE,CAAC;KAsBhD;AApBC,IAAA,EAAE,CAAC,QAA6B,EAAA;AAC9B,QAAA,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;KAC/B;AAED,IAAA,GAAG,CAAC,QAA6B,EAAA;QAC/B,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;AAC/C,QAAA,IAAI,KAAK,GAAG,CAAC,CAAC,EAAE;YACd,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;AACjC,SAAA;KACF;IAED,MAAM,GAAA;AACJ,QAAA,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;KAC3B;AAED,IAAA,SAAS,CAAC,KAAQ,EAAA;AAChB,QAAA,IAAI,CAAC,SAAS,C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AA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA,CAAA;;;ACnDD;;;;;;;;;;;;;;AAcG;;ACdH;;;;;;;;;;;;;;AAcG;MASU,SAAS,CAAA;;uGAAT,SAAS,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,QAAA,EAAA,CAAA,CAAA;wGAAT,SAAS,EAAA,CAAA,CAAA;wGAAT,SAAS,EAAA,CAAA,CAAA;4FAAT,SAAS,EAAA,UAAA,EAAA,CAAA;kBALrB,QAAQ;AAAC,YAAA,IAAA,EAAA,CAAA;AACR,oBAAA,YAAY,EAAE,EAAE;AAChB,oBAAA,OAAO,EAAE,EAAE;AACX,oBAAA,OAAO,EAAE,EAAE;AACZ,iBAAA,CAAA;;;ACtBD;;;;;;;;;;;;;;AAcG;;ACdH;;AAEG;;;;"}
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generated bundle index. Do not edit.
3
+ */
4
+ /// <amd-module name="@valtimo/sse" />
5
+ export * from './public-api';
@@ -0,0 +1,2 @@
1
+ export * from './sse-bucket.model';
2
+ export * from './sse-events.model';
@@ -0,0 +1,14 @@
1
+ import { BaseSseEvent, SseError, SseEventListener } from './sse-events.model';
2
+ export declare class SseSubscriptionBucket<T> {
3
+ readonly listeners: SseEventListener<T>[];
4
+ on(listener: SseEventListener<T>): void;
5
+ off(listener: SseEventListener<T>): void;
6
+ offAll(): void;
7
+ sendEvent(event: T): void;
8
+ }
9
+ export declare class SseErrorBucket extends SseSubscriptionBucket<SseError> {
10
+ }
11
+ export declare class SseEventSubscriptionBucket<T extends BaseSseEvent> extends SseSubscriptionBucket<T> {
12
+ readonly event: string;
13
+ constructor(event: string);
14
+ }
@@ -0,0 +1,18 @@
1
+ declare type SseEventType = 'CASE_CREATED' | 'TASK_UPDATE' | 'PROCESS_END' | 'CASE_ASSIGNED' | 'CASE_UNASSIGNED' | 'ESTABLISHED_CONNECTION';
2
+ declare type SseEventListener<T> = (event: T) => void;
3
+ interface BaseSseEvent {
4
+ eventType?: SseEventType;
5
+ processInstanceId?: string;
6
+ }
7
+ interface TaskUpdateSseEvent extends BaseSseEvent {
8
+ processInstanceId: string;
9
+ }
10
+ interface EstablishedConnectionSseEvent extends BaseSseEvent {
11
+ subscriptionId: string;
12
+ }
13
+ interface SseError {
14
+ state: number;
15
+ message: string;
16
+ data?: any;
17
+ }
18
+ export { SseError, EstablishedConnectionSseEvent, TaskUpdateSseEvent, BaseSseEvent, SseEventListener, SseEventType, };
@@ -0,0 +1 @@
1
+ export * from './sse.service';
@@ -0,0 +1,67 @@
1
+ import { Observable } from 'rxjs';
2
+ import { BaseSseEvent, SseEventType } from '../models/sse-events.model';
3
+ import { ConfigService } from '@valtimo/config';
4
+ import { NGXLogger } from 'ngx-logger';
5
+ import * as i0 from "@angular/core";
6
+ /**
7
+ * Server-side events service, for connecting and reconnecting to SSE,
8
+ * and a translation layer between SSE json string data and TS typed events.
9
+ *
10
+ * The service is always present, and so events can be registered with {@link onMessage} and {@link onEvent},
11
+ * and will not be lost on disconnect or reconnect of the SSE connection itself.
12
+ * It is expected to also unregister events using the off-method variants of the on-method listeners.
13
+ *
14
+ * To ensure messages are received the connection must be explicitly opened with {@link connect},
15
+ * likewise the connection should also be closed with {@link disconnect} to stop receiving messages.
16
+ *
17
+ * This service can reconnect itself with the backend, and might use the {@link subscriptionId} to ask for its
18
+ * previous connection state. This state currently exists in the backend as the "Subscriber" class, and might
19
+ * have a state containing queues of items that need to be sent, so you can reconnect and receive the rest.
20
+ *
21
+ * At this time no state data is kept, so reconnection without a subscriptionId does not give a different result
22
+ */
23
+ export declare class SseService {
24
+ private readonly configService;
25
+ private readonly logger;
26
+ private static readonly CONNECTION_RETRIES_EXCEEDED;
27
+ private static readonly NOT_CONNECTED;
28
+ private static readonly CONNECTING;
29
+ private static readonly RECONNECTING;
30
+ private static readonly CONNECTED;
31
+ private readonly VALTIMO_ENDPOINT_URL;
32
+ private connectionCount;
33
+ private establishedDataHandler?;
34
+ private establishedConnection?;
35
+ private establishedConnectionObservable;
36
+ private subscriptionId?;
37
+ private sequentialConnectionAttemptFailCount;
38
+ private errorBucket;
39
+ private anySubscribersBucket;
40
+ private eventSubscribersBuckets;
41
+ private state;
42
+ private _sseMessages$;
43
+ get sseMessages$(): Observable<MessageEvent<BaseSseEvent>>;
44
+ constructor(configService: ConfigService, logger: NGXLogger);
45
+ getSseMessagesObservableByEventType(eventTypes: Array<SseEventType>): Observable<MessageEvent<BaseSseEvent>>;
46
+ /**
47
+ * Start receiving SSE events
48
+ */
49
+ private connect;
50
+ private onMessage;
51
+ private onEvent;
52
+ private offMessage;
53
+ private offEvent;
54
+ private offEvents;
55
+ private offMessages;
56
+ private offAll;
57
+ private disconnect;
58
+ private disconnectWith;
59
+ private ensureConnection;
60
+ private constructNewSse;
61
+ private getEventSource;
62
+ private registerSseEventHandling;
63
+ private internalListenerEstablishConnection;
64
+ private err;
65
+ static ɵfac: i0.ɵɵFactoryDeclaration<SseService, never>;
66
+ static ɵprov: i0.ɵɵInjectableDeclaration<SseService>;
67
+ }
@@ -0,0 +1,6 @@
1
+ import * as i0 from "@angular/core";
2
+ export declare class SseModule {
3
+ static ɵfac: i0.ɵɵFactoryDeclaration<SseModule, never>;
4
+ static ɵmod: i0.ɵɵNgModuleDeclaration<SseModule, never, never, never>;
5
+ static ɵinj: i0.ɵɵInjectorDeclaration<SseModule>;
6
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@valtimo/sse",
3
+ "version": "10.5.0",
4
+ "peerDependencies": {
5
+ "@angular/common": "^14.2.0",
6
+ "@angular/core": "^14.2.0"
7
+ },
8
+ "dependencies": {
9
+ "tslib": "^2.3.0"
10
+ },
11
+ "module": "fesm2015/valtimo-sse.mjs",
12
+ "es2020": "fesm2020/valtimo-sse.mjs",
13
+ "esm2020": "esm2020/valtimo-sse.mjs",
14
+ "fesm2020": "fesm2020/valtimo-sse.mjs",
15
+ "fesm2015": "fesm2015/valtimo-sse.mjs",
16
+ "typings": "index.d.ts",
17
+ "exports": {
18
+ "./package.json": {
19
+ "default": "./package.json"
20
+ },
21
+ ".": {
22
+ "types": "./index.d.ts",
23
+ "esm2020": "./esm2020/valtimo-sse.mjs",
24
+ "es2020": "./fesm2020/valtimo-sse.mjs",
25
+ "es2015": "./fesm2015/valtimo-sse.mjs",
26
+ "node": "./fesm2015/valtimo-sse.mjs",
27
+ "default": "./fesm2020/valtimo-sse.mjs"
28
+ }
29
+ },
30
+ "sideEffects": false
31
+ }
@@ -0,0 +1,3 @@
1
+ export * from './lib/models';
2
+ export * from './lib/services';
3
+ export * from './lib/sse.module';