eko-flobx
Version:
Flux inspired state management on top of MobX
2 lines • 10.1 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("mobx")):"function"==typeof define&&define.amd?define("eko-flobx",["mobx"],e):"object"==typeof exports?exports["eko-flobx"]=e(require("mobx")):t["eko-flobx"]=e(t.mobx)}("undefined"!=typeof self?self:this,(function(t){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=20)}([function(t,e){function n(e){return t.exports=n=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},n(e)}t.exports=n},function(t,e){t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}},function(t,e){function n(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)}}t.exports=function(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),t}},function(t,e,n){var r=n(10);function o(e,n,i){return"undefined"!=typeof Reflect&&Reflect.get?t.exports=o=Reflect.get:t.exports=o=function(t,e,n){var o=r(t,e);if(o){var i=Object.getOwnPropertyDescriptor(o,e);return i.get?i.get.call(n):i.value}},o(e,n,i||e)}t.exports=o},function(t,e){t.exports=function(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}},function(t,e,n){var r=n(11);t.exports=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&r(t,e)}},function(t,e,n){var r=n(12),o=n(13);t.exports=function(t,e){return!e||"object"!==r(e)&&"function"!=typeof e?o(t):e}},function(e,n){e.exports=t},function(t,e,n){t.exports.Dispatcher=n(14)},function(t,e,n){var r=n(16),o=n(17),i=n(18),c=n(19);t.exports=function(t){return r(t)||o(t)||i(t)||c()}},function(t,e,n){var r=n(0);t.exports=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=r(t)););return t}},function(t,e){function n(e,r){return t.exports=n=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},n(e,r)}t.exports=n},function(t,e){function n(e){return"function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?t.exports=n=function(t){return typeof t}:t.exports=n=function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(e)}t.exports=n},function(t,e){t.exports=function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}},function(t,e,n){"use strict";function r(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var o=n(15),i=function(){function t(){r(this,"_callbacks",void 0),r(this,"_isDispatching",void 0),r(this,"_isHandled",void 0),r(this,"_isPending",void 0),r(this,"_lastID",void 0),r(this,"_pendingPayload",void 0),this._callbacks={},this._isDispatching=!1,this._isHandled={},this._isPending={},this._lastID=1}var e=t.prototype;return e.register=function(t){var e="ID_"+this._lastID++;return this._callbacks[e]=t,e},e.unregister=function(t){this._callbacks[t]||o(!1),delete this._callbacks[t]},e.waitFor=function(t){this._isDispatching||o(!1);for(var e=0;e<t.length;e++){var n=t[e];this._isPending[n]?this._isHandled[n]||o(!1):(this._callbacks[n]||o(!1),this._invokeCallback(n))}},e.dispatch=function(t){this._isDispatching&&o(!1),this._startDispatching(t);try{for(var e in this._callbacks)this._isPending[e]||this._invokeCallback(e)}finally{this._stopDispatching()}},e.isDispatching=function(){return this._isDispatching},e._invokeCallback=function(t){this._isPending[t]=!0,this._callbacks[t](this._pendingPayload),this._isHandled[t]=!0},e._startDispatching=function(t){for(var e in this._callbacks)this._isPending[e]=!1,this._isHandled[e]=!1;this._pendingPayload=t,this._isDispatching=!0},e._stopDispatching=function(){delete this._pendingPayload,this._isDispatching=!1},t}();t.exports=i},function(t,e,n){"use strict";var r=function(t){};t.exports=function(t,e){for(var n=arguments.length,o=new Array(n>2?n-2:0),i=2;i<n;i++)o[i-2]=arguments[i];if(r(e),!t){var c;if(void 0===e)c=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var a=0;(c=new Error(e.replace(/%s/g,(function(){return String(o[a++])})))).name="Invariant Violation"}throw c.framesToPop=1,c}}},function(t,e,n){var r=n(4);t.exports=function(t){if(Array.isArray(t))return r(t)}},function(t,e){t.exports=function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}},function(t,e,n){var r=n(4);t.exports=function(t,e){if(t){if("string"==typeof t)return r(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(t,e):void 0}}},function(t,e){t.exports=function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}},function(t,e,n){"use strict";n.r(e),n.d(e,"StateStore",(function(){return m})),n.d(e,"dispatcher",(function(){return P})),n.d(e,"dispatch",(function(){return D})),n.d(e,"reducer",(function(){return j})),n.d(e,"effect",(function(){return S})),n.d(e,"waitFor",(function(){return E}));var r=n(1),o=n.n(r),i=n(2),c=n.n(i),a=n(3),u=n.n(a),s=n(5),f=n.n(s),l=n(6),p=n.n(l),d=n(0),h=n.n(d),y=n(7),v=n(8),_=n(9),b=n.n(_),g=function(t){var e=[],n=t;do{e.push.apply(e,b()(Object.getOwnPropertyNames(n)))}while(n=Object.getPrototypeOf(n));return e.sort().filter((function(t,e,n){return n.indexOf(t)===e})).filter((function(e){return"function"==typeof t[e]}))},m=function(){function t(e){var n=this,r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];o()(this,t),this._name=void 0,this._reducers=new Map,this._effects=new Map,this.dispatchToken=void 0,this._name=e||this.constructor.name;var i=g(this);i.map((function(t){return n[t]._reducer})).filter(Boolean).forEach((function(t){var e=t.key,r=t.fn;return n._reducers.set(e.includes(".")?e:"".concat(n._name,".").concat(e),r.bind(n))})),i.map((function(t){return n[t]._effect})).filter(Boolean).forEach((function(t){var e=t.key,r=t.fn;return n._effects.set(e,r.bind(n))})),this.dispatchToken=P.register(this,r)}return c()(t,[{key:"reduce",value:function(t){var e=t.type,n=t.payload;if(!e)throw new Error("Action dispatched without a type");if(this._reducers.has(e)){var r=this._reducers.get(e);return r?r.apply(this,n):void 0}}}]),t}();function x(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,r=h()(t);if(e){var o=h()(this).constructor;n=Reflect.construct(r,arguments,o)}else n=r.apply(this,arguments);return p()(this,n)}}var w={},k=function(t){return new Proxy(t,{get:function(t,e){return e in t?t[e]:(t!==w&&console.warn("[eko-flobx] ".concat(e," doesn't exist in ").concat(t._name," store")),function(){})}})},O=function(t){f()(n,t);var e=x(n);function n(){var t;o()(this,n);for(var r=arguments.length,i=new Array(r),c=0;c<r;c++)i[c]=arguments[c];return(t=e.call.apply(e,[this].concat(i)))._storeNames=new Set,t}return c()(n,[{key:"register",value:function(t){var e=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];return this._addStore(t,e),u()(h()(n.prototype),"register",this).call(this,(function(e){return t.reduce(e)}))}},{key:"dispatch",value:function(t){var e=this;Object(y.runInAction)((function(){return u()(h()(n.prototype),"dispatch",e).call(e,t)}))}},{key:"_addStore",value:function(t,e){var n=this;if(this._storeNames.has(t._name))throw new Error("[eko-flobx] A store named ".concat(t._name," already exists"));t._name in this||(e&&this._storeNames.add(t._name),this[t._name]=k({_name:t._name})),t._reducers.forEach((function(e,r){n._addReducer(t._name,r)})),t._effects.forEach((function(e,r){n._addEffect(t._name,r,e)}))}},{key:"_addReducer",value:function(t,e){var n=this;if(e.startsWith("".concat(t,"."))){var r=e.replace("".concat(t,"."),"");this[t][r]=function(){for(var t=arguments.length,r=new Array(t),o=0;o<t;o++)r[o]=arguments[o];return n._dispatch("".concat(e),r)}}}},{key:"_addEffect",value:function(t,e,n){this[t][e]=n}},{key:"_dispatch",value:function(t,e){this.dispatch({type:t,payload:e})}}]),n}(v.Dispatcher),P=new Proxy(new O,{get:function(t,e){return e in t?t[e]:(t!==w&&console.warn("[eko-flobx] ".concat(e," store isn't loaded.")),k(w))}});function j(t,e,n){var r=n.value;return n.value=function(){throw new Error("A reducer method should not be called directly, use dispatcher shortcut")},n.value._reducer={key:e.replace("_",""),fn:r},n}function S(t,e,n){var r=n.value;return n.value=function(){throw new Error("An effect method should not be called directly, use dispatcher shortcut")},n.value._effect={key:e,fn:r},n}var D=P.dispatch.bind(P),E=P.waitFor.bind(P)}])}));
//# sourceMappingURL=client.js.map