Skip to content
Snippets Groups Projects
Commit ea6cd311 authored by Alexander Rose's avatar Alexander Rose
Browse files

fix ref-cache deleting, added spec

parent 0e3f6d8c
No related branches found
No related tags found
No related merge requests found
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { idFactory } from '../id-factory'
describe('id-factory', () => {
it('basic', () => {
const getNextId = idFactory()
expect(getNextId()).toBe(0)
expect(getNextId()).toBe(1)
})
it('start-id', () => {
const getNextId = idFactory(5)
expect(getNextId()).toBe(5)
expect(getNextId()).toBe(6)
})
it('negative-start-id', () => {
const getNextId = idFactory(-1)
expect(getNextId()).toBe(-1)
expect(getNextId()).toBe(0)
})
});
\ No newline at end of file
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { createReferenceCache } from '../reference-cache'
describe('reference-cache', () => {
it('basic', () => {
const refCache = createReferenceCache(
(x: number) => x.toString(),
(ctx: {}, x) => x,
() => {}
)
expect(refCache.count).toBe(0)
const ctx = {}
const ref2a = refCache.get(ctx, 2)
expect(refCache.count).toBe(1)
const ref2b = refCache.get(ctx, 2)
expect(refCache.count).toBe(1)
expect(ref2b.value).toBe(2)
const ref3 = refCache.get(ctx, 3)
expect(refCache.count).toBe(2)
expect(ref3.value).toBe(3)
ref2a.free()
refCache.clear()
expect(refCache.count).toBe(2)
ref2b.free()
refCache.clear()
expect(refCache.count).toBe(1)
refCache.dispose()
expect(refCache.count).toBe(0)
})
});
\ No newline at end of file
......@@ -28,6 +28,7 @@ export interface ReferenceCache<T, P, C> {
get: (ctx: C, props: P) => ReferenceItem<T>
clear: () => void
count: number
// values: Reference<T>[]
dispose: () => void
}
......@@ -52,14 +53,19 @@ export function createReferenceCache<T, P, C>(hashFn: (props: P) => string, ctor
console.warn('Reference usageCount below zero.')
}
deleteFn(ref.value)
map.delete(id)
}
})
},
get count () {
return map.size
},
// get values () {
// return Array.from(map.values())
// },
dispose: () => {
map.forEach(ref => deleteFn(ref.value))
map.clear()
},
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment