muschema
Version:
Schemas for mudb
169 lines • 5.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var util_1 = require("./util");
var MuDictionary = (function () {
function MuDictionary(valueSchema, id) {
this.muType = 'dictionary';
this.identity = id || {};
this.muData = valueSchema;
this.json = {
type: 'dictionary',
valueType: this.muData.json,
identity: JSON.stringify(this.identity),
};
}
MuDictionary.prototype.alloc = function () { return {}; };
MuDictionary.prototype.free = function (x) {
var valueSchema = this.muData;
var props = Object.keys(x);
for (var i = 0; i < props.length; ++i) {
valueSchema.free(x[props[i]]);
}
};
MuDictionary.prototype.equal = function (x, y) {
if (x !== Object(x) || y !== Object(y)) {
return false;
}
var xProps = Object.keys(x);
if (xProps.length !== Object.keys(y).length) {
return false;
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
for (var i = xProps.length - 1; i >= 0; --i) {
if (!hasOwnProperty.call(y, xProps[i])) {
return false;
}
}
for (var i = xProps.length - 1; i >= 0; --i) {
var prop = xProps[i];
if (!this.muData.equal(x[prop], y[prop])) {
return false;
}
}
return true;
};
MuDictionary.prototype.clone = function (x) {
var result = {};
var props = Object.keys(x);
var schema = this.muData;
for (var i = 0; i < props.length; ++i) {
result[props[i]] = schema.clone(x[props[i]]);
}
return result;
};
MuDictionary.prototype.copy = function (source, target) {
if (source === target) {
return;
}
var sourceProps = Object.keys(source);
var targetProps = Object.keys(target);
var hasOwnProperty = Object.prototype.hasOwnProperty;
for (var i = 0; i < targetProps.length; ++i) {
var prop = targetProps[i];
if (!hasOwnProperty.call(source, prop)) {
this.muData.free(target[prop]);
delete target[prop];
}
}
if (util_1.isMuPrimitive(this.muData.muType)) {
for (var i = 0; i < sourceProps.length; ++i) {
var prop = sourceProps[i];
target[prop] = source[prop];
}
return;
}
for (var i = 0; i < sourceProps.length; ++i) {
var prop = sourceProps[i];
if (!hasOwnProperty.call(target, prop)) {
target[prop] = this.muData.clone(source[prop]);
}
else {
this.muData.copy(source[prop], target[prop]);
}
}
};
MuDictionary.prototype.diff = function (base, target, stream) {
stream.grow(32);
var numRemove = 0;
var numPatch = 0;
var removeCounterOffset = stream.offset;
var patchCounterOffset = removeCounterOffset + 4;
stream.offset = removeCounterOffset + 8;
var baseProps = Object.keys(base);
for (var i = 0; i < baseProps.length; ++i) {
var prop = baseProps[i];
if (!(prop in target)) {
stream.grow(4 + 4 * prop.length);
stream.writeString(prop);
++numRemove;
}
}
var valueSchema = this.muData;
var targetProps = Object.keys(target);
for (var i = 0; i < targetProps.length; ++i) {
var prefixOffset = stream.offset;
var prop = targetProps[i];
stream.grow(4 + 4 * prop.length);
stream.writeString(prop);
if (prop in base) {
if (valueSchema.diff(base[prop], target[prop], stream)) {
++numPatch;
}
else {
stream.offset = prefixOffset;
}
}
else {
if (!valueSchema.diff(valueSchema.identity, target[prop], stream)) {
stream.buffer.uint8[prefixOffset + 3] |= 0x80;
}
++numPatch;
}
}
if (numPatch > 0 || numRemove > 0) {
stream.writeUint32At(removeCounterOffset, numRemove);
stream.writeUint32At(patchCounterOffset, numPatch);
return true;
}
else {
stream.offset = removeCounterOffset;
return false;
}
};
MuDictionary.prototype.patch = function (base, stream) {
var result = {};
var valueSchema = this.muData;
var numRemove = stream.readUint32();
var numPatch = stream.readUint32();
var propsToRemove = {};
for (var i = 0; i < numRemove; ++i) {
propsToRemove[stream.readString()] = true;
}
var props = Object.keys(base);
for (var i = 0; i < props.length; ++i) {
var prop = props[i];
if (propsToRemove[prop]) {
continue;
}
result[prop] = valueSchema.clone(base[prop]);
}
for (var i = 0; i < numPatch; ++i) {
var isIdentity = stream.buffer.uint8[stream.offset + 3] & 0x80;
stream.buffer.uint8[stream.offset + 3] &= ~0x80;
var prop = stream.readString();
if (prop in base) {
result[prop] = valueSchema.patch(base[prop], stream);
}
else if (isIdentity) {
result[prop] = valueSchema.clone(valueSchema.identity);
}
else {
result[prop] = valueSchema.patch(valueSchema.identity, stream);
}
}
return result;
};
return MuDictionary;
}());
exports.MuDictionary = MuDictionary;
//# sourceMappingURL=dictionary.js.map