Skip to content

Commit

Permalink
fix(record): use weak refs (#30)
Browse files Browse the repository at this point in the history
* fix(record): use weak refs

* Update src/record/record-handler.js

* Update src/record/record-handler.js
  • Loading branch information
ronag committed Jul 17, 2024
1 parent 14b028f commit e9bf58a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/record/record-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,19 @@ class RecordHandler {
this._connection = connection
this._client = client
this._records = new Map()
this._cache = new Map()
this._listeners = new Map()
this._pruning = new Set()
this._patching = new Map()
this._updating = new Map()

this._registry = new FinalizationRegistry((name) => {
const entry = this._cache.get(name)
if (entry && entry.deref && entry.deref() === undefined) {
this._cache.delete(name)
}
})

this._connected = 0
this._stats = {
updating: 0,
Expand Down Expand Up @@ -134,6 +142,11 @@ class RecordHandler {
for (const rec of pruning) {
rec._$dispose()
this._records.delete(rec.name)

if (!this._cache.has(rec.name)) {
this._cache.set(rec.name, new WeakRef(rec))
this._registry.register(rec, rec.name)
}
}

this._stats.pruning -= pruning.size
Expand Down Expand Up @@ -219,7 +232,7 @@ class RecordHandler {
let record = this._records.get(name)

if (!record) {
record = new Record(name, this)
record = this._cache.get(name)?.deref() ?? new Record(name, this)
this._stats.records += 1
this._stats.created += 1
this._records.set(name, record)
Expand Down

0 comments on commit e9bf58a

Please sign in to comment.