@stated-library/base
Version:
3 lines (2 loc) • 7.89 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self)["@stated-library/base"]={})}(this,function(t){"use strict";function e(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}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)}}var r=function(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)},o="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},i="object"==typeof o&&o&&o.Object===Object&&o,a="object"==typeof self&&self&&self.Object===Object&&self,c=i||a||Function("return this")(),u=function(){return c.Date.now()},s=c.Symbol,f=Object.prototype,l=f.hasOwnProperty,b=f.toString,v=s?s.toStringTag:void 0;var d=function(t){var e=l.call(t,v),n=t[v];try{t[v]=void 0;var r=!0}catch(t){}var o=b.call(t);return r&&(e?t[v]=n:delete t[v]),o},y=Object.prototype.toString;var p=function(t){return y.call(t)},h="[object Null]",O="[object Undefined]",g=s?s.toStringTag:void 0;var S=function(t){return null==t?void 0===t?O:h:g&&g in Object(t)?d(t):p(t)};var j=function(t){return null!=t&&"object"==typeof t},m="[object Symbol]";var w=function(t){return"symbol"==typeof t||j(t)&&S(t)==m},E=NaN,T=/^\s+|\s+$/g,_=/^[-+]0x[0-9a-f]+$/i,L=/^0b[01]+$/i,$=/^0o[0-7]+$/i,k=parseInt;var x=function(t){if("number"==typeof t)return t;if(w(t))return E;if(r(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=r(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(T,"");var n=L.test(t);return n||$.test(t)?k(t.slice(2),n?2:8):_.test(t)?E:+t},P="Expected a function",D=Math.max,N=Math.min;var I=function(t,e,n){var o,i,a,c,s,f,l=0,b=!1,v=!1,d=!0;if("function"!=typeof t)throw new TypeError(P);function y(e){var n=o,r=i;return o=i=void 0,l=e,c=t.apply(r,n)}function p(t){var n=t-f;return void 0===f||n>=e||n<0||v&&t-l>=a}function h(){var t=u();if(p(t))return O(t);s=setTimeout(h,function(t){var n=e-(t-f);return v?N(n,a-(t-l)):n}(t))}function O(t){return s=void 0,d&&o?y(t):(o=i=void 0,c)}function g(){var t=u(),n=p(t);if(o=arguments,i=this,f=t,n){if(void 0===s)return function(t){return l=t,s=setTimeout(h,e),b?y(t):c}(f);if(v)return s=setTimeout(h,e),y(f)}return void 0===s&&(s=setTimeout(h,e)),c}return e=x(e)||0,r(n)&&(b=!!n.leading,a=(v="maxWait"in n)?D(x(n.maxWait)||0,e):a,d="trailing"in n?!!n.trailing:d),g.cancel=function(){void 0!==s&&clearTimeout(s),l=0,o=f=i=s=void 0},g.flush=function(){return void 0===s?c:O(u())},g},M="Expected a function";var C=function(t,e,n){var o=!0,i=!0;if("function"!=typeof t)throw new TypeError(M);return r(n)&&(o="leading"in n?!!n.leading:o,i="trailing"in n?!!n.trailing:i),I(t,e,{leading:o,maxWait:e,trailing:i})};function A(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function R(t,e){var n=[],r=t,o=Object.assign({},e),i=o.onSubscribe,a=o.onUnsubscribe,c=o.getValue;return A({get value(){return c?c():r},subscribe:function(t){return 0===n.length&&i&&i(),"function"==typeof t&&(t={next:t}),n.push(t),t.next(this.value),{unsubscribe:function(){0===(n=n.filter(function(e){return e!==t})).length&&a&&a()}}},next:function(t){r=t,n.map(function(t){return t.next(r)})}},Symbol&&Symbol.observable||"@@observable",function(){return this})}var U=Object.prototype.hasOwnProperty;function V(t,e){return t===e?0!==t||0!==e||1/t==1/e:t!=t&&e!=e}function X(t){var e=t.onState,n=t.onStateEvent,r=t.onConnectLib,o=t.onDisconnectLib,i={},a={};return{connectedLibs:{},connect:function(t,c){var u=this;if(this.connectedLibs[c])throw new Error("A library is already connected with key: ".concat(c));var s={disconnect:function(){o&&o(c,t),delete u.connectedLibs[c],i[c].unsubscribe(),delete i[c]}},f=r?r(c,t,s):s;return e&&(i[c]=t.state$.subscribe(function(n){return e(n,c,t)})),n&&(a[c]=t.stateEvent$.subscribe(function(e){return n(e,c,t)})),this.connectedLibs[c]=t,f},disconnect:function(){var t=this;Object.keys(this.connectedLibs).map(function(e){i[e]&&(i[e].unsubscribe(),delete i[e]),a[e]&&(a[e].unsubscribe(),delete a[e]),delete t.connectedLibs[e]})}}}var F="**__DEVTOOLS__**";!function(){var t,e,n={};window.__REDUX_DEVTOOLS_EXTENSION__&&((t=window.__REDUX_DEVTOOLS_EXTENSION__.connect({name:"StatedLibraries"})).subscribe(function(t){if("DISPATCH"===t.type&&t.state){console.log("DevTools requested to change the state to",t.state);var n=JSON.parse(t.state);Object.keys(n).forEach(function(t){return e.connectedLibs[t].resetState(n[t],F)})}}),t.init(n)),e=X({onStateEvent:function(e,r){var o=e.event;n[r]=e.rawState,o!==F&&t&&t.send("".concat(r,"::").concat(o),n)}})}();function J(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),r.forEach(function(e){A(t,e,n[e])})}return t}W={},q=X({onConnectLib:function(t,e,n){var r=function(t){try{var e=localStorage.getItem(t);return e?JSON.parse(e):void 0}catch(t){return}}(t);return null!=r&&e.resetState(r,"RESET_FROM_LOCAL_STATE"),W[t]=C(function(e){localStorage.setItem(t,JSON.stringify(e))},300,{leading:!0,trailing:!0}),J({},n,{clear:function(){return localStorage.removeItem(t)}})},onStateEvent:function(t,e){W[e](t.rawState)}}),J({},q,{clear:function(){Object.keys(this.connectedLibs).map(function(t){localStorage.removeItem(t)})}});var W,q,B=function(t){return t};function H(t,e,n,r){return{rawState:t,event:e,state:(r&&r.deriveState||B)(t),meta:n}}function z(t){for(var e=Object.getPrototypeOf(t),n=Object.getOwnPropertyDescriptors(e),r=0,o=Object.keys(n);r<o.length;r++){var i=o[r];"constructor"!==i&&("function"==typeof n[i].value&&(t[i]=t[i].bind(t)))}}var G=function(){function t(e,n){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.opts=void 0,this.stateEvent$=void 0,this.state$=void 0,this.opts=Object.assign({},n);var r=this.opts.createObs||R,o=H(e,"INIT",void 0,n);this.stateEvent$=r(o),this.state$=r(o.state)}var r,o,i;return r=t,i=[{key:"bindMethods",value:function(t){z(t)}}],(o=[{key:"setState",value:function(t,n,r){var o=function(t,e){if(V(t,e))return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;var n=Object.keys(t),r=Object.keys(e);if(n.length!==r.length)return!1;for(var o=0;o<n.length;o++)if(!U.call(e,n[o])||!V(t[n[o]],e[n[o]]))return!1;return!0}(t,this.stateEvent$.value.rawState)?function(t){for(var n=1;n<arguments.length;n++){var r=null!=arguments[n]?arguments[n]:{},o=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),o.forEach(function(n){e(t,n,r[n])})}return t}({},this.stateEvent$.value,{event:n,meta:r}):H(t,n,r,this.opts);Object.is(o.state,this.state$.value)||this.state$.next(o.state),this.stateEvent$.next(o)}},{key:"updateState",value:function(t,e,n){var r="function"==typeof t?t(this.state):t,o=Object.assign({},this.stateEvent$.value.rawState,r);this.setState(o,e,n)}},{key:"resetState",value:function(t,e,n){this.setState(t,e,n)}},{key:"state",get:function(){return this.stateEvent$.value.state}}])&&n(r.prototype,o),i&&n(r,i),t}();t.default=G,t.bindMethodsFromProto=z,t.StatedLibBase=G,t.createStatedLib=function(t,e,n){var r=new G(t,n);z(r);var o="function"==typeof e?e(r):e,i={get state(){return this.stateEvent$.value.state}};return Object.assign(i,r),Object.keys(o).forEach(function(t){"function"==typeof o[t]&&(i[t]=o[t].bind(i))}),i},Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=index.umd.production.js.map