UNPKG

@x5e/gink

Version:

an eventually consistent database

64 lines 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BoundBundler = void 0; const utils_1 = require("./utils"); /** * This class is considered part of the internal interface of Gink and is not part of the API */ class BoundBundler { constructor(medallion, sealer, meta) { this.medallion = medallion; this.sealer = sealer; this.meta = meta; this.bundleInfo = undefined; this.changes = []; } async commit(comment) { this.requireNotSealed(); const meta = { ...this.meta }; if (comment) { meta.comment = comment; } this.bundleInfo = await this.sealer(this.changes, meta); return this.bundleInfo; } requireNotSealed() { if (this.bundleInfo) throw new Error("This Bundler has already been sealed."); } /** * * @param changeBuilder a protobuf Change ready to be serialized * @returns an Address who's offset is immediately available and whose medallion and * timestamp become defined when this Bundle is sealed. */ addChange(changeBuilder) { this.requireNotSealed(); this.changes.push(changeBuilder); const offset = this.changes.length; // Using an anonymous class here because I only need the interface of Address, // but I need some non-trivial behavior: // The timestamp is undefined until the associated bundle is finalized, then all the // components of the address become well-defined. return new (class { constructor(bundler, offset) { this.bundler = bundler; this.offset = offset; } get medallion() { return this.bundler.medallion; } get timestamp() { return this.bundler.bundleInfo?.timestamp; } [utils_1.inspectSymbol](depth, opts) { return this.toString(); } toString() { return `Muid {timestamp: ${this.timestamp}, medallion: ${this.medallion}, offset: ${this.offset}}`; } })(this, offset); } } exports.BoundBundler = BoundBundler; //# sourceMappingURL=BoundBundler.js.map