Skip to content

Commit

Permalink
Rm unused _subs + clear() and destroy() test (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote authored Jul 2, 2024
1 parent 04124d5 commit 0bc09ae
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class GlobalCache {

this._array = parent?._array || []
this._map = new Map()
this._subs = 0
}

get globalSize () {
Expand Down Expand Up @@ -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 () {
Expand Down
47 changes: 47 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down

0 comments on commit 0bc09ae

Please sign in to comment.