Bladeren bron

use idFactory throughout lib

Alexander Rose 6 jaren geleden
bovenliggende
commit
24eba5226f
5 gewijzigde bestanden met toevoegingen van 24 en 18 verwijderingen
  1. 2 4
      src/mol-gl/scene.ts
  2. 3 7
      src/mol-task/task.ts
  3. 8 0
      src/mol-util/_spec/id-factory.spec.ts
  4. 7 2
      src/mol-util/id-factory.ts
  5. 4 5
      src/mol-util/value-cell.ts

+ 2 - 4
src/mol-gl/scene.ts

@@ -8,11 +8,9 @@ import { PointRenderable, MeshRenderable, Renderable } from './renderable'
 
 import { ValueCell } from 'mol-util';
 import { Context } from './webgl/context';
+import { idFactory } from 'mol-util/id-factory';
 
-let _renderObjectId = 0;
-function getNextId() {
-    return _renderObjectId++ % 0x7FFFFFFF;
-}
+const getNextId = idFactory(0, 0x7FFFFFFF)
 
 export type RenderData = { [k: string]: ValueCell<Helpers.TypedArray> }
 

+ 3 - 7
src/mol-task/task.ts

@@ -8,6 +8,7 @@ import { RuntimeContext } from './execution/runtime-context'
 import { Progress } from './execution/progress'
 import { ExecuteObservable, ExecuteObservableChild, ExecuteInContext } from './execution/observable';
 import { SyncRuntimeContext } from 'mol-task/execution/synchronous';
+import { idFactory } from 'mol-util/id-factory';
 
 // A "named function wrapper" with built in "computation tree progress tracking".
 // Use Run(t, ?observer, ?updateRate) to execute
@@ -48,7 +49,7 @@ namespace Task {
         }
 
         constructor(public name: string, public f: (ctx: RuntimeContext) => Promise<T>, public onAbort?: () => void) {
-            this.id = nextId();
+            this.id = getNextId();
         }
     }
 
@@ -74,12 +75,7 @@ namespace Task {
         max: number
     }
 
-    let _id = 0;
-    function nextId() {
-        const ret = _id;
-        _id = (_id + 1) % 0x3fffffff;
-        return ret;
-    }
+    const getNextId = idFactory(0, 0x3fffffff)
 }
 
 export { Task }

+ 8 - 0
src/mol-util/_spec/id-factory.spec.ts

@@ -24,4 +24,12 @@ describe('id-factory', () => {
         expect(getNextId()).toBe(-1)
         expect(getNextId()).toBe(0)
     })
+
+    it('max-id', () => {
+        const getNextId = idFactory(0, 2)
+        expect(getNextId()).toBe(0)
+        expect(getNextId()).toBe(1)
+        expect(getNextId()).toBe(0)
+        expect(getNextId()).toBe(1)
+    })
 });

+ 7 - 2
src/mol-util/id-factory.ts

@@ -4,7 +4,12 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-export function idFactory(firstId = 0) {
+/** Builds id function returning ids within [firstId, maxId) */
+export function idFactory(firstId = 0, maxId = Number.MAX_SAFE_INTEGER) {
     let _nextId = firstId
-    return () => _nextId++
+    return () => {
+        const ret = _nextId
+        _nextId = (_nextId + 1) % maxId
+        return ret
+    }
 }

+ 4 - 5
src/mol-util/value-cell.ts

@@ -4,6 +4,8 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
+import { idFactory } from './id-factory'
+
 /** A mutable value reference. */
 interface ValueRef<T> { ref: T }
 
@@ -12,17 +14,14 @@ namespace ValueRef {
     export function set<T>(ref: ValueRef<T>, value: T) { ref.ref = value; return ref; }
 }
 
-let _valueBoxId = 0;
-function getNextId() {
-    return _valueBoxId++ % 0x7FFFFFFF;
-}
+const getNextId = idFactory(0, 0x7FFFFFFF)
 
 /**
  * An immutable value box that also holds a version of the attribute.
  * Optionally includes automatically propadated "metadata".
  */
 type ValueBox<T, D = never> = {
-    // Unique identifier in the range 0 to 0x7FFFFFFF
+    /** Unique identifier in the range 0 to 0x7FFFFFFF */
     readonly id: number,
     readonly version: number,
     readonly metadata: D,