Skip to content

Commit

Permalink
lib/entities-store: support a TTL of Infinity
Browse files Browse the repository at this point in the history
see also #1
  • Loading branch information
derhuerst committed Sep 15, 2024
1 parent a90e229 commit b808845
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/entities-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const entityTimestamp = (entity) => {
}

const createEntitiesStore = (ttl, now) => {
const useTtl = ttl !== Infinity

const timers = new Map()
const datas = new Map()
const fields = new Map()
Expand All @@ -45,9 +47,8 @@ const createEntitiesStore = (ttl, now) => {
let cache = null // cached final `FeedMessage` buffer

const del = (id) => {
if (!timers.has(id)) return;

clearTimeout(timers.get(id))
if (!datas.has(id)) return;
if (useTtl) clearTimeout(timers.get(id))
timers.delete(id)
datas.delete(id)
fields.delete(id)
Expand All @@ -63,7 +64,7 @@ const createEntitiesStore = (ttl, now) => {
// console.error('put', id, entity) // todo: remove
del(id)

{
if (useTtl) {
// todo: use sth more memory-efficient than closures?
// todo: set expiry relative to entity's timestamp?
const timer = setTimeout(del, ttl, id)
Expand Down Expand Up @@ -104,7 +105,7 @@ const createEntitiesStore = (ttl, now) => {
cache = null
}

const nrOfEntities = () => timers.size
const nrOfEntities = () => datas.size

const getTimestamp = () => {
return timestamps.length > 0
Expand All @@ -115,7 +116,7 @@ const createEntitiesStore = (ttl, now) => {
const asFeedMessage = () => {
if (cache !== null) return cache

const ids = Array.from(timers.keys())
const ids = Array.from(datas.keys())
const chunks = new Array(2 + ids.length * 2)

const rawHeader = {
Expand Down

0 comments on commit b808845

Please sign in to comment.