@jsenv/server
Advanced tools
Comparing version 2.6.0 to 2.7.0
{ | ||
"name": "@jsenv/server", | ||
"version": "2.6.0", | ||
"version": "2.7.0", | ||
"description": "High level api for node.js server.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -11,2 +11,4 @@ import { createLogger } from "@jsenv/logger" | ||
maxConnectionAllowed = 100, // max 100 users accepted | ||
computeEventId = (event, lastEventId) => lastEventId + 1, | ||
joinEvent = false, | ||
} = {}) => { | ||
@@ -16,7 +18,7 @@ const logger = createLogger({ logLevel }) | ||
const connections = new Set() | ||
// what about history that keeps growing ? | ||
// we should add some limit | ||
// one limit could be that an event older than 24h is be deleted | ||
const history = createEventHistory(historyLength) | ||
let previousEventId | ||
const eventHistory = createEventHistory(historyLength) | ||
// what about previousEventId that keeps growing ? | ||
// we could add some limit | ||
// one limit could be that an event older than 24h is deleted | ||
let previousEventId = 0 | ||
let state = "closed" | ||
@@ -37,15 +39,18 @@ let interval | ||
const joinEvent = { | ||
id: previousEventId === undefined ? 0 : previousEventId + 1, | ||
const firstEvent = { | ||
retry: retryDuration, | ||
type: "join", | ||
type: joinEvent ? "join" : "comment", | ||
data: new Date().toLocaleTimeString(), | ||
} | ||
previousEventId = joinEvent.id | ||
history.add(joinEvent) | ||
if (joinEvent) { | ||
firstEvent.id = computeEventId(firstEvent, previousEventId) | ||
previousEventId = firstEvent.id | ||
eventHistory.add(firstEvent) | ||
} | ||
const events = [ | ||
joinEvent, | ||
// send events which occured between lastKnownId & now | ||
...(lastKnownId === undefined ? [] : history.since(lastKnownId)), | ||
...(lastKnownId === undefined ? [] : eventHistory.since(lastKnownId)), | ||
firstEvent, | ||
] | ||
@@ -106,5 +111,5 @@ | ||
) | ||
event.id = previousEventId === undefined ? 0 : previousEventId + 1 | ||
event.id = computeEventId(event, previousEventId) | ||
previousEventId = event.id | ||
history.add(event) | ||
eventHistory.add(event) | ||
} | ||
@@ -135,7 +140,9 @@ | ||
clearInterval(interval) | ||
history.reset() | ||
eventHistory.reset() | ||
state = "stopped" | ||
} | ||
return { start, stop, connect, sendEvent } | ||
const eventsSince = (id) => eventHistory.since(id) | ||
return { start, stop, connect, eventsSince, sendEvent } | ||
} | ||
@@ -171,3 +178,2 @@ | ||
const events = [] | ||
let removedCount = 0 | ||
@@ -179,13 +185,8 @@ const add = (data) => { | ||
events.shift() | ||
removedCount++ | ||
} | ||
} | ||
const since = (index) => { | ||
index = parseInt(index) | ||
if (isNaN(index)) { | ||
throw new TypeError("history.since() expect a number") | ||
} | ||
index -= removedCount | ||
return index < 0 ? [] : events.slice(index) | ||
const since = (id) => { | ||
const index = events.findIndex((event) => event.id === id) | ||
return index === -1 ? [] : events.slice(index) | ||
} | ||
@@ -195,3 +196,2 @@ | ||
events.length = 0 | ||
removedCount = 0 | ||
} | ||
@@ -198,0 +198,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
245983
4558