From 24eba5226fea224343fdd3eae54f6366b0a3b68b Mon Sep 17 00:00:00 2001
From: Alexander Rose <alex.rose@rcsb.org>
Date: Wed, 23 May 2018 10:47:34 -0700
Subject: [PATCH] use idFactory throughout lib

---
 src/mol-gl/scene.ts                   |  6 ++----
 src/mol-task/task.ts                  | 10 +++-------
 src/mol-util/_spec/id-factory.spec.ts |  8 ++++++++
 src/mol-util/id-factory.ts            |  9 +++++++--
 src/mol-util/value-cell.ts            |  9 ++++-----
 5 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/src/mol-gl/scene.ts b/src/mol-gl/scene.ts
index 5e7c91763..3649546ee 100644
--- a/src/mol-gl/scene.ts
+++ b/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> }
 
diff --git a/src/mol-task/task.ts b/src/mol-task/task.ts
index ee5474fa9..92203f50e 100644
--- a/src/mol-task/task.ts
+++ b/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 }
\ No newline at end of file
diff --git a/src/mol-util/_spec/id-factory.spec.ts b/src/mol-util/_spec/id-factory.spec.ts
index 4c2c9cbe9..a5ad68bdd 100644
--- a/src/mol-util/_spec/id-factory.spec.ts
+++ b/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)
+    })
 });
\ No newline at end of file
diff --git a/src/mol-util/id-factory.ts b/src/mol-util/id-factory.ts
index 0de5654ff..0c7ad893b 100644
--- a/src/mol-util/id-factory.ts
+++ b/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
+    }
 }
\ No newline at end of file
diff --git a/src/mol-util/value-cell.ts b/src/mol-util/value-cell.ts
index a77875634..948f5d283 100644
--- a/src/mol-util/value-cell.ts
+++ b/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,
-- 
GitLab