@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
48 lines • 1.62 kB
JavaScript
;
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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