@stated-library/core
Version:
Core functionality for [`Stated Libraries`](https://github.com/bradfordlemley/stated-library)
3 lines (2 loc) • 6.81 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/core"]={})}(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,n){var r=[],o=t,i=Object.assign({},n),u=i.onSubscribe,c=i.onUnsubscribe,a=i.getValue;return e({get value(){return a?a():o},subscribe:function(t){return 0===r.length&&u&&u(),"function"==typeof t&&(t={next:t}),r.push(t),t.next(this.value),{unsubscribe:function(){0===(r=r.filter(function(e){return e!==t})).length&&c&&c()}}},next:function(t){o=t,r.map(function(t){return t.next(o)})}},Symbol&&Symbol.observable||"@@observable",function(){return this})}function r(t){return"[object Array]"===Object.prototype.toString.call(t)}function o(t){if(t.hasOwnProperty("value"))return t.value;var e=void 0;return t.subscribe(function(t){return e=t}).unsubscribe(),e}function i(t){if(r(t)){var e=[];return t.map(function(t,n){e[n]=o(t)}),e}return o(t)}var u=Object.prototype.hasOwnProperty;function c(t,e){return t===e?0!==t||0!==e||1/t==1/e:t!=t&&e!=e}function a(t,e){if(c(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])||!c(t[n[o]],e[n[o]]))return!1;return!0}function f(t){var e=t.onState,n=t.onStateEvent,r=t.onConnectLib,o=t.onDisconnectLib,i={},u={};return{connectedLibs:{},connect:function(t,c){var a=this;if(this.connectedLibs[c])throw new Error("A library is already connected with key: ".concat(c));var f={disconnect:function(){o&&o(c,t),delete a.connectedLibs[c],i[c].unsubscribe(),delete i[c]}},s=r?r(c,t,f):f;return e&&(i[c]=t.state$.subscribe(function(n){return e(n,c,t)})),n&&(u[c]=t.stateEvent$.subscribe(function(e){return n(e,c,t)})),this.connectedLibs[c]=t,s},disconnect:function(){var t=this;Object.keys(this.connectedLibs).map(function(e){i[e]&&(i[e].unsubscribe(),delete i[e]),u[e]&&(u[e].unsubscribe(),delete u[e]),delete t.connectedLibs[e]})}}}var s="**__DEVTOOLS__**";function l(){var t,e,n={};return 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],s)})}}),t.init(n)),e=f({onStateEvent:function(e,r){var o=e.event;n[r]=e.rawState,o!==s&&t&&t.send("".concat(r,"::").concat(o),n)}})}var b=l();function v(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}var d=function(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)},p="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},y="object"==typeof p&&p&&p.Object===Object&&p,g="object"==typeof self&&self&&self.Object===Object&&self,O=y||g||Function("return this")(),S=function(){return O.Date.now()},m=O.Symbol,h=Object.prototype,j=h.hasOwnProperty,w=h.toString,E=m?m.toStringTag:void 0;var T=function(t){var e=j.call(t,E),n=t[E];try{t[E]=void 0;var r=!0}catch(t){}var o=w.call(t);return r&&(e?t[E]=n:delete t[E]),o},_=Object.prototype.toString;var L=function(t){return _.call(t)},x="[object Null]",D="[object Undefined]",N=m?m.toStringTag:void 0;var P=function(t){return null==t?void 0===t?D:x:N&&N in Object(t)?T(t):L(t)};var I=function(t){return null!=t&&"object"==typeof t},V="[object Symbol]";var k=function(t){return"symbol"==typeof t||I(t)&&P(t)==V},A=NaN,C=/^\s+|\s+$/g,$=/^[-+]0x[0-9a-f]+$/i,U=/^0b[01]+$/i,M=/^0o[0-7]+$/i,R=parseInt;var X=function(t){if("number"==typeof t)return t;if(k(t))return A;if(d(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=d(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(C,"");var n=U.test(t);return n||M.test(t)?R(t.slice(2),n?2:8):$.test(t)?A:+t},J="Expected a function",W=Math.max,q=Math.min;var F=function(t,e,n){var r,o,i,u,c,a,f=0,s=!1,l=!1,b=!0;if("function"!=typeof t)throw new TypeError(J);function v(e){var n=r,i=o;return r=o=void 0,f=e,u=t.apply(i,n)}function p(t){var n=t-a;return void 0===a||n>=e||n<0||l&&t-f>=i}function y(){var t=S();if(p(t))return g(t);c=setTimeout(y,function(t){var n=e-(t-a);return l?q(n,i-(t-f)):n}(t))}function g(t){return c=void 0,b&&r?v(t):(r=o=void 0,u)}function O(){var t=S(),n=p(t);if(r=arguments,o=this,a=t,n){if(void 0===c)return function(t){return f=t,c=setTimeout(y,e),s?v(t):u}(a);if(l)return c=setTimeout(y,e),v(a)}return void 0===c&&(c=setTimeout(y,e)),u}return e=X(e)||0,d(n)&&(s=!!n.leading,i=(l="maxWait"in n)?W(X(n.maxWait)||0,e):i,b="trailing"in n?!!n.trailing:b),O.cancel=function(){void 0!==c&&clearTimeout(c),f=0,r=a=o=c=void 0},O.flush=function(){return void 0===c?u:g(S())},O},H="Expected a function";var z=function(t,e,n){var r=!0,o=!0;if("function"!=typeof t)throw new TypeError(H);return d(n)&&(r="leading"in n?!!n.leading:r,o="trailing"in n?!!n.trailing:o),F(t,e,{leading:r,maxWait:e,trailing:o})};function B(){var t={},e=f({onConnectLib:function(e,n,r){var o=function(t){try{var e=localStorage.getItem(t);return e?JSON.parse(e):void 0}catch(t){return}}(e);null!=o&&n.resetState(o,"RESET_FROM_LOCAL_STATE");return t[e]=z(function(t){localStorage.setItem(e,JSON.stringify(t))},300,{leading:!0,trailing:!0}),v({},r,{clear:function(){return localStorage.removeItem(e)}})},onStateEvent:function(e,n){t[n](e.rawState)}});return v({},e,{clear:function(){Object.keys(this.connectedLibs).map(function(t){localStorage.removeItem(t)})}})}var G=B();t.createObservable=n,t.isArray=r,t.getValue=o,t.getValueOrValues=i,t.shallowEqual=a,t.mapState=function(t,e,o){var u,c,f,s=!1,l=Object.assign({},o);function b(){arguments.length>0&&void 0!==arguments[0]&&arguments[0]&&(u=i(t));var n=function(t,e){return e(t)}(u,e);return a(n,c)||(c=n),c}var v=n(b(!0),{onUnsubscribe:function(){f.map(function(t){return t.unsubscribe()}),s=!1},onSubscribe:function(){b(!0),f=r(t)?t.map(function(t,e){return u[e]=t.value,t.subscribe(function(t){u[e]=t,d()})}):[t.subscribe(function(t){u=t,d()})],s=!0},getValue:function(){return s?c:b(!0)}});function d(){var t=c,e=b();e!==t&&function(t){l.async?setTimeout(function(){t===c&&v.next(c)},0):v.next(c)}(e)}return v},t.createDevToolsConnector=l,t.devTools=b,t.createLocalStorageConnector=B,t.locStorage=G,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=index.umd.production.js.map