From 0bc09ae39bb9598fc86f059270cfb574dffde7d6 Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Tue, 2 Jul 2024 22:04:18 +0200 Subject: [PATCH] Rm unused _subs + clear() and destroy() test (#6) --- index.js | 8 ++++++-- test.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f93ccac..7757727 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,6 @@ class GlobalCache { this._array = parent?._array || [] this._map = new Map() - this._subs = 0 } get globalSize () { @@ -71,13 +70,18 @@ class GlobalCache { } clear () { + // The entries in map linger on in _array, + // so on top of clearing the map, we also kill the ref, + // so that any gc running later on the old map won't interfere + // (in case a new entry was added with the same key as a cleared entry) + this._map.clear() - // instead of clearing the map, we kill the ref, so that any gc running on the old map wont interfere this._map = new Map() } destroy () { this._map = null + this._array = null } _gc () { diff --git a/test.js b/test.js index 907ef20..9ea4263 100644 --- a/test.js +++ b/test.js @@ -140,6 +140,53 @@ test('values()', t => { t.alike([...sub.values()], ['value', 'ever2'], 'expected values') }) +test('clear()', t => { + const cache = new GlobalCache() + const sub = cache.sub() + + cache.set('key', 'value') + cache.set('key2', 'value2') + sub.set('key', 'value') + sub.set('what2', 'ever2') + + sub.clear() + t.is(sub.size, 0, 'cleared') + + // DEVNOTE: this assertion documents the current behaviour, + // that cleared entries linger on in the _array. But if this + // test ever fails because we no longer have those entries linger + // then it's fine to modify this assertion + t.is(sub.globalSize, 4, 'elements still exist in the array') + + sub.set('key', 'value') // Reset it + t.is(sub.globalSize, 5, 'old and new entry exist simultaneously') + + sub.delete('key') + t.is(sub.globalSize, 4, 'old entry unaffected by new entry deletion') +}) + +test('destroy()', t => { + const cache = new GlobalCache() + const sub = cache.sub() + + cache.set('key', 'value') + cache.set('key2', 'value2') + sub.set('key', 'value') + sub.set('what2', 'ever2') + + sub.destroy() + t.is(sub._array, null, 'null internal array') + t.is(sub._map, null, 'null internal map') + t.is(cache._array !== null, true, 'no null for cache') + t.is(cache._map !== null, true, 'no null for cache') + + // DEVNOTE: this assertion documents the current behaviour, + // that destroyed entries linger on in the _array. But if this + // test ever fails because we no longer have those entries linger + // then it's fine to modify this assertion + t.is(cache.globalSize, 4, 'not yet removed from internal array') +}) + test('internal structure remains consistent', t => { const cache = new GlobalCache({ maxSize: 3 })