Ver Fonte

fix ref-cache deleting, added spec

Alexander Rose há 6 anos atrás
pai
commit
ea6cd311ee

+ 27 - 0
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)
+    })
+});

+ 40 - 0
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)
+    })
+});

+ 6 - 0
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()
         },
     }
 }