bobtail-json-cell
Advanced tools
Comparing version
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define('bobtail-json-cell', ['exports', 'underscore', 'bobtail-rx', 'lodash.get', 'lodash.set', 'lodash.hasin', 'jsondiffpatch'], factory); | ||
define('bobtail-json-cell', ['exports', 'underscore', 'bobtail-rx', 'lodash.get', 'lodash.set', 'lodash.hasin', 'lodash.clonedeep', 'jsondiffpatch'], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports, require('underscore'), require('bobtail-rx'), require('lodash.get'), require('lodash.set'), require('lodash.hasin'), require('jsondiffpatch')); | ||
factory(exports, require('underscore'), require('bobtail-rx'), require('lodash.get'), require('lodash.set'), require('lodash.hasin'), require('lodash.clonedeep'), require('jsondiffpatch')); | ||
} else { | ||
@@ -10,6 +10,6 @@ var mod = { | ||
}; | ||
factory(mod.exports, global._, global.rx, global.lodashGet, global.lodashSet, global.lodashHasin, global.jsondiffpatch); | ||
factory(mod.exports, global._, global.rx, global.lodashGet, global.lodashSet, global.lodashHasin, global.lodashClonedeep, global.jsondiffpatch); | ||
global.bobtailJsonCell = mod.exports; | ||
} | ||
})(this, function (exports, _underscore, _bobtailRx, _lodash, _lodash3, _lodash5, _jsondiffpatch) { | ||
})(this, function (exports, _underscore, _bobtailRx, _lodash, _lodash3, _lodash5, _lodash7, _jsondiffpatch) { | ||
'use strict'; | ||
@@ -20,3 +20,3 @@ | ||
}); | ||
exports.update = exports.jsonCell = exports.SrcJsonCell = exports.DepJsonCell = exports.DepMutationError = exports.ObsJsonCell = exports.UPDATE = exports.patchHas = undefined; | ||
exports.update = exports.jsonCell = exports.SrcJsonCell = exports.DepJsonCell = exports.DepMutationError = exports.ObsJsonCell = exports.UPDATE = exports.patchHas = exports.IS_PROXY_SYM = undefined; | ||
exports.logReturn = logReturn; | ||
@@ -34,2 +34,4 @@ | ||
var _lodash8 = _interopRequireDefault(_lodash7); | ||
var _jsondiffpatch2 = _interopRequireDefault(_jsondiffpatch); | ||
@@ -171,2 +173,3 @@ | ||
var recorder = rx._recorder; | ||
var IS_PROXY_SYM = exports.IS_PROXY_SYM = Symbol('is_proxy'); | ||
@@ -240,3 +243,5 @@ var patchHas = exports.patchHas = function patchHas(patch, path) { | ||
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 }); | ||
_this.onUnsafeMutation.pub({ op: 'delete', path: path, obj: obj, prop: prop, base: rx.snap(function () { | ||
return _this.data; | ||
}) }); | ||
} | ||
@@ -264,3 +269,5 @@ recorder.mutating(function () { | ||
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 }); | ||
_this2.onUnsafeMutation.pub({ op: 'set', path: path, obj: obj, prop: prop, val: val, base: rx.snap(function () { | ||
return _this2.data; | ||
}) }); | ||
} | ||
@@ -283,2 +290,5 @@ recorder.mutating(function () { | ||
var val = obj[prop]; | ||
if (prop === 'IS_PROXY_SYM') { | ||
return true; | ||
} | ||
if (prop === '__proto__' || _underscore2.default.isFunction(obj[prop])) { | ||
@@ -290,3 +300,3 @@ return obj[prop]; | ||
if (_underscore2.default.isObject(val)) { | ||
return new Proxy(val, this.conf(getPath(prop), obj)); | ||
return new Proxy(val, this.conf(getPath(prop))); | ||
} | ||
@@ -304,10 +314,7 @@ return val; | ||
_this3._base = _base; | ||
_this3.onChange = _this3._mkEv(function () { | ||
return jsondiffpatch.diff({}, _this3._base); | ||
return jsondiffpatch.diff({}, _base); | ||
}); | ||
_this3.onUnsafeMutation = _this3._mkEv(function () {}); | ||
_this3._updating(function () { | ||
return _this3._data = new Proxy({ value: _this3._base }, _this3.conf([], null)); | ||
}); | ||
_this3._base = { value: _base }; | ||
return _this3; | ||
@@ -317,5 +324,10 @@ } | ||
_createClass(ObsJsonCell, [{ | ||
key: '_proxify', | ||
value: function _proxify() { | ||
return new Proxy(this._base, this.conf([])); | ||
} | ||
}, { | ||
key: '_updating', | ||
value: function _updating(f) { | ||
this._oldUpdating = this._nowUpdating || false; | ||
var _oldUpdating = this._nowUpdating || false; | ||
this._nowUpdating = true; | ||
@@ -325,3 +337,3 @@ try { | ||
} finally { | ||
this._nowUpdating = this._oldUpdating; | ||
this._nowUpdating = _oldUpdating; | ||
} | ||
@@ -336,4 +348,4 @@ return true; | ||
this._updating(function () { | ||
var diff = jsondiffpatch.diff(_this4._data, { value: newVal }); | ||
jsondiffpatch.patch(_this4._data, diff); | ||
var diff = jsondiffpatch.diff(_this4._base, { value: newVal }); | ||
jsondiffpatch.patch(_this4._proxify(), diff); | ||
}); | ||
@@ -348,2 +360,7 @@ return true; | ||
}, { | ||
key: 'cloneRaw', | ||
value: function cloneRaw() { | ||
return (0, _lodash8.default)(this._base.value); | ||
} | ||
}, { | ||
key: 'readonly', | ||
@@ -408,3 +425,5 @@ value: function readonly() { | ||
recorder.sub(this.onChange, function () { | ||
var newVal = (0, _lodash2.default)(_this9._base, path.slice(1)); // necessary because of wrapping in value field | ||
var newVal = rx.snap(function () { | ||
return (0, _lodash2.default)(_this9._base, path); | ||
}); // necessary because of wrapping in value field | ||
if (newVal !== oldVal) { | ||
@@ -458,3 +477,3 @@ oldVal = newVal; | ||
has: function has(obj, prop) { | ||
var path = getPath(prop).slice(1); // necessary because we wrap within the value field. | ||
var path = getPath(prop); // necessary because we wrap within the value field. | ||
var had = prop in obj; | ||
@@ -492,3 +511,3 @@ recorder.sub(_this10.onChange, function (patch) { | ||
get: function get() { | ||
return this._data.value; | ||
return this._proxify().value; | ||
} | ||
@@ -551,3 +570,5 @@ }]); | ||
function SrcJsonCell(init) { | ||
function SrcJsonCell() { | ||
var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
_classCallCheck(this, SrcJsonCell); | ||
@@ -569,3 +590,3 @@ | ||
get: function get() { | ||
return this._data.value; | ||
return this._proxify().value; | ||
} | ||
@@ -572,0 +593,0 @@ }]); |
@@ -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){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 f(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 h(t,e,n,r){var o=this;return b.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=x(e,[c({},r,t),0,0]);r in n&&(delete n[r],o.onChange.pub(a))}),!0})}function d(t,e,n,r,o){var a=this;return b.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=b.snap(function(){return n[r]}),i=j.diff(c({},r,t),c({},r,o));i&&(j.patch(n,i),a.onChange.pub(x(e,i)))}),!0})}function y(t,e,n,r){var o=n[r];return"__proto__"===r||v.default.isFunction(n[r])?n[r]:(this.subscribeProperty(t,n,r),v.default.isObject(o)?new Proxy(o,this.conf(t(r),n)):o)}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 v=u(e),b=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),_=u(r),g=u(o),P=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")}}(),k=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=w.default.create({cloneDiffValues:!0}),O=b._recorder,C=t.patchHas=function t(e,n){var r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(v.default.isString(n))return t(e,n.split("."));if(!n.length)return!0;v.default.isArray(e)&&!r&&(r=!0,e=e[0]);var o=p(n),a=o[0],i=o.slice(1);if(v.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},x=function(t,e){if(!t.length)return e;var n={};return(0,g.default)(n,t,e),n},A=(t.UPDATE=Symbol("update"),t.ObsJsonCell=function(t){function e(t){l(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 f(e,b.ObsBase),k(e,[{key:"_updating",value:function(t){this._oldUpdating=this._nowUpdating||!1,this._nowUpdating=!0;try{b.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 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:"mkGetProperty",value:function(t,e){var n=this;return function(r,o){return n.getProperty(t,e,r,o)}}},{key:"deleteProperty",value:function(t,e,n,r){return h.call(this,t,e,n,r)}},{key:"setProperty",value:function(t,e,n,r,o){return d.call(this,t,e,n,r,o)}},{key:"getProperty",value:function(t,e,n,r){return y.call(this,t,e,n,r)}},{key:"subscribeProperty",value:function(t,e,n){var r=this,o=t(n);if("length"===n&&v.default.isArray(e)){var a=e.length;O.sub(this.onChange,function(){var t=(0,_.default)(r._base,o.slice(1));return t!==a&&(a=t,!0)})}else O.sub(this.onChange,function(t){return C(t,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:this.mkGetProperty(n,t),has:function(t,r){var o=n(r).slice(1),a=r in t;return O.sub(e.onChange,function(t){var n=(0,P.default)(e._base,o);return a!==n&&(a=n,!0)}),a},ownKeys:function(n){return O.sub(e.onChange,function(e){var n=(0,_.default)(e,t);return!!n&&(!!v.default.isArray(n&&2!==n.length)||"a"===n._t&&v.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=s(this,(t=e.__proto__||Object.getPrototypeOf(e)).call.apply(t,[this].concat(r)));return Error.captureStackTrace(a,e),a}return f(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=s(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,n));r.f=t;var o=b.bind(r.f);return b.autoSub(o.onSet,function(t){var e=m(t,2),n=(e[0],e[1]);return r._update(n)}),r._makeReadOnly(),r}return f(e,A),e}(),U=t.SrcJsonCell=function(t){function e(t){return l(this,e),s(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t))}return f(e,A),k(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 b.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","lodash.clonedeep","jsondiffpatch"],e);else if("undefined"!=typeof exports)e(exports,require("underscore"),require("bobtail-rx"),require("lodash.get"),require("lodash.set"),require("lodash.hasin"),require("lodash.clonedeep"),require("jsondiffpatch"));else{var n={exports:{}};e(n.exports,t._,t.rx,t.lodashGet,t.lodashSet,t.lodashHasin,t.lodashClonedeep,t.jsondiffpatch),t.bobtailJsonCell=n.exports}}(this,function(t,e,n,r,o,a,i,u){"use strict";function l(t){return t&&t.__esModule?t:{default:t}}function s(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 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,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function h(t){return Array.isArray(t)?t:Array.from(t)}function d(t,e,n,r){var o=this;return _.snap(function(){var a=t(r);return x.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:_.snap(function(){return o.data})})),x.mutating(function(){var t=n[r],a=A(e,[p({},r,t),0,0]);r in n&&(delete n[r],o.onChange.pub(a))}),!0})}function y(t,e,n,r,o){var a=this;return _.snap(function(){var i=t(r);return x.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:_.snap(function(){return a.data})})),x.mutating(function(){var t=_.snap(function(){return n[r]}),i=C.diff(p({},r,t),p({},r,o));i&&(C.patch(n,i),a.onChange.pub(A(e,i)))}),!0})}function v(t,e,n,r){var o=n[r];return"IS_PROXY_SYM"===r||("__proto__"===r||b.default.isFunction(n[r])?n[r]:(this.subscribeProperty(t,n,r),b.default.isObject(o)?new Proxy(o,this.conf(t(r))):o))}Object.defineProperty(t,"__esModule",{value:!0}),t.update=t.jsonCell=t.SrcJsonCell=t.DepJsonCell=t.DepMutationError=t.ObsJsonCell=t.UPDATE=t.patchHas=t.IS_PROXY_SYM=void 0,t.logReturn=function(t){return console.info(t),t};var b=l(e),_=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),g=l(r),P=l(o),w=l(a),k=l(i),m=l(u),O=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}}(),C=m.default.create({cloneDiffValues:!0}),x=_._recorder,S=(t.IS_PROXY_SYM=Symbol("is_proxy"),t.patchHas=function t(e,n){var r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(b.default.isString(n))return t(e,n.split("."));if(!n.length)return!0;b.default.isArray(e)&&!r&&(r=!0,e=e[0]);var o=h(n),a=o[0],i=o.slice(1);if(b.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}),A=function(t,e){if(!t.length)return e;var n={};return(0,P.default)(n,t,e),n},E=(t.UPDATE=Symbol("update"),t.ObsJsonCell=function(t){function e(t){s(this,e);var n=f(this,(e.__proto__||Object.getPrototypeOf(e)).call(this));return n.onChange=n._mkEv(function(){return C.diff({},t)}),n.onUnsafeMutation=n._mkEv(function(){}),n._base={value:t},n}return c(e,_.ObsBase),j(e,[{key:"_proxify",value:function(){return new Proxy(this._base,this.conf([]))}},{key:"_updating",value:function(t){var e=this._nowUpdating||!1;this._nowUpdating=!0;try{_.snap(t)}finally{this._nowUpdating=e}return!0}},{key:"_update",value:function(t){var e=this;return this._updating(function(){var n=C.diff(e._base,{value:t});C.patch(e._proxify(),n)}),!0}},{key:"all",value:function(){return this.data}},{key:"cloneRaw",value:function(){return(0,k.default)(this._base.value)}},{key:"readonly",value:function(){var t=this;return new M(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:"mkGetProperty",value:function(t,e){var n=this;return function(r,o){return n.getProperty(t,e,r,o)}}},{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 y.call(this,t,e,n,r,o)}},{key:"getProperty",value:function(t,e,n,r){return v.call(this,t,e,n,r)}},{key:"subscribeProperty",value:function(t,e,n){var r=this,o=t(n);if("length"===n&&b.default.isArray(e)){var a=e.length;x.sub(this.onChange,function(){var t=_.snap(function(){return(0,g.default)(r._base,o)});return t!==a&&(a=t,!0)})}else x.sub(this.onChange,function(t){return S(t,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 D("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 D("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:this.mkGetProperty(n,t),has:function(t,r){var o=n(r),a=r in t;return x.sub(e.onChange,function(t){var n=(0,w.default)(e._base,o);return a!==n&&(a=n,!0)}),a},ownKeys:function(n){return x.sub(e.onChange,function(e){var n=(0,g.default)(e,t);return!!n&&(!!b.default.isArray(n&&2!==n.length)||"a"===n._t&&b.default.chain(n).omit("_t").values().value().some())}),Reflect.ownKeys(n)}}}},{key:"data",get:function(){return this._proxify().value}}]),e}()),D=t.DepMutationError=function(t){function e(){var t;s(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 c(e,Error),e}(),M=t.DepJsonCell=function(t){function e(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};s(this,e);var r=f(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,n));r.f=t;var o=_.bind(r.f);return _.autoSub(o.onSet,function(t){var e=O(t,2),n=(e[0],e[1]);return r._update(n)}),r._makeReadOnly(),r}return c(e,E),e}(),U=t.SrcJsonCell=function(t){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return s(this,e),f(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t))}return c(e,E),j(e,[{key:"update",value:function(t){return this._update(t)}},{key:"data",set:function(t){this._update(t)},get:function(){return this._proxify().value}}]),e}();t.jsonCell=function(t){return new U(t).data},t.update=function(t,e){return _.snap(function(){var n=C.diff(t,e);return C.patch(t,n),!0})}}); |
{ | ||
"name": "bobtail-json-cell", | ||
"version": "0.2.6", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -44,2 +44,3 @@ "main": "dist/main.js", | ||
"jsondiffpatch": "^0.2.4", | ||
"lodash.clonedeep": "^4.5.0", | ||
"lodash.get": "^4.4.2", | ||
@@ -46,0 +47,0 @@ "lodash.hasin": "^4.5.2", |
@@ -6,3 +6,3 @@ import nn from 'nevernull'; | ||
let {snap, bind} = rx; | ||
let {DepMutationError, DepJsonCell, SrcJsonCell, jsonCell} = jsonRx; | ||
let {IS_PROXY_SYM, DepMutationError, DepJsonCell, SrcJsonCell, jsonCell} = jsonRx; | ||
import deepGet from 'lodash.get'; | ||
@@ -19,3 +19,2 @@ | ||
}); | ||
console.info(foo); | ||
let zebra = bind(() => { | ||
@@ -178,3 +177,53 @@ let suffix = deepGet(foo, 'f.x.z'); | ||
expect(dep.raw()).toEqual([1,2,3,4]); | ||
}) | ||
}); | ||
}); | ||
describe('cloneRaw', () => { | ||
it('should not return a Proxy', () => { | ||
expect(new SrcJsonCell({}).raw()[IS_PROXY_SYM]).toBeUndefined() | ||
}); | ||
it('should not return a reactive object', () => { | ||
let cell = new SrcJsonCell({a: 1, b: {c: 2}}); | ||
let x = cell.cloneRaw(); | ||
let y = rx.bind(() => x); | ||
cell.data.a = 2; | ||
cell.data.b.c = 3; | ||
expect(y.raw()).toEqual({a: 1, b: {c: 2}}); | ||
}); | ||
it('should not return an object whose mutations will affect its source', () => { | ||
let cell = new SrcJsonCell({a: 1, b: {c: 2}}); | ||
let x = cell.cloneRaw(); | ||
x.a = 2; | ||
expect(rx.snap(() => cell.data.a)).toBe(1); | ||
}); | ||
it('should return a clone of its parent object', () => { | ||
let cell = new SrcJsonCell({a: 1, b: {c: 2}}); | ||
let x = cell.cloneRaw(); | ||
expect(rx.snap(() => _.isEqual(x, cell.data))).toBe(true); | ||
expect(rx.snap(() => x === cell.data)).toBe(false); | ||
}); | ||
}); | ||
describe('multiple proxies', () => { | ||
// cell.data returns a new Proxy every time, so make sure that this doesn't break reactivity. | ||
it('Cells dependent on different proxies from the same object should update whenever the object changes', () => { | ||
let cell = new SrcJsonCell({a: 1}); | ||
let x = cell.data; | ||
let y = cell.data; | ||
let xCell = bind(() => x.a); | ||
let yCell = bind(() => y.a); | ||
x.a = 2; | ||
expect(xCell.raw()).toBe(2); | ||
expect(yCell.raw()).toBe(2); | ||
}); | ||
}); | ||
describe('nullification', () => { | ||
it("should work even after being reset to null", () => { | ||
// This previously caused breakages due to reliance on the old _base attribute. | ||
// let's make sure it doesn't sneak in again. | ||
let x = new SrcJsonCell(null); | ||
x.data = [1, 2, 3, 4]; | ||
expect(bind(() => x.data.map(_.identity)).raw()).toEqual([1, 2, 3, 4]); | ||
}); | ||
}); |
@@ -6,2 +6,3 @@ import _ from 'underscore'; | ||
import deepHas from 'lodash.hasin'; | ||
import cloneDeep from 'lodash.clonedeep'; | ||
@@ -16,2 +17,3 @@ import patchFactory from 'jsondiffpatch'; | ||
let recorder = rx._recorder; | ||
export let IS_PROXY_SYM = Symbol('is_proxy'); | ||
@@ -77,3 +79,3 @@ export let patchHas = function(patch, path, diffArray=false) { | ||
); | ||
this.onUnsafeMutation.pub({op: 'delete', path, obj, prop, base: this._base}) | ||
this.onUnsafeMutation.pub({op: 'delete', path, obj, prop, base: rx.snap(() => this.data)}) | ||
} | ||
@@ -101,3 +103,3 @@ recorder.mutating(() => { | ||
); | ||
this.onUnsafeMutation.pub({op: 'set', path, obj, prop, val, base: this._base}); | ||
this.onUnsafeMutation.pub({op: 'set', path, obj, prop, val, base: rx.snap(() => this.data)}); | ||
} | ||
@@ -118,2 +120,5 @@ recorder.mutating(() => { | ||
let val = obj[prop]; | ||
if (prop === 'IS_PROXY_SYM') { | ||
return true; | ||
} | ||
if (prop === '__proto__' || _.isFunction(obj[prop])) { | ||
@@ -125,3 +130,3 @@ return obj[prop]; | ||
if (_.isObject(val)) { | ||
return new Proxy(val, this.conf(getPath(prop), obj)); | ||
return new Proxy(val, this.conf(getPath(prop))); | ||
} | ||
@@ -134,13 +139,14 @@ return val; | ||
super(); | ||
this._base = _base; | ||
this.onChange = this._mkEv(() => jsondiffpatch.diff({}, this._base)); | ||
this.onChange = this._mkEv(() => jsondiffpatch.diff({}, _base)); | ||
this.onUnsafeMutation = this._mkEv(() => {}); | ||
this._updating(() => this._data = new Proxy({value: this._base}, this.conf([], null))); | ||
this._base = {value: _base}; | ||
} | ||
_proxify() {return new Proxy(this._base, this.conf([]));} | ||
_updating (f) { | ||
this._oldUpdating = this._nowUpdating || false; | ||
let _oldUpdating = this._nowUpdating || false; | ||
this._nowUpdating = true; | ||
try {rx.snap(f);} | ||
finally {this._nowUpdating = this._oldUpdating;} | ||
finally {this._nowUpdating = _oldUpdating;} | ||
return true; | ||
@@ -151,4 +157,4 @@ } | ||
this._updating(() => { | ||
let diff = jsondiffpatch.diff(this._data, {value: newVal}); | ||
jsondiffpatch.patch(this._data, diff); | ||
let diff = jsondiffpatch.diff(this._base, {value: newVal}); | ||
jsondiffpatch.patch(this._proxify(), diff); | ||
}); | ||
@@ -158,6 +164,7 @@ return true; | ||
all () {return this.data;} | ||
all () {return this.data} | ||
cloneRaw () {return cloneDeep(this._base.value)} | ||
readonly () {return new DepJsonCell(() => this.data)} | ||
get data() {return this._data.value;} | ||
get data() {return this._proxify().value;} | ||
@@ -193,3 +200,3 @@ mkDeleteProperty (getPath, basePath) { | ||
recorder.sub(this.onChange, () => { | ||
let newVal = deepGet(this._base, path.slice(1)); // necessary because of wrapping in value field | ||
let newVal = rx.snap(() => deepGet(this._base, path)); // necessary because of wrapping in value field | ||
if(newVal !== oldVal){ | ||
@@ -234,7 +241,7 @@ oldVal = newVal; | ||
has: (obj, prop) => { | ||
let path = getPath(prop).slice(1); // necessary because we wrap within the value field. | ||
let had = prop in obj; | ||
let path = getPath(prop); // necessary because we wrap within the value field. | ||
let had = prop in obj; | ||
recorder.sub(this.onChange, patch => { | ||
let has = deepHas(this._base, path); | ||
if(had !== has) { | ||
if (had !== has) { | ||
had = has; | ||
@@ -286,6 +293,6 @@ return true; | ||
export class SrcJsonCell extends ObsJsonCell { | ||
constructor (init) {super(init);} | ||
constructor (init = {}) {super(init);} | ||
update(val) {return this._update(val);} | ||
set data(val) {this._update(val);} | ||
get data() {return this._data.value;} | ||
get data() {return this._proxify().value;} | ||
} | ||
@@ -292,0 +299,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
193699
154.05%17
6.25%4128
342.92%8
14.29%1
Infinity%+ Added
+ Added