UNPKG

active-data

Version:

Reactive data manager, inspired by MobX. Automatically detects associated data and perform updates to your views or everything dependent on that data when it changes. Implemented with js Proxy objects

2 lines 18 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.activeData=e():t.activeData=e()}("undefined"!=typeof self?self:this,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=24)}([function(t,e){var n=t.exports="undefined"!=typeof window&&window.Math==Math?window:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")();"number"==typeof __g&&(__g=n)},function(t,e,n){var r=n(8)("wks"),o=n(9),i=n(0).Symbol,a="function"==typeof i;(t.exports=function(t){return r[t]||(r[t]=a&&i[t]||(a?i:o)("Symbol."+t))}).store=r},function(t,e,n){var r=n(4),o=n(18);t.exports=n(5)?function(t,e,n){return r.f(t,e,o(1,n))}:function(t,e,n){return t[e]=n,t}},function(t,e){var n=t.exports={version:"2.6.5"};"number"==typeof __e&&(__e=n)},function(t,e,n){var r=n(10),o=n(29),i=n(30),a=Object.defineProperty;e.f=n(5)?Object.defineProperty:function(t,e,n){if(r(t),e=i(e,!0),r(n),o)try{return a(t,e,n)}catch(t){}if("get"in n||"set"in n)throw TypeError("Accessors not supported!");return"value"in n&&(t[e]=n.value),t}},function(t,e,n){t.exports=!n(16)(function(){return 7!=Object.defineProperty({},"a",{get:function(){return 7}}).a})},function(t,e){var n={}.hasOwnProperty;t.exports=function(t,e){return n.call(t,e)}},function(t,e){t.exports=!1},function(t,e,n){var r=n(3),o=n(0),i=o["__core-js_shared__"]||(o["__core-js_shared__"]={});(t.exports=function(t,e){return i[t]||(i[t]=void 0!==e?e:{})})("versions",[]).push({version:r.version,mode:n(7)?"pure":"global",copyright:"© 2019 Denis Pushkarev (zloirock.ru)"})},function(t,e){var n=0,r=Math.random();t.exports=function(t){return"Symbol(".concat(void 0===t?"":t,")_",(++n+r).toString(36))}},function(t,e,n){var r=n(11);t.exports=function(t){if(!r(t))throw TypeError(t+" is not an object!");return t}},function(t,e){t.exports=function(t){return"object"==typeof t?null!==t:"function"==typeof t}},function(t,e){t.exports={}},function(t,e,n){var r=n(35),o=n(19);t.exports=function(t){return r(o(t))}},function(t,e,n){var r=n(0),o=n(2),i=n(6),a=n(9)("src"),u=n(39),c=(""+u).split("toString");n(3).inspectSource=function(t){return u.call(t)},(t.exports=function(t,e,n,u){var s="function"==typeof n;s&&(i(n,"name")||o(n,"name",e)),t[e]!==n&&(s&&(i(n,a)||o(n,a,t[e]?""+t[e]:c.join(String(e)))),t===r?t[e]=n:u?t[e]?t[e]=n:o(t,e,n):(delete t[e],o(t,e,n)))})(Function.prototype,"toString",function(){return"function"==typeof this&&this[a]||u.call(this)})},function(t,e,n){var r=n(8)("keys"),o=n(9);t.exports=function(t){return r[t]||(r[t]=o(t))}},function(t,e){t.exports=function(t){try{return!!t()}catch(t){return!0}}},function(t,e,n){var r=n(11),o=n(0).document,i=r(o)&&r(o.createElement);t.exports=function(t){return i?o.createElement(t):{}}},function(t,e){t.exports=function(t,e){return{enumerable:!(1&t),configurable:!(2&t),writable:!(4&t),value:e}}},function(t,e){t.exports=function(t){if(null==t)throw TypeError("Can't call method on "+t);return t}},function(t,e,n){var r=n(45),o=n(22);t.exports=Object.keys||function(t){return r(t,o)}},function(t,e){var n=Math.ceil,r=Math.floor;t.exports=function(t){return isNaN(t=+t)?0:(t>0?r:n)(t)}},function(t,e){t.exports="constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf".split(",")},function(t,e,n){var r=n(4).f,o=n(6),i=n(1)("toStringTag");t.exports=function(t,e,n){t&&!o(t=n?t:t.prototype,i)&&r(t,i,{configurable:!0,value:e})}},function(t,e,n){"use strict";n.r(e),function(t){n.d(e,"Manager",function(){return a}),n.d(e,"observable",function(){return u}),n.d(e,"reaction",function(){return c}),n.d(e,"computed",function(){return s}),n.d(e,"updatable",function(){return f});n(26),n(31);const r=new WeakMap,o=t=>r.set(t,{}),i=t=>r.get(t);class a{constructor(t){const e=this;o(e);const n=i(e);n.gen=0,n.intentToRun=0,n.dataSourceKey=Symbol("dataSource"),n.observables=new WeakMap,n.options={enabled:!0,immediateReaction:!1,maxIterations:10,watchKey:"$$watch",watchDeepKey:"$$watchDeep",afterRun:null,timeLimit:50,getTime:"undefined"!=typeof performance?()=>performance.now():()=>Date.now()},n.callStack=[],n.reactionsToUpdate=new Set,e.setOptions(t),e.makeObservable=e.makeObservable.bind(e),e.makeReaction=e.makeReaction.bind(e),e.makeComputed=e.makeComputed.bind(e),e.makeUpdatable=e.makeUpdatable.bind(e),e.mapProperties=e.mapProperties.bind(e),e.isObservable=e.isObservable.bind(e),e.getDataSource=e.getDataSource.bind(e),e.observable=e.makeObservable,e.reaction=e.makeReaction,e.computed=e.makeComputed,e.updatable=e.makeUpdatable}mapProperties(t,e,n){[].concat(n||Object.keys(t)).forEach(n=>{Object.defineProperty(e,n,{enumerable:!0,get(){return Reflect.get(t,n,this)},set(e){return Reflect.set(t,n,e,this)}})})}setOptions(t={}){i(this).options=Object.assign(i(this).options,t)}getOptions(){return Object.assign({},i(this).options)}makeObservable(t){const e=this,n=i(e);if(!t)return t;if(t.constructor!==Object&&t.constructor!==Array&&"function"!=typeof t)return t;if(e.isObservable(t))return t;const r=Array.isArray(t);let o=n.observables.get(t);if(!o){const i=new Map,a={};Object.keys(t).forEach(n=>{const r=Object.getOwnPropertyDescriptor(t,n);r&&"function"==typeof r.get&&(a[n]=e.makeUpdatable(r.get))});const u=t=>{t.invalidIteration=!0,t.onInvalidate&&t.onInvalidate(),t.valid&&(t.valid=!1,t.deps.forEach(t=>u(t))),t.deps.clear()},c=t=>{const e=new Set;return i.set(t,e),e};let s=!1;const f=(r,u)=>{const f=u===n.options.watchDeepKey?n.options.watchKey:u;if(u===n.options.watchDeepKey){if(s)return;s=!0,Object.keys(t).forEach(r=>{if("object"==typeof t[r]){e.makeObservable(t[r])[n.options.watchDeepKey]}}),s=!1}f===n.options.watchKey&&Object.keys(a).forEach(t=>{a[t].call(o)});const l=i.get(f)||c(f);l.has(r)||(l.add(r),r.uninitMap.set(t,t=>{l.delete(t),0===l.size&&i.delete(f)}))},l={toUpdate:i,dataSource:t,registerRead:f},p=t=>{const r=t=>u(t);if(null==t)[...l.toUpdate.values()].forEach(t=>t.forEach(r));else{const e=l.toUpdate.get(t);e&&e.forEach(r);const o=l.toUpdate.get(n.options.watchKey);o&&o.forEach(r)}n.inRunSection||1!==n.intentToRun||(n.options.immediateReaction?e.run():e.runDeferred())};o=new Proxy(t,{get:(o,i,a)=>{if(i===n.dataSourceKey)return t;let u;if(n.callStack.length&&(u=n.callStack[n.callStack.length-1],f(u,i)),i===n.options.watchKey||i===n.options.watchDeepKey)return a;const c=Reflect.get(o,i,a);return r&&"function"==typeof c&&"constructor"!==i?new Proxy(c,{apply:(e,r,o)=>{if(["copyWithin","fill","pop","push","reverse","shift","sort","splice","unshift"].includes(i)){n.intentToRun++;try{p()}finally{n.intentToRun--}return e.apply(t,o)}return e.apply(r,o)}}):"object"==typeof c?e.makeObservable(c):c},set:(t,e,r,o)=>{if(r!==Reflect.get(t,e,o)||Array.isArray(t)&&"length"===e){n.intentToRun++;try{Reflect.set(t,e,r,o),p(e)}finally{n.intentToRun--}}return!0},defineProperty:(t,n,r)=>(r&&"function"==typeof r.get&&(a[n]=e.makeUpdatable(r.get)),Reflect.defineProperty(t,n,r)),deleteProperty:(t,e)=>{n.intentToRun++;try{e in a&&delete a[e],p(e)}finally{n.intentToRun--}return Reflect.deleteProperty(t,e)}}),n.observables.set(t,o)}return o}makeUpdatable(t,e={}){if(t.originalFn)return t;const n=e.onInvalidate,r=e.onUninit,o=i(this),a={id:++o.gen,active:!0,valid:!1,onInvalidate:n,onUninit:r,value:void 0,deps:new Set,uninitMap:new Map,uninit:()=>{[...a.uninitMap.values()].forEach(t=>t(a)),a.uninitMap.clear()}},u=function(){if(!a.active)return t.call(this,this);if(a.computing)console.warn('Detected cross reference inside computed properties! "undefined" will be returned to prevent infinite loop');else{if(o.callStack.length&&a.deps.add(o.callStack[o.callStack.length-1]),a.valid)return a.value;a.computing=!0,a.uninit(),o.callStack.push(a);try{const e=this?o.observables.get(this)||this:null;a.invalidIteration=!1;const n=t.call(e,e);return a.valid=!a.invalidIteration,a.value=n,n}finally{a.computing=!1,o.callStack.pop()}}};return u.uninit=(()=>{a.uninit(),a.active=!1,r&&r()}),u.invalidate=(()=>{a.valid=!1}),u.originalFn=t,u}makeComputed(t,e,n,r){Object.defineProperty(t,e,{enumerable:!0,configurable:!0,get:this.makeUpdatable(n),set:r})}makeReaction(t,e=!0){const n=this,r=i(n),o=n.makeUpdatable(t,{onInvalidate:()=>r.reactionsToUpdate.add(o),onUninit:()=>r.reactionsToUpdate.delete(o)});return r.reactionsToUpdate.add(o),e&&(r.options.immediateReaction?n.run():n.runDeferred()),o}getDataSource(t){return t[i(this).dataSourceKey]}isObservable(t){return null!=t[i(this).dataSourceKey]}run(t){const e=this,n=i(e);if(n.options.enabled){n.inRunSection=!0;try{"function"==typeof t&&t(),n.runScheduled=!1;let r=0;for(;n.reactionsToUpdate.size;){if(r>n.options.maxIterations)throw n.reactionsToUpdate.clear(),new Error("Max iterations exceeded!");r++;const t=n.options.getTime(),o=[...n.reactionsToUpdate.values()];for(const e of o)if(n.reactionsToUpdate.delete(e),e(),n.options.getTime()-t>=n.options.timeLimit)break;n.reactionsToUpdate.size&&e.runDeferred()}"function"==typeof n.options.afterRun&&n.options.afterRun()}finally{n.inRunSection=!1}}}runDeferred(t,e=0){const n=i(this);if(n.options.enabled){n.inRunSection=!0;try{n.runScheduled||(n.runScheduled=setTimeout(()=>this.run(),e)),"function"==typeof t&&t()}finally{n.inRunSection=!1}}}}a.default=new a,a.default.Manager=a,e.default=a.default;const u=a.default.observable,c=a.default.reaction,s=a.default.computed,f=a.default.updatable}.call(this,n(25))},function(t,e){var n,r,o=t.exports={};function i(){throw new Error("setTimeout has not been defined")}function a(){throw new Error("clearTimeout has not been defined")}function u(t){if(n===setTimeout)return setTimeout(t,0);if((n===i||!n)&&setTimeout)return n=setTimeout,setTimeout(t,0);try{return n(t,0)}catch(e){try{return n.call(null,t,0)}catch(e){return n.call(this,t,0)}}}!function(){try{n="function"==typeof setTimeout?setTimeout:i}catch(t){n=i}try{r="function"==typeof clearTimeout?clearTimeout:a}catch(t){r=a}}();var c,s=[],f=!1,l=-1;function p(){f&&c&&(f=!1,c.length?s=c.concat(s):l=-1,s.length&&d())}function d(){if(!f){var t=u(p);f=!0;for(var e=s.length;e;){for(c=s,s=[];++l<e;)c&&c[l].run();l=-1,e=s.length}c=null,f=!1,function(t){if(r===clearTimeout)return clearTimeout(t);if((r===a||!r)&&clearTimeout)return r=clearTimeout,clearTimeout(t);try{r(t)}catch(e){try{return r.call(null,t)}catch(e){return r.call(this,t)}}}(t)}}function y(t,e){this.fun=t,this.array=e}function v(){}o.nextTick=function(t){var e=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)e[n-1]=arguments[n];s.push(new y(t,e)),1!==s.length||f||u(d)},y.prototype.run=function(){this.fun.apply(null,this.array)},o.title="browser",o.browser=!0,o.env={},o.argv=[],o.version="",o.versions={},o.on=v,o.addListener=v,o.once=v,o.off=v,o.removeListener=v,o.removeAllListeners=v,o.emit=v,o.prependListener=v,o.prependOnceListener=v,o.listeners=function(t){return[]},o.binding=function(t){throw new Error("process.binding is not supported")},o.cwd=function(){return"/"},o.chdir=function(t){throw new Error("process.chdir is not supported")},o.umask=function(){return 0}},function(t,e,n){n(27)("asyncIterator")},function(t,e,n){var r=n(0),o=n(3),i=n(7),a=n(28),u=n(4).f;t.exports=function(t){var e=o.Symbol||(o.Symbol=i?{}:r.Symbol||{});"_"==t.charAt(0)||t in e||u(e,t,{value:a.f(t)})}},function(t,e,n){e.f=n(1)},function(t,e,n){t.exports=!n(5)&&!n(16)(function(){return 7!=Object.defineProperty(n(17)("div"),"a",{get:function(){return 7}}).a})},function(t,e,n){var r=n(11);t.exports=function(t,e){if(!r(t))return t;var n,o;if(e&&"function"==typeof(n=t.toString)&&!r(o=n.call(t)))return o;if("function"==typeof(n=t.valueOf)&&!r(o=n.call(t)))return o;if(!e&&"function"==typeof(n=t.toString)&&!r(o=n.call(t)))return o;throw TypeError("Can't convert object to primitive value")}},function(t,e,n){for(var r=n(32),o=n(20),i=n(14),a=n(0),u=n(2),c=n(12),s=n(1),f=s("iterator"),l=s("toStringTag"),p=c.Array,d={CSSRuleList:!0,CSSStyleDeclaration:!1,CSSValueList:!1,ClientRectList:!1,DOMRectList:!1,DOMStringList:!1,DOMTokenList:!0,DataTransferItemList:!1,FileList:!1,HTMLAllCollection:!1,HTMLCollection:!1,HTMLFormElement:!1,HTMLSelectElement:!1,MediaList:!0,MimeTypeArray:!1,NamedNodeMap:!1,NodeList:!0,PaintRequestList:!1,Plugin:!1,PluginArray:!1,SVGLengthList:!1,SVGNumberList:!1,SVGPathSegList:!1,SVGPointList:!1,SVGStringList:!1,SVGTransformList:!1,SourceBufferList:!1,StyleSheetList:!0,TextTrackCueList:!1,TextTrackList:!1,TouchList:!1},y=o(d),v=0;v<y.length;v++){var h,b=y[v],m=d[b],g=a[b],S=g&&g.prototype;if(S&&(S[f]||u(S,f,p),S[l]||u(S,l,b),c[b]=p,m))for(h in r)S[h]||i(S,h,r[h],!0)}},function(t,e,n){"use strict";var r=n(33),o=n(34),i=n(12),a=n(13);t.exports=n(37)(Array,"Array",function(t,e){this._t=a(t),this._i=0,this._k=e},function(){var t=this._t,e=this._k,n=this._i++;return!t||n>=t.length?(this._t=void 0,o(1)):o(0,"keys"==e?n:"values"==e?t[n]:[n,t[n]])},"values"),i.Arguments=i.Array,r("keys"),r("values"),r("entries")},function(t,e,n){var r=n(1)("unscopables"),o=Array.prototype;null==o[r]&&n(2)(o,r,{}),t.exports=function(t){o[r][t]=!0}},function(t,e){t.exports=function(t,e){return{value:e,done:!!t}}},function(t,e,n){var r=n(36);t.exports=Object("z").propertyIsEnumerable(0)?Object:function(t){return"String"==r(t)?t.split(""):Object(t)}},function(t,e){var n={}.toString;t.exports=function(t){return n.call(t).slice(8,-1)}},function(t,e,n){"use strict";var r=n(7),o=n(38),i=n(14),a=n(2),u=n(12),c=n(42),s=n(23),f=n(50),l=n(1)("iterator"),p=!([].keys&&"next"in[].keys()),d=function(){return this};t.exports=function(t,e,n,y,v,h,b){c(n,e,y);var m,g,S,x=function(t){if(!p&&t in k)return k[t];switch(t){case"keys":case"values":return function(){return new n(this,t)}}return function(){return new n(this,t)}},w=e+" Iterator",O="values"==v,T=!1,k=t.prototype,j=k[l]||k["@@iterator"]||v&&k[v],R=j||x(v),P=v?O?x("entries"):R:void 0,_="Array"==e&&k.entries||j;if(_&&(S=f(_.call(new t)))!==Object.prototype&&S.next&&(s(S,w,!0),r||"function"==typeof S[l]||a(S,l,d)),O&&j&&"values"!==j.name&&(T=!0,R=function(){return j.call(this)}),r&&!b||!p&&!T&&k[l]||a(k,l,R),u[e]=R,u[w]=d,v)if(m={values:O?R:x("values"),keys:h?R:x("keys"),entries:P},b)for(g in m)g in k||i(k,g,m[g]);else o(o.P+o.F*(p||T),e,m);return m}},function(t,e,n){var r=n(0),o=n(3),i=n(2),a=n(14),u=n(40),c=function(t,e,n){var s,f,l,p,d=t&c.F,y=t&c.G,v=t&c.S,h=t&c.P,b=t&c.B,m=y?r:v?r[e]||(r[e]={}):(r[e]||{}).prototype,g=y?o:o[e]||(o[e]={}),S=g.prototype||(g.prototype={});for(s in y&&(n=e),n)l=((f=!d&&m&&void 0!==m[s])?m:n)[s],p=b&&f?u(l,r):h&&"function"==typeof l?u(Function.call,l):l,m&&a(m,s,l,t&c.U),g[s]!=l&&i(g,s,p),h&&S[s]!=l&&(S[s]=l)};r.core=o,c.F=1,c.G=2,c.S=4,c.P=8,c.B=16,c.W=32,c.U=64,c.R=128,t.exports=c},function(t,e,n){t.exports=n(8)("native-function-to-string",Function.toString)},function(t,e,n){var r=n(41);t.exports=function(t,e,n){if(r(t),void 0===e)return t;switch(n){case 1:return function(n){return t.call(e,n)};case 2:return function(n,r){return t.call(e,n,r)};case 3:return function(n,r,o){return t.call(e,n,r,o)}}return function(){return t.apply(e,arguments)}}},function(t,e){t.exports=function(t){if("function"!=typeof t)throw TypeError(t+" is not a function!");return t}},function(t,e,n){"use strict";var r=n(43),o=n(18),i=n(23),a={};n(2)(a,n(1)("iterator"),function(){return this}),t.exports=function(t,e,n){t.prototype=r(a,{next:o(1,n)}),i(t,e+" Iterator")}},function(t,e,n){var r=n(10),o=n(44),i=n(22),a=n(15)("IE_PROTO"),u=function(){},c=function(){var t,e=n(17)("iframe"),r=i.length;for(e.style.display="none",n(49).appendChild(e),e.src="javascript:",(t=e.contentWindow.document).open(),t.write("<script>document.F=Object<\/script>"),t.close(),c=t.F;r--;)delete c.prototype[i[r]];return c()};t.exports=Object.create||function(t,e){var n;return null!==t?(u.prototype=r(t),n=new u,u.prototype=null,n[a]=t):n=c(),void 0===e?n:o(n,e)}},function(t,e,n){var r=n(4),o=n(10),i=n(20);t.exports=n(5)?Object.defineProperties:function(t,e){o(t);for(var n,a=i(e),u=a.length,c=0;u>c;)r.f(t,n=a[c++],e[n]);return t}},function(t,e,n){var r=n(6),o=n(13),i=n(46)(!1),a=n(15)("IE_PROTO");t.exports=function(t,e){var n,u=o(t),c=0,s=[];for(n in u)n!=a&&r(u,n)&&s.push(n);for(;e.length>c;)r(u,n=e[c++])&&(~i(s,n)||s.push(n));return s}},function(t,e,n){var r=n(13),o=n(47),i=n(48);t.exports=function(t){return function(e,n,a){var u,c=r(e),s=o(c.length),f=i(a,s);if(t&&n!=n){for(;s>f;)if((u=c[f++])!=u)return!0}else for(;s>f;f++)if((t||f in c)&&c[f]===n)return t||f||0;return!t&&-1}}},function(t,e,n){var r=n(21),o=Math.min;t.exports=function(t){return t>0?o(r(t),9007199254740991):0}},function(t,e,n){var r=n(21),o=Math.max,i=Math.min;t.exports=function(t,e){return(t=r(t))<0?o(t+e,0):i(t,e)}},function(t,e,n){var r=n(0).document;t.exports=r&&r.documentElement},function(t,e,n){var r=n(6),o=n(51),i=n(15)("IE_PROTO"),a=Object.prototype;t.exports=Object.getPrototypeOf||function(t){return t=o(t),r(t,i)?t[i]:"function"==typeof t.constructor&&t instanceof t.constructor?t.constructor.prototype:t instanceof Object?a:null}},function(t,e,n){var r=n(19);t.exports=function(t){return Object(r(t))}}]).default}); //# sourceMappingURL=active-data.modern.js.map