Skip to content

Commit e9bf58a

Browse files
committed
fix(record): use weak refs (#30)
* fix(record): use weak refs * Update src/record/record-handler.js * Update src/record/record-handler.js
1 parent 14b028f commit e9bf58a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/record/record-handler.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,19 @@ class RecordHandler {
9797
this._connection = connection
9898
this._client = client
9999
this._records = new Map()
100+
this._cache = new Map()
100101
this._listeners = new Map()
101102
this._pruning = new Set()
102103
this._patching = new Map()
103104
this._updating = new Map()
104105

106+
this._registry = new FinalizationRegistry((name) => {
107+
const entry = this._cache.get(name)
108+
if (entry && entry.deref && entry.deref() === undefined) {
109+
this._cache.delete(name)
110+
}
111+
})
112+
105113
this._connected = 0
106114
this._stats = {
107115
updating: 0,
@@ -134,6 +142,11 @@ class RecordHandler {
134142
for (const rec of pruning) {
135143
rec._$dispose()
136144
this._records.delete(rec.name)
145+
146+
if (!this._cache.has(rec.name)) {
147+
this._cache.set(rec.name, new WeakRef(rec))
148+
this._registry.register(rec, rec.name)
149+
}
137150
}
138151

139152
this._stats.pruning -= pruning.size
@@ -219,7 +232,7 @@ class RecordHandler {
219232
let record = this._records.get(name)
220233

221234
if (!record) {
222-
record = new Record(name, this)
235+
record = this._cache.get(name)?.deref() ?? new Record(name, this)
223236
this._stats.records += 1
224237
this._stats.created += 1
225238
this._records.set(name, record)

0 commit comments

Comments
 (0)