diff --git a/src/mol-util/_spec/id-factory.spec.ts b/src/mol-util/_spec/id-factory.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c2c9cbe90fed42cb694869b08ed11af9d692283 --- /dev/null +++ b/src/mol-util/_spec/id-factory.spec.ts @@ -0,0 +1,27 @@ +/** + * 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 diff --git a/src/mol-util/_spec/reference-cache.spec.ts b/src/mol-util/_spec/reference-cache.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..544b642ecfb686d638c8f718f890f5271a552007 --- /dev/null +++ b/src/mol-util/_spec/reference-cache.spec.ts @@ -0,0 +1,40 @@ +/** + * 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 diff --git a/src/mol-util/reference-cache.ts b/src/mol-util/reference-cache.ts index a58346ca12ee5a38fc7110e4ba5b7c6c4f634733..7e9138a66896176d228be6636d3addc0a9e7726c 100644 --- a/src/mol-util/reference-cache.ts +++ b/src/mol-util/reference-cache.ts @@ -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