diff --git a/core/index.js b/core/index.js index 996d831..ccf315a 100644 --- a/core/index.js +++ b/core/index.js @@ -1,6 +1,8 @@ const views = Object.freeze({ f32: Float32Array, u32: Uint32Array, i32: Int32Array }); export class SharedRows { + #view; + constructor(buffer, descriptor) { if (!(buffer instanceof SharedArrayBuffer)) throw new TypeError("ROWS_DESCRIPTOR"); this.buffer = buffer; @@ -11,6 +13,8 @@ export class SharedRows { if (!views[descriptor.format] || descriptor.name !== (this.descriptor?.name ?? descriptor.name)) throw new TypeError("ROWS_DESCRIPTOR"); this.descriptor = Object.freeze(descriptor); + const View = views[descriptor.format]; + this.#view = new View(this.buffer, descriptor.offset, descriptor.bytes / View.BYTES_PER_ELEMENT); return this; } @@ -18,10 +22,8 @@ export class SharedRows { get rows() { return this.descriptor.rows; } get stride() { return this.descriptor.stride; } get format() { return this.descriptor.format; } - get view() { - const View = views[this.format]; - return new View(this.buffer, this.descriptor.offset, this.descriptor.bytes / View.BYTES_PER_ELEMENT); - } + /** One typed view over the whole array; replaced only when the descriptor changes. */ + get view() { return this.#view; } row(index) { if (!Number.isInteger(index) || index < 0 || index >= this.rows) throw new RangeError("ROW_RANGE");