UNPKG

@chainsafe/ssz

Version:

Simple Serialize

79 lines 2.32 kB
import { getHashComputations } from "@chainsafe/persistent-merkle-tree"; import { TreeViewDU } from "./abstract.js"; /** * Thin wrapper around BitArray to upstream changes after `this.commit()` */ export class BitArrayTreeViewDU extends TreeViewDU { type; _rootNode; /** Cached BitArray instance computed only on demand */ _bitArray = null; constructor(type, _rootNode) { super(); this.type = type; this._rootNode = _rootNode; } get node() { return this._rootNode; } get cache() { // biome-ignore lint/suspicious/useGetterReturn: There is no cache to return return; } // Wrapped API from BitArray /** @see BitArray.uint8Array */ get uint8Array() { return this.bitArray.uint8Array; } /** @see BitArray.bitLen */ get bitLen() { return this.bitArray.bitLen; } /** Lazily computed bitArray instance */ get bitArray() { if (this._bitArray === null) { this._bitArray = this.type.tree_toValue(this._rootNode); } return this._bitArray; } commit(hcOffset = 0, hcByLevel = null) { if (this._bitArray !== null) { this._rootNode = this.type.value_toTree(this._bitArray); } if (hcByLevel !== null && this._rootNode.h0 === null) { getHashComputations(this._rootNode, hcOffset, hcByLevel); } } /** @see BitArray.get */ get(bitIndex) { return this.bitArray.get(bitIndex); } /** @see BitArray.set */ set(bitIndex, bit) { this.bitArray.set(bitIndex, bit); } /** @see BitArray.mergeOrWith */ mergeOrWith(bitArray2) { this.bitArray.mergeOrWith(bitArray2); } /** @see BitArray.intersectValues */ intersectValues(values) { return this.bitArray.intersectValues(values); } /** @see BitArray.getTrueBitIndexes */ getTrueBitIndexes() { return this.bitArray.getTrueBitIndexes(); } /** @see BitArray.getSingleTrueBit */ getSingleTrueBit() { return this.bitArray.getSingleTrueBit(); } /** @see BitArray.toBoolArray */ toBoolArray() { return this.bitArray.toBoolArray(); } clearCache() { this._bitArray = null; } } //# sourceMappingURL=bitArray.js.map