// ets_tracing: off import "../../../Operator/index.js" import type { Atomic } from "../../TRef/index.js" import { Versioned } from "../Versioned/index.js" export const EntryTypeId = Symbol() export type EntryTypeId = typeof EntryTypeId export class Entry { readonly _typeId: EntryTypeId = EntryTypeId constructor(readonly use: (f: (entry: EntryOps) => X) => X) {} } export function makeEntry(tref0: Atomic, isNew0: boolean): Entry { const versioned = tref0.versioned const ops = new EntryOps(tref0, versioned, versioned.value, isNew0, false) return new Entry((f) => f(ops)) } export const EntryOpsTypeId = Symbol() export type EntryOpsTypeId = typeof EntryOpsTypeId export class EntryOps { readonly _typeId: EntryOpsTypeId = EntryOpsTypeId readonly tref: Atomic readonly expected: Versioned newValue: S readonly isNew: boolean _isChanged: boolean constructor( tref: Atomic, expected: Versioned, newValue: S, isNew: boolean, isChanged: boolean ) { this.tref = tref this.expected = expected this.newValue = newValue this.isNew = isNew this._isChanged = isChanged } unsafeSet(value: unknown) { this._isChanged = true this.newValue = value as S } unsafeGet(): B { return this.newValue as unknown as B } commit() { this.tref.versioned = new Versioned(this.newValue) } copy(): Entry { const ops = new EntryOps( this.tref, this.expected, this.newValue, this.isNew, this.isChanged() ) return new Entry((f) => f(ops)) } isInvalid() { return !this.isValid() } isValid() { return this.tref.versioned === this.expected } isChanged() { return this._isChanged } toString() { return `Entry(expected.value = ${this.expected.value}, newValue = ${ this.newValue }, tref = ${this.tref}, isChanged = ${this.isChanged()})` } }