@jsenv/server 12.0.0 → 12.1.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.
- package/package.json +1 -1
- package/src/sse/createSSERoom.js +16 -0
package/package.json
CHANGED
package/src/sse/createSSERoom.js
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
// https://www.html5rocks.com/en/tutorials/eventsource/basics/
|
|
9
9
|
export const createSSERoom = ({
|
|
10
10
|
logLevel,
|
|
11
|
+
effect = () => {},
|
|
11
12
|
// do not keep process alive because of rooms, something else must keep it alive
|
|
12
13
|
keepProcessAlive = false,
|
|
13
14
|
keepaliveDuration = 30 * 1000,
|
|
@@ -29,6 +30,7 @@ export const createSSERoom = ({
|
|
|
29
30
|
let previousEventId = 0
|
|
30
31
|
let opened = false
|
|
31
32
|
let interval
|
|
33
|
+
let cleanupEffect = CLEANUP_NOOP
|
|
32
34
|
|
|
33
35
|
const join = (request) => {
|
|
34
36
|
// should we ensure a given request can join a room only once?
|
|
@@ -56,6 +58,14 @@ export const createSSERoom = ({
|
|
|
56
58
|
next,
|
|
57
59
|
complete,
|
|
58
60
|
}
|
|
61
|
+
if (clients.size === 0) {
|
|
62
|
+
const effectReturnValue = effect()
|
|
63
|
+
if (typeof effectReturnValue === "function") {
|
|
64
|
+
cleanupEffect = effectReturnValue
|
|
65
|
+
} else {
|
|
66
|
+
cleanupEffect = CLEANUP_NOOP
|
|
67
|
+
}
|
|
68
|
+
}
|
|
59
69
|
clients.add(client)
|
|
60
70
|
logger.debug(
|
|
61
71
|
`A client has joined. Number of client in room: ${clients.size}`,
|
|
@@ -101,6 +111,10 @@ export const createSSERoom = ({
|
|
|
101
111
|
|
|
102
112
|
return () => {
|
|
103
113
|
clients.delete(client)
|
|
114
|
+
if (clients.size === 0) {
|
|
115
|
+
cleanupEffect()
|
|
116
|
+
cleanupEffect = CLEANUP_NOOP
|
|
117
|
+
}
|
|
104
118
|
logger.debug(`A client left. Number of client in room: ${clients.size}`)
|
|
105
119
|
}
|
|
106
120
|
})
|
|
@@ -210,6 +224,8 @@ export const createSSERoom = ({
|
|
|
210
224
|
return room
|
|
211
225
|
}
|
|
212
226
|
|
|
227
|
+
const CLEANUP_NOOP = () => {}
|
|
228
|
+
|
|
213
229
|
const requestMap = new Map()
|
|
214
230
|
|
|
215
231
|
const connectRequestAndRoom = (request, room, roomObservable) => {
|