bobtail-json-cell
Advanced tools
Comparing version
214
dist/main.js
@@ -95,42 +95,2 @@ (function (global, factory) { | ||
var _get = function get(object, property, receiver) { | ||
if (object === null) object = Function.prototype; | ||
var desc = Object.getOwnPropertyDescriptor(object, property); | ||
if (desc === undefined) { | ||
var parent = Object.getPrototypeOf(object); | ||
if (parent === null) { | ||
return undefined; | ||
} else { | ||
return get(parent, property, receiver); | ||
} | ||
} else if ("value" in desc) { | ||
return desc.value; | ||
} else { | ||
var getter = desc.get; | ||
if (getter === undefined) { | ||
return undefined; | ||
} | ||
return getter.call(receiver); | ||
} | ||
}; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
@@ -184,2 +144,17 @@ if (!(instance instanceof Constructor)) { | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _toArray(arr) { | ||
@@ -254,2 +229,50 @@ return Array.isArray(arr) ? arr : Array.from(arr); | ||
function _deleteProperty(getPath, basePath, obj, prop) { | ||
var _this = this; | ||
return rx.snap(function () { | ||
var path = getPath(prop); | ||
if (recorder.stack.length > 0) { | ||
// the default mutation warning is nowhere near dire enough. mutating nested objects within a | ||
// bind is extremely likely to lead to infinite loops. | ||
console.warn('Warning: deleting nested element at ' + path.join('.') + ' from within a bind context. Affected object:', obj); | ||
_this.onUnsafeMutation.pub({ op: 'delete', path: path, obj: obj, prop: prop, base: _this._base }); | ||
} | ||
recorder.mutating(function () { | ||
var old = obj[prop]; | ||
var diff = prefixDiff(basePath, [_defineProperty({}, prop, old), 0, 0]); | ||
if (prop in obj) { | ||
delete obj[prop]; | ||
_this.onChange.pub(diff); | ||
} | ||
}); | ||
return true; | ||
}); | ||
} | ||
function _setProperty(getPath, basePath, obj, prop, val) { | ||
var _this2 = this; | ||
return rx.snap(function () { | ||
var path = getPath(prop); | ||
if (recorder.stack.length > 0) { | ||
// the default mutation warning is nowhere near dire enough. mutating nested objects within a | ||
// bind is extremely likely to lead to infinite loops. | ||
console.warn('Warning: updating nested element at ' + path.join('.') + ' from within a bind context. Affected object:', obj); | ||
_this2.onUnsafeMutation.pub({ op: 'set', path: path, obj: obj, prop: prop, val: val, base: _this2._base }); | ||
} | ||
recorder.mutating(function () { | ||
var old = rx.snap(function () { | ||
return obj[prop]; | ||
}); | ||
var diff = jsondiffpatch.diff(_defineProperty({}, prop, old), _defineProperty({}, prop, val)); | ||
if (diff) { | ||
jsondiffpatch.patch(obj, diff); | ||
_this2.onChange.pub(prefixDiff(basePath, diff)); | ||
} | ||
}); | ||
return true; | ||
}); | ||
} | ||
var ObsJsonCell = exports.ObsJsonCell = function (_rx$ObsBase) { | ||
@@ -261,13 +284,13 @@ _inherits(ObsJsonCell, _rx$ObsBase); | ||
var _this = _possibleConstructorReturn(this, (ObsJsonCell.__proto__ || Object.getPrototypeOf(ObsJsonCell)).call(this)); | ||
var _this3 = _possibleConstructorReturn(this, (ObsJsonCell.__proto__ || Object.getPrototypeOf(ObsJsonCell)).call(this)); | ||
_this._base = _base; | ||
_this.onChange = _this._mkEv(function () { | ||
return jsondiffpatch.diff({}, _this._base); | ||
_this3._base = _base; | ||
_this3.onChange = _this3._mkEv(function () { | ||
return jsondiffpatch.diff({}, _this3._base); | ||
}); | ||
_this.onUnsafeMutation = _this._mkEv(function () {}); | ||
_this._updating(function () { | ||
return _this._data = new Proxy({ value: _this._base }, _this.conf([], null)); | ||
_this3.onUnsafeMutation = _this3._mkEv(function () {}); | ||
_this3._updating(function () { | ||
return _this3._data = new Proxy({ value: _this3._base }, _this3.conf([], null)); | ||
}); | ||
return _this; | ||
return _this3; | ||
} | ||
@@ -290,7 +313,7 @@ | ||
value: function _update(newVal) { | ||
var _this2 = this; | ||
var _this4 = this; | ||
this._updating(function () { | ||
var diff = jsondiffpatch.diff(_this2._data, { value: newVal }); | ||
jsondiffpatch.patch(_this2._data, diff); | ||
var diff = jsondiffpatch.diff(_this4._data, { value: newVal }); | ||
jsondiffpatch.patch(_this4._data, diff); | ||
}); | ||
@@ -307,6 +330,6 @@ return true; | ||
value: function readonly() { | ||
var _this3 = this; | ||
var _this5 = this; | ||
return new DepJsonCell(function () { | ||
return _this3.data; | ||
return _this5.data; | ||
}); | ||
@@ -317,6 +340,6 @@ } | ||
value: function mkDeleteProperty(getPath, basePath) { | ||
var _this4 = this; | ||
var _this6 = this; | ||
return function (obj, prop) { | ||
return _this4.deleteProperty(getPath, basePath, obj, prop); | ||
return _this6.deleteProperty(getPath, basePath, obj, prop); | ||
}; | ||
@@ -327,6 +350,6 @@ } | ||
value: function mkSetProperty(getPath, basePath) { | ||
var _this5 = this; | ||
var _this7 = this; | ||
return function (obj, prop, val) { | ||
return _this5.setProperty(getPath, basePath, obj, prop, val); | ||
return _this7.setProperty(getPath, basePath, obj, prop, val); | ||
}; | ||
@@ -337,22 +360,3 @@ } | ||
value: function deleteProperty(getPath, basePath, obj, prop) { | ||
var _this6 = this; | ||
return rx.snap(function () { | ||
var path = getPath(prop); | ||
if (recorder.stack.length > 0) { | ||
// the default mutation warning is nowhere near dire enough. mutating nested objects within a | ||
// bind is extremely likely to lead to infinite loops. | ||
console.warn('Warning: deleting nested element at ' + path.join('.') + ' from within a bind context. Affected object:', obj); | ||
_this6.onUnsafeMutation.pub({ op: 'delete', path: path, obj: obj, prop: prop, base: _this6._base }); | ||
} | ||
recorder.mutating(function () { | ||
var old = obj[prop]; | ||
var diff = prefixDiff(basePath, [_defineProperty({}, prop, old), 0, 0]); | ||
if (prop in obj) { | ||
delete obj[prop]; | ||
_this6.onChange.pub(diff); | ||
} | ||
}); | ||
return true; | ||
}); | ||
return _deleteProperty.call(this, getPath, basePath, obj, prop); | ||
} | ||
@@ -362,24 +366,21 @@ }, { | ||
value: function setProperty(getPath, basePath, obj, prop, val) { | ||
var _this7 = this; | ||
return _setProperty.call(this, getPath, basePath, obj, prop, val); | ||
} | ||
}, { | ||
key: '_makeReadOnly', | ||
value: function _makeReadOnly() { | ||
var oldSet = this.setProperty; | ||
this.setProperty = function (getPath, basePath, obj, prop, val) { | ||
if (!this._nowUpdating) { | ||
throw new DepMutationError("Cannot mutate DepJsonCell!"); | ||
} else return oldSet.call(this, getPath, basePath, obj, prop, val); | ||
}; | ||
return rx.snap(function () { | ||
var path = getPath(prop); | ||
if (recorder.stack.length > 0) { | ||
// the default mutation warning is nowhere near dire enough. mutating nested objects within a | ||
// bind is extremely likely to lead to infinite loops. | ||
console.warn('Warning: updating nested element at ' + path.join('.') + ' from within a bind context. Affected object:', obj); | ||
_this7.onUnsafeMutation.pub({ op: 'set', path: path, obj: obj, prop: prop, val: val, base: _this7._base }); | ||
} | ||
recorder.mutating(function () { | ||
var old = rx.snap(function () { | ||
return obj[prop]; | ||
}); | ||
var diff = jsondiffpatch.diff(_defineProperty({}, prop, old), _defineProperty({}, prop, val)); | ||
if (diff) { | ||
jsondiffpatch.patch(obj, diff); | ||
_this7.onChange.pub(prefixDiff(basePath, diff)); | ||
} | ||
}); | ||
return true; | ||
}); | ||
var oldDelete = this.deleteProperty; | ||
this.deleteProperty = function (getPath, basePath, obj, prop) { | ||
if (!this._nowUpdating) { | ||
throw new DepMutationError("Cannot mutate DepJsonCell!"); | ||
} else return oldDelete.call(this, getPath, basePath, obj, prop); | ||
}; | ||
return this; | ||
} | ||
@@ -516,21 +517,6 @@ }, { | ||
}); | ||
_this10._makeReadOnly(); | ||
return _this10; | ||
} | ||
_createClass(DepJsonCell, [{ | ||
key: 'setProperty', | ||
value: function setProperty(getPath, basePath, obj, prop, val) { | ||
if (!this._nowUpdating) { | ||
throw new DepMutationError("Cannot mutate DepJsonCell!"); | ||
} else return _get(DepJsonCell.prototype.__proto__ || Object.getPrototypeOf(DepJsonCell.prototype), 'setProperty', this).call(this, getPath, basePath, obj, prop, val); | ||
} | ||
}, { | ||
key: 'deleteProperty', | ||
value: function deleteProperty(getPath, basePath, obj, prop) { | ||
if (!this._nowUpdating) { | ||
throw new DepMutationError("Cannot mutate DepJsonCell!"); | ||
} else return _get(DepJsonCell.prototype.__proto__ || Object.getPrototypeOf(DepJsonCell.prototype), 'deleteProperty', this).call(this, getPath, basePath, obj, prop); | ||
} | ||
}]); | ||
return DepJsonCell; | ||
@@ -537,0 +523,0 @@ }(ObsJsonCell); |
@@ -1,1 +0,1 @@ | ||
!function(t,e){if("function"==typeof define&&define.amd)define("bobtail-json-cell",["exports","underscore","bobtail-rx","lodash.get","lodash.set","lodash.hasin","jsondiffpatch"],e);else if("undefined"!=typeof exports)e(exports,require("underscore"),require("bobtail-rx"),require("lodash.get"),require("lodash.set"),require("lodash.hasin"),require("jsondiffpatch"));else{var n={exports:{}};e(n.exports,t._,t.rx,t.lodashGet,t.lodashSet,t.lodashHasin,t.jsondiffpatch),t.bobtailJsonCell=n.exports}}(this,function(t,e,n,r,o,a,i){"use strict";function u(t){return t&&t.__esModule?t:{default:t}}function l(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function f(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function s(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function c(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function p(t){return Array.isArray(t)?t:Array.from(t)}Object.defineProperty(t,"__esModule",{value:!0}),t.update=t.jsonCell=t.SrcJsonCell=t.DepJsonCell=t.DepMutationError=t.ObsJsonCell=t.UPDATE=t.patchHas=void 0,t.logReturn=function(t){return console.info(t),t};var d=u(e),h=function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e.default=t,e}(n),y=u(r),v=u(o),b=u(a),_=u(i),g=function(){function t(t,e){var n=[],r=!0,o=!1,a=void 0;try{for(var i,u=t[Symbol.iterator]();!(r=(i=u.next()).done)&&(n.push(i.value),!e||n.length!==e);r=!0);}catch(t){o=!0,a=t}finally{try{!r&&u.return&&u.return()}finally{if(o)throw a}}return n}return function(e,n){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),w=function t(e,n,r){null===e&&(e=Function.prototype);var o=Object.getOwnPropertyDescriptor(e,n);if(void 0===o){var a=Object.getPrototypeOf(e);return null===a?void 0:t(a,n,r)}if("value"in o)return o.value;var i=o.get;if(void 0!==i)return i.call(r)},O=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),j=_.default.create({cloneDiffValues:!0}),P=h._recorder,m=t.patchHas=function t(e,n){var r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(d.default.isString(n))return t(e,n.split("."));if(!n.length)return!0;d.default.isArray(e)&&!r&&(r=!0,e=e[0]);var o=p(n),a=o[0],i=o.slice(1);if(d.default.isObject(e)){if(!r&&"a"===e._t){var u=""+a;return u in e?t(e[u],i,r):a in e&&t(e[a],i,r)}return a in e&&(1===n.length||t(e[a],i,r))}return!1},k=function(t,e){if(!t.length)return e;var n={};return(0,v.default)(n,t,e),n},C=t.UPDATE=Symbol("update"),x=t.ObsJsonCell=function(t){function e(t){f(this,e);var n=s(this,(e.__proto__||Object.getPrototypeOf(e)).call(this));return n._base=t,n.onChange=n._mkEv(function(){return j.diff({},n._base)}),n.onUnsafeMutation=n._mkEv(function(){}),n._updating(function(){return n._data=new Proxy({value:n._base},n.conf([],null))}),n}return c(e,h.ObsBase),O(e,[{key:"_updating",value:function(t){this._oldUpdating=this._nowUpdating||!1,this._nowUpdating=!0;try{h.snap(t)}finally{this._nowUpdating=this._oldUpdating}return!0}},{key:"_update",value:function(t){var e=this;return this._updating(function(){var n=j.diff(e._data,{value:t});j.patch(e._data,n)}),!0}},{key:"all",value:function(){return this.data}},{key:"readonly",value:function(){var t=this;return new S(function(){return t.data})}},{key:"mkDeleteProperty",value:function(t,e){var n=this;return function(r,o){return n.deleteProperty(t,e,r,o)}}},{key:"mkSetProperty",value:function(t,e){var n=this;return function(r,o,a){return n.setProperty(t,e,r,o,a)}}},{key:"deleteProperty",value:function(t,e,n,r){var o=this;return h.snap(function(){var a=t(r);return P.stack.length>0&&(console.warn("Warning: deleting nested element at "+a.join(".")+" from within a bind context. Affected object:",n),o.onUnsafeMutation.pub({op:"delete",path:a,obj:n,prop:r,base:o._base})),P.mutating(function(){var t=n[r],a=k(e,[l({},r,t),0,0]);r in n&&(delete n[r],o.onChange.pub(a))}),!0})}},{key:"setProperty",value:function(t,e,n,r,o){var a=this;return h.snap(function(){var i=t(r);return P.stack.length>0&&(console.warn("Warning: updating nested element at "+i.join(".")+" from within a bind context. Affected object:",n),a.onUnsafeMutation.pub({op:"set",path:i,obj:n,prop:r,val:o,base:a._base})),P.mutating(function(){var t=h.snap(function(){return n[r]}),i=j.diff(l({},r,t),l({},r,o));i&&(j.patch(n,i),a.onChange.pub(k(e,i)))}),!0})}},{key:"conf",value:function(t){var e=this,n=function(){for(var e=arguments.length,n=Array(e),r=0;r<e;r++)n[r]=arguments[r];return t.concat(n)};return{deleteProperty:this.mkDeleteProperty(n,t),set:this.mkSetProperty(n,t),get:function(t,r){var o=t[r];if("__proto__"===r||d.default.isFunction(o))return o;if(r===C||"updateRxb"===r&&!("updateRxb"in t))return function(e){return j.patch(t,j.diff(t,e))};var a=n(r);if("length"===r&&d.default.isArray(t)){var i=t.length;P.sub(e.onChange,function(){var t=(0,y.default)(e._base,a.slice(1));return t!==i&&(i=t,!0)})}else P.sub(e.onChange,function(t){return m(t,a)});return d.default.isObject(o)?new Proxy(o,e.conf(n(r),t)):o},has:function(t,r){var o=n(r).slice(1),a=r in t;return P.sub(e.onChange,function(t){var n=(0,b.default)(e._base,o);return a!==n&&(a=n,!0)}),a},ownKeys:function(n){return P.sub(e.onChange,function(e){var n=(0,y.default)(e,t);return!!n&&(!!d.default.isArray(n&&2!==n.length)||"a"===n._t&&d.default.chain(n).omit("_t").values().value().some())}),Reflect.ownKeys(n)}}}},{key:"data",get:function(){return this._data.value}}]),e}(),A=t.DepMutationError=function(t){function e(){var t;f(this,e);for(var n=arguments.length,r=Array(n),o=0;o<n;o++)r[o]=arguments[o];var a=s(this,(t=e.__proto__||Object.getPrototypeOf(e)).call.apply(t,[this].concat(r)));return Error.captureStackTrace(a,e),a}return c(e,Error),e}(),S=t.DepJsonCell=function(t){function e(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};f(this,e);var r=s(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,n));r.f=t;var o=h.bind(r.f);return h.autoSub(o.onSet,function(t){var e=g(t,2),n=(e[0],e[1]);return r._update(n)}),r}return c(e,x),O(e,[{key:"setProperty",value:function(t,n,r,o,a){if(this._nowUpdating)return w(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"setProperty",this).call(this,t,n,r,o,a);throw new A("Cannot mutate DepJsonCell!")}},{key:"deleteProperty",value:function(t,n,r,o){if(this._nowUpdating)return w(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"deleteProperty",this).call(this,t,n,r,o);throw new A("Cannot mutate DepJsonCell!")}}]),e}(),D=t.SrcJsonCell=function(t){function e(t){return f(this,e),s(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t))}return c(e,x),O(e,[{key:"update",value:function(t){return this._update(t)}},{key:"data",set:function(t){this._update(t)},get:function(){return this._data.value}}]),e}();t.jsonCell=function(t){return new D(t).data},t.update=function(t,e){return h.snap(function(){var n=j.diff(t,e);return j.patch(t,n),!0})}}); | ||
!function(t,e){if("function"==typeof define&&define.amd)define("bobtail-json-cell",["exports","underscore","bobtail-rx","lodash.get","lodash.set","lodash.hasin","jsondiffpatch"],e);else if("undefined"!=typeof exports)e(exports,require("underscore"),require("bobtail-rx"),require("lodash.get"),require("lodash.set"),require("lodash.hasin"),require("jsondiffpatch"));else{var n={exports:{}};e(n.exports,t._,t.rx,t.lodashGet,t.lodashSet,t.lodashHasin,t.jsondiffpatch),t.bobtailJsonCell=n.exports}}(this,function(t,e,n,r,o,a,i){"use strict";function u(t){return t&&t.__esModule?t:{default:t}}function l(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function f(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function s(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function c(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function p(t){return Array.isArray(t)?t:Array.from(t)}function d(t,e,n,r){var o=this;return v.snap(function(){var a=t(r);return O.stack.length>0&&(console.warn("Warning: deleting nested element at "+a.join(".")+" from within a bind context. Affected object:",n),o.onUnsafeMutation.pub({op:"delete",path:a,obj:n,prop:r,base:o._base})),O.mutating(function(){var t=n[r],a=C(e,[c({},r,t),0,0]);r in n&&(delete n[r],o.onChange.pub(a))}),!0})}function h(t,e,n,r,o){var a=this;return v.snap(function(){var i=t(r);return O.stack.length>0&&(console.warn("Warning: updating nested element at "+i.join(".")+" from within a bind context. Affected object:",n),a.onUnsafeMutation.pub({op:"set",path:i,obj:n,prop:r,val:o,base:a._base})),O.mutating(function(){var t=v.snap(function(){return n[r]}),i=P.diff(c({},r,t),c({},r,o));i&&(P.patch(n,i),a.onChange.pub(C(e,i)))}),!0})}Object.defineProperty(t,"__esModule",{value:!0}),t.update=t.jsonCell=t.SrcJsonCell=t.DepJsonCell=t.DepMutationError=t.ObsJsonCell=t.UPDATE=t.patchHas=void 0,t.logReturn=function(t){return console.info(t),t};var y=u(e),v=function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e.default=t,e}(n),b=u(r),_=u(o),g=u(a),w=u(i),m=function(){function t(t,e){var n=[],r=!0,o=!1,a=void 0;try{for(var i,u=t[Symbol.iterator]();!(r=(i=u.next()).done)&&(n.push(i.value),!e||n.length!==e);r=!0);}catch(t){o=!0,a=t}finally{try{!r&&u.return&&u.return()}finally{if(o)throw a}}return n}return function(e,n){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),j=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),P=w.default.create({cloneDiffValues:!0}),O=v._recorder,k=t.patchHas=function t(e,n){var r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(y.default.isString(n))return t(e,n.split("."));if(!n.length)return!0;y.default.isArray(e)&&!r&&(r=!0,e=e[0]);var o=p(n),a=o[0],i=o.slice(1);if(y.default.isObject(e)){if(!r&&"a"===e._t){var u=""+a;return u in e?t(e[u],i,r):a in e&&t(e[a],i,r)}return a in e&&(1===n.length||t(e[a],i,r))}return!1},C=function(t,e){if(!t.length)return e;var n={};return(0,_.default)(n,t,e),n},x=t.UPDATE=Symbol("update"),A=t.ObsJsonCell=function(t){function e(t){l(this,e);var n=f(this,(e.__proto__||Object.getPrototypeOf(e)).call(this));return n._base=t,n.onChange=n._mkEv(function(){return P.diff({},n._base)}),n.onUnsafeMutation=n._mkEv(function(){}),n._updating(function(){return n._data=new Proxy({value:n._base},n.conf([],null))}),n}return s(e,v.ObsBase),j(e,[{key:"_updating",value:function(t){this._oldUpdating=this._nowUpdating||!1,this._nowUpdating=!0;try{v.snap(t)}finally{this._nowUpdating=this._oldUpdating}return!0}},{key:"_update",value:function(t){var e=this;return this._updating(function(){var n=P.diff(e._data,{value:t});P.patch(e._data,n)}),!0}},{key:"all",value:function(){return this.data}},{key:"readonly",value:function(){var t=this;return new E(function(){return t.data})}},{key:"mkDeleteProperty",value:function(t,e){var n=this;return function(r,o){return n.deleteProperty(t,e,r,o)}}},{key:"mkSetProperty",value:function(t,e){var n=this;return function(r,o,a){return n.setProperty(t,e,r,o,a)}}},{key:"deleteProperty",value:function(t,e,n,r){return d.call(this,t,e,n,r)}},{key:"setProperty",value:function(t,e,n,r,o){return h.call(this,t,e,n,r,o)}},{key:"_makeReadOnly",value:function(){var t=this.setProperty;this.setProperty=function(e,n,r,o,a){if(this._nowUpdating)return t.call(this,e,n,r,o,a);throw new S("Cannot mutate DepJsonCell!")};var e=this.deleteProperty;return this.deleteProperty=function(t,n,r,o){if(this._nowUpdating)return e.call(this,t,n,r,o);throw new S("Cannot mutate DepJsonCell!")},this}},{key:"conf",value:function(t){var e=this,n=function(){for(var e=arguments.length,n=Array(e),r=0;r<e;r++)n[r]=arguments[r];return t.concat(n)};return{deleteProperty:this.mkDeleteProperty(n,t),set:this.mkSetProperty(n,t),get:function(t,r){var o=t[r];if("__proto__"===r||y.default.isFunction(o))return o;if(r===x||"updateRxb"===r&&!("updateRxb"in t))return function(e){return P.patch(t,P.diff(t,e))};var a=n(r);if("length"===r&&y.default.isArray(t)){var i=t.length;O.sub(e.onChange,function(){var t=(0,b.default)(e._base,a.slice(1));return t!==i&&(i=t,!0)})}else O.sub(e.onChange,function(t){return k(t,a)});return y.default.isObject(o)?new Proxy(o,e.conf(n(r),t)):o},has:function(t,r){var o=n(r).slice(1),a=r in t;return O.sub(e.onChange,function(t){var n=(0,g.default)(e._base,o);return a!==n&&(a=n,!0)}),a},ownKeys:function(n){return O.sub(e.onChange,function(e){var n=(0,b.default)(e,t);return!!n&&(!!y.default.isArray(n&&2!==n.length)||"a"===n._t&&y.default.chain(n).omit("_t").values().value().some())}),Reflect.ownKeys(n)}}}},{key:"data",get:function(){return this._data.value}}]),e}(),S=t.DepMutationError=function(t){function e(){var t;l(this,e);for(var n=arguments.length,r=Array(n),o=0;o<n;o++)r[o]=arguments[o];var a=f(this,(t=e.__proto__||Object.getPrototypeOf(e)).call.apply(t,[this].concat(r)));return Error.captureStackTrace(a,e),a}return s(e,Error),e}(),E=t.DepJsonCell=function(t){function e(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};l(this,e);var r=f(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,n));r.f=t;var o=v.bind(r.f);return v.autoSub(o.onSet,function(t){var e=m(t,2),n=(e[0],e[1]);return r._update(n)}),r._makeReadOnly(),r}return s(e,A),e}(),U=t.SrcJsonCell=function(t){function e(t){return l(this,e),f(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t))}return s(e,A),j(e,[{key:"update",value:function(t){return this._update(t)}},{key:"data",set:function(t){this._update(t)},get:function(){return this._data.value}}]),e}();t.jsonCell=function(t){return new U(t).data},t.update=function(t,e){return v.snap(function(){var n=P.diff(t,e);return P.patch(t,n),!0})}}); |
{ | ||
"name": "bobtail-json-cell", | ||
"version": "0.2.2", | ||
"version": "0.2.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/main.js", |
@@ -151,3 +151,3 @@ import nn from 'nevernull'; | ||
expect(() => x.data.a = 90).toThrowError("Cannot mutate DepJsonCell!"); | ||
}) | ||
}); | ||
}); | ||
@@ -154,0 +154,0 @@ |
122
src/main.js
@@ -66,2 +66,48 @@ import _ from 'underscore'; | ||
function deleteProperty (getPath, basePath, obj, prop) { | ||
return rx.snap(() => { | ||
let path = getPath(prop); | ||
if (recorder.stack.length > 0) { | ||
// the default mutation warning is nowhere near dire enough. mutating nested objects within a | ||
// bind is extremely likely to lead to infinite loops. | ||
console.warn( | ||
`Warning: deleting nested element at ${path.join('.')} from within a bind context. Affected object:`, obj | ||
); | ||
this.onUnsafeMutation.pub({op: 'delete', path, obj, prop, base: this._base}) | ||
} | ||
recorder.mutating(() => { | ||
let old = obj[prop]; | ||
let diff = prefixDiff(basePath, [{[prop]: old}, 0, 0]); | ||
if(prop in obj) { | ||
delete obj[prop]; | ||
this.onChange.pub(diff); | ||
} | ||
}); | ||
return true; | ||
}) | ||
} | ||
function setProperty (getPath, basePath, obj, prop, val) { | ||
return rx.snap(() => { | ||
let path = getPath(prop); | ||
if (recorder.stack.length > 0) { | ||
// the default mutation warning is nowhere near dire enough. mutating nested objects within a | ||
// bind is extremely likely to lead to infinite loops. | ||
console.warn( | ||
`Warning: updating nested element at ${path.join('.')} from within a bind context. Affected object:`, obj | ||
); | ||
this.onUnsafeMutation.pub({op: 'set', path, obj, prop, val, base: this._base}); | ||
} | ||
recorder.mutating(() => { | ||
let old = rx.snap(() => obj[prop]); | ||
let diff = jsondiffpatch.diff({[prop]: old}, {[prop]: val}); | ||
if (diff) { | ||
jsondiffpatch.patch(obj, diff); | ||
this.onChange.pub(prefixDiff(basePath, diff)); | ||
} | ||
}); | ||
return true; | ||
}) | ||
} | ||
export class ObsJsonCell extends rx.ObsBase { | ||
@@ -106,45 +152,26 @@ constructor(_base) { | ||
deleteProperty (getPath, basePath, obj, prop) { | ||
return rx.snap(() => { | ||
let path = getPath(prop); | ||
if (recorder.stack.length > 0) { | ||
// the default mutation warning is nowhere near dire enough. mutating nested objects within a | ||
// bind is extremely likely to lead to infinite loops. | ||
console.warn( | ||
`Warning: deleting nested element at ${path.join('.')} from within a bind context. Affected object:`, obj | ||
); | ||
this.onUnsafeMutation.pub({op: 'delete', path, obj, prop, base: this._base}) | ||
} | ||
recorder.mutating(() => { | ||
let old = obj[prop]; | ||
let diff = prefixDiff(basePath, [{[prop]: old}, 0, 0]); | ||
if(prop in obj) { | ||
delete obj[prop]; | ||
this.onChange.pub(diff); | ||
} | ||
}); | ||
return true; | ||
}) | ||
return deleteProperty.call(this, getPath, basePath, obj, prop); | ||
} | ||
setProperty (getPath, basePath, obj, prop, val) { | ||
return rx.snap(() => { | ||
let path = getPath(prop); | ||
if (recorder.stack.length > 0) { | ||
// the default mutation warning is nowhere near dire enough. mutating nested objects within a | ||
// bind is extremely likely to lead to infinite loops. | ||
console.warn( | ||
`Warning: updating nested element at ${path.join('.')} from within a bind context. Affected object:`, obj | ||
); | ||
this.onUnsafeMutation.pub({op: 'set', path, obj, prop, val, base: this._base}); | ||
return setProperty.call(this, getPath, basePath, obj, prop, val); | ||
} | ||
_makeReadOnly() { | ||
let oldSet = this.setProperty; | ||
this.setProperty = function(getPath, basePath, obj, prop, val) { | ||
if (!this._nowUpdating) { | ||
throw new DepMutationError("Cannot mutate DepJsonCell!"); | ||
} | ||
recorder.mutating(() => { | ||
let old = rx.snap(() => obj[prop]); | ||
let diff = jsondiffpatch.diff({[prop]: old}, {[prop]: val}); | ||
if (diff) { | ||
jsondiffpatch.patch(obj, diff); | ||
this.onChange.pub(prefixDiff(basePath, diff)); | ||
} | ||
}); | ||
return true; | ||
}) | ||
else return oldSet.call(this, getPath, basePath, obj, prop, val); | ||
}; | ||
let oldDelete = this.deleteProperty; | ||
this.deleteProperty = function (getPath, basePath, obj, prop) { | ||
if (!this._nowUpdating) { | ||
throw new DepMutationError("Cannot mutate DepJsonCell!"); | ||
} | ||
else return oldDelete.call(this, getPath, basePath, obj, prop); | ||
}; | ||
return this; | ||
} | ||
@@ -235,18 +262,5 @@ | ||
let c = rx.bind(this.f); | ||
rx.autoSub(c.onSet, ([o, n]) => this._update(n)) | ||
rx.autoSub(c.onSet, ([o, n]) => this._update(n)); | ||
this._makeReadOnly(); | ||
} | ||
setProperty(getPath, basePath, obj, prop, val) { | ||
if (!this._nowUpdating) { | ||
throw new DepMutationError("Cannot mutate DepJsonCell!"); | ||
} | ||
else return super.setProperty(getPath, basePath, obj, prop, val); | ||
} | ||
deleteProperty(getPath, basePath, obj, prop) { | ||
if (!this._nowUpdating) { | ||
throw new DepMutationError("Cannot mutate DepJsonCell!"); | ||
} | ||
else return super.deleteProperty(getPath, basePath, obj, prop); | ||
} | ||
} | ||
@@ -253,0 +267,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
188315
153.64%17
6.25%4025
347.72%