"use strict";
var mobx_1 = require("mobx");
function getOrCreateComputed(obj, key, options) {
var result;
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
var _a = options(), get = _a.get, set = _a.set;
obj[key] = result = mobx_1.computed(get, set);
}
else {
result = obj[key];
}
return result;
}
exports.getOrCreateComputed = getOrCreateComputed;
function isArray(obj) {
return Array.isArray(obj) || mobx_1.isObservableArray(obj);
}
exports.isArray = isArray;
function hasJsonProperty(obj) {
return obj && typeof obj === "object" && ("json" in obj);
}
exports.hasJsonProperty = hasJsonProperty;
function canLoadInto(obj) {
return hasJsonProperty(obj) || (obj && isArray(obj));
}
exports.canLoadInto = canLoadInto;
function save(obj) {
return hasJsonProperty(obj) ? obj.json : obj;
}
exports.save = save;
function load(obj, data) {
if (data === "undefined" || !obj) {
return;
}
if (!canLoadInto(obj)) {
throw new Error("Can only load JSON into an object with a json property, or an array");
}
if (hasJsonProperty(obj)) {
obj.json = data;
return;
}
Eif (isArray(obj)) {
// Plain array data, so just replace everything
if (isArray(data)) {
obj.splice.apply(obj, [0, obj.length].concat(data));
}
else {
obj.length = 0;
}
return;
}
}
exports.load = load;
//# sourceMappingURL=core.js.map |