diff --git a/src/mol-util/index.ts b/src/mol-util/index.ts index 4d540010c66a0f3d2033c4e39c81da8a205a3775..e6d8a2d3b3729a4d55cfe7eaed7200d1eea0aa5a 100644 --- a/src/mol-util/index.ts +++ b/src/mol-util/index.ts @@ -10,6 +10,7 @@ import StringBuilder from './string-builder' import UUID from './uuid' import Mask from './mask' +export * from './value-cell' export { BitFlags, StringBuilder, UUID, Mask } export function arrayEqual<T>(arr1: T[], arr2: T[]) { diff --git a/src/mol-util/value-cell.ts b/src/mol-util/value-cell.ts new file mode 100644 index 0000000000000000000000000000000000000000..762bcbf7e723970b2f755d6658cddc3efba52675 --- /dev/null +++ b/src/mol-util/value-cell.ts @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author David Sehnal <david.sehnal@gmail.com> + */ + +/** A mutable value cell. */ +interface ValueCell<T> { value: T } +/** Create a mutable value cell. */ +function ValueCell<T>(value: T): ValueCell<T> { return { value }; } + +/** An immutable value box that also holds a version of the attribute. */ +interface ValueBox<T> { readonly version: number, readonly value: T } +/** Create a new box with the specified value and version = 0 */ +function ValueBox<T>(value: T): ValueBox<T> +/** Create a new box by updating the value of an old box and incrementing the version number. */ +function ValueBox<T>(box: ValueBox<T>, value: T): ValueBox<T> +function ValueBox<T>(boxOrValue: T | ValueBox<T>, value?: T): ValueBox<T> { + if (arguments.length === 2) return { version: (boxOrValue as ValueBox<T>).version + 1, value: value! }; + return { version: 0, value: boxOrValue as T }; +} + +export { ValueCell, ValueBox }; +