mute-structs
Version:
NodeJS module providing an implementation of the LogootSplit CRDT algorithm
60 lines (56 loc) • 2.2 kB
JavaScript
/*
This file is part of MUTE-structs.
Copyright (C) 2017 Matthieu Nicolas, Victorien Elvinger
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { isArrayFromMap, isObject } from "../data-validation";
import { RenamingMap } from "./renamingmap";
export class RenamingMapStore {
constructor() {
this.renamingMaps = new Map();
}
static fromPlain(o) {
if (isObject(o) &&
Array.isArray(o.renamingMaps)) {
const renamingMaps = o.renamingMaps
.filter(isArrayFromMap)
.map(([k, v]) => {
return [k, RenamingMap.fromPlain(v)];
})
.filter((arg) => typeof arg[0] === "string" && arg[1] !== null);
if (o.renamingMaps.length === renamingMaps.length) {
const renamingMapStore = new RenamingMapStore();
renamingMaps.forEach(([epochId, renamingMap]) => {
renamingMapStore.internalAdd(epochId, renamingMap);
});
return renamingMapStore;
}
}
return null;
}
add(epoch, renamingMap) {
this.internalAdd(epoch.id.asStr, renamingMap);
}
getRenamingMap(epochId) {
return this.renamingMaps.get(epochId.asStr);
}
toJSON() {
return { renamingMaps: Array.from(this.renamingMaps) };
}
purge() {
this.renamingMaps.clear();
}
internalAdd(epochId, renamingMap) {
this.renamingMaps.set(epochId, renamingMap);
}
}
//# sourceMappingURL=renamingmapstore.js.map