Skip to content

Commit

Permalink
lib/entities-store: support ttl: Infinity
Browse files Browse the repository at this point in the history
see also #1
  • Loading branch information
derhuerst committed Mar 13, 2020
1 parent b0377dc commit f2909e9
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/entities-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ const encodeField = (fieldNumber, wireType, dataLength) => {
}

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

let timers = new Map()
let datas = new Map()
let fields = new Map()
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 @@ -43,10 +45,12 @@ const createEntitiesStore = (ttl, timestamp) => {
del(id)
cache = null

// todo: use sth more memory-efficient than closures?
timers.set(id, setTimeout(() => {
remove(id)
}, ttl))
if (useTtl) {
// todo: use sth more memory-efficient than closures?
timers.set(id, setTimeout(() => {
remove(id)
}, ttl))
}

const data = FeedEntity.encode(entity)
datas.set(id, data)
Expand All @@ -67,12 +71,12 @@ const createEntitiesStore = (ttl, timestamp) => {
cache = null
}

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

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 header = chunks[1] = FeedHeader.encode({
Expand Down

0 comments on commit f2909e9

Please sign in to comment.