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

use idFactory throughout lib

parent 2799ef31
No related branches found
No related tags found
No related merge requests found
......@@ -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> }
......
......@@ -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
......@@ -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
......@@ -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
......@@ -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,
......
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