@domgell/lazy-buffer-view
Version:
Provides lazily-initialized access to all the typed arrays on an ArrayBuffer
105 lines (89 loc) • 3.65 kB
text/typescript
import {assert} from "@domgell/ts-util";
/**
* Lazily-initialized access to typed arrays on an ArrayBuffer
*/
export interface LazyBufferView {
readonly arrayBuffer: ArrayBuffer;
readonly float32Array: Float32Array;
readonly uint32Array: Uint32Array;
readonly int32Array: Int32Array;
readonly uint16Array: Uint16Array;
readonly int16Array: Int16Array;
readonly uint8Array: Uint8Array;
readonly int8Array: Int8Array;
readonly uint8ClampedArray: Uint8ClampedArray;
readonly float64Array: Float64Array;
readonly bigInt64Array: BigInt64Array;
readonly bigUint64Array: BigUint64Array;
readonly float16Array: Float16Array;
}
/**
* Create a new LazyView with the given `arrayBuffer`
* @param arrayBuffer
* @constructor
*/
export function LazyBufferView(arrayBuffer: ArrayBuffer): LazyBufferView
/**
* Create a new LazyBufferView with an ArrayBuffer of the given `byteSize`
* @param byteSize
* @constructor
*/
export function LazyBufferView(byteSize: number): LazyBufferView
export function LazyBufferView(arg: ArrayBuffer | number): LazyBufferView {
const arrayBuffer = typeof arg === "number" ? new ArrayBuffer(arg) : arg;
return new LazyBufferViewImpl(arrayBuffer);
}
class LazyBufferViewImpl {
private readonly supportFloat16 = globalThis["Float16Array"] !== undefined;
constructor(readonly arrayBuffer: ArrayBuffer) {
}
private _float32Array: Float32Array | undefined;
get float32Array(): Float32Array {
return this._float32Array ??= new Float32Array(this.arrayBuffer);
}
private _uint32Array: Uint32Array | undefined;
get uint32Array(): Uint32Array {
return this._uint32Array ??= new Uint32Array(this.arrayBuffer);
}
private _int32Array: Int32Array | undefined;
get int32Array(): Int32Array {
return this._int32Array ??= new Int32Array(this.arrayBuffer);
}
private _uint16Array: Uint16Array | undefined;
get uint16Array(): Uint16Array {
return this._uint16Array ??= new Uint16Array(this.arrayBuffer);
}
private _int16Array: Int16Array | undefined;
get int16Array(): Int16Array {
return this._int16Array ??= new Int16Array(this.arrayBuffer);
}
private _uint8Array: Uint8Array | undefined;
get uint8Array(): Uint8Array {
return this._uint8Array ??= new Uint8Array(this.arrayBuffer);
}
private _int8Array: Int8Array | undefined;
get int8Array(): Int8Array {
return this._int8Array ??= new Int8Array(this.arrayBuffer);
}
private _uint8ClampedArray: Uint8ClampedArray | undefined;
get uint8ClampedArray(): Uint8ClampedArray {
return this._uint8ClampedArray ??= new Uint8ClampedArray(this.arrayBuffer);
}
private _float64Array: Float64Array | undefined;
get float64Array(): Float64Array {
return this._float64Array ??= new Float64Array(this.arrayBuffer);
}
private _bigInt64Array: BigInt64Array | undefined;
get bigInt64Array(): BigInt64Array {
return this._bigInt64Array ??= new BigInt64Array(this.arrayBuffer);
}
private _bigUint64Array: BigUint64Array | undefined;
get bigUint64Array(): BigUint64Array {
return this._bigUint64Array ??= new BigUint64Array(this.arrayBuffer);
}
private _float16Array: Float16Array | undefined;
get float16Array(): Float16Array {
assert(this.supportFloat16, "Float16Array is not supported in this browser");
return this._float16Array ??= new Float16Array(this.arrayBuffer);
}
}