UNPKG

@darlean/base

Version:

Base types and definitions for creating Darlean actors and suites

52 lines (51 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomPersistable = void 0; const utils_1 = require("@darlean/utils"); /** * Base class for creating custom persistables. */ class CustomPersistable extends utils_1.Changeable { constructor(value, dirty = true) { super(value, dirty); } getVersion() { return this._version; } async load(whenUndefined) { const result = await this._load(); this._version = result.version; if (result.value === undefined) { if (whenUndefined === 'clear') { this.setClear(); } } else { this.setValue(result.value); } this.markDirty(false); return result.value; } async persist(condition) { if (condition !== 'always' && !this.isDirty()) { return; } let version = this._version; if (version) { const next = parseInt(this._version || '0') + 1; version = next.toString().padStart(20, '0'); this._version = version; } else { const next = Date.now(); version = next.toString().padStart(20, '0'); this._version = version; } await this._persist(this.tryGetValue(), version); this.markDirty(false); } setVersion(version) { this._version = version; } } exports.CustomPersistable = CustomPersistable;