@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
39 lines • 1.25 kB
JavaScript
;
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.LWWRegister = void 0;
const time_1 = require("../util/time");
/** a CRDT implementation. Uses timestamps to resolve conflicts when updating the value (last write wins)
* mostly based on https://jakelazaroff.com/words/an-interactive-intro-to-crdts/
*
* @param T the type of the value stored in the register
*/
class LWWRegister {
state;
constructor(state) {
this.state = state;
}
get value() {
return this.state.value;
}
get timestamp() {
return this.state.timestamp;
}
set(value) {
this.state = { timestamp: (0, time_1.nowBigInt)(), value };
}
merge(incoming) {
// only update if the incoming timestamp is greater than the local timestamp
if (incoming.timestamp > this.state.timestamp) {
this.state = incoming;
}
return this.state;
}
}
exports.LWWRegister = LWWRegister;
//# sourceMappingURL=lwwRegister.js.map