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 Oct 7, 2021
1 parent ce7addb commit 82f5cd6
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 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

let timers = new Map()
let datas = new Map()
let fields = new Map()
Expand All @@ -45,8 +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 @@ -62,9 +64,11 @@ const createEntitiesStore = (ttl, now) => {
del(id)
cache = null

// todo: use sth more memory-efficient than closures?
// todo: set expiry relative to entity's timestamp?
timers.set(id, setTimeout(del, ttl, id))
if (useTtl) {
// todo: use sth more memory-efficient than closures?
// todo: set expiry relative to entity's timestamp?
timers.set(id, setTimeout(del, ttl, id))
}

FeedEntity.verify(entity)
const data = FeedEntity.encode(entity).finish()
Expand Down Expand Up @@ -93,7 +97,7 @@ const createEntitiesStore = (ttl, now) => {
cache = null
}

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

const getTimestamp = () => {
return timestamps.length > 0
Expand All @@ -104,7 +108,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 82f5cd6

Please sign in to comment.