UNPKG

@stated-library/react

Version:

React bindings for [`Stated Libraries`](https://github.com/bradfordlemley/stated-library)

3 lines (2 loc) 8.35 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e((t=t||self)["@stated-library/react"]={},t.React)}(this,function(t,e){"use strict";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)}}function r(t){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t){return(o="function"==typeof Symbol&&"symbol"===r(Symbol.iterator)?function(t){return r(t)}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":r(t)})(t)}function i(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function c(t){return(c=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function u(t,e){return(u=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}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 f(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}var l=function(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)},s="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},b="object"==typeof s&&s&&s.Object===Object&&s,y="object"==typeof self&&self&&self.Object===Object&&self,p=b||y||Function("return this")(),v=function(){return p.Date.now()},d=p.Symbol,m=Object.prototype,O=m.hasOwnProperty,h=m.toString,S=d?d.toStringTag:void 0;var g=function(t){var e=O.call(t,S),n=t[S];try{t[S]=void 0;var r=!0}catch(t){}var o=h.call(t);return r&&(e?t[S]=n:delete t[S]),o},w=Object.prototype.toString;var j=function(t){return w.call(t)},E="[object Null]",_="[object Undefined]",T=d?d.toStringTag:void 0;var L=function(t){return null==t?void 0===t?_:E:T&&T in Object(t)?g(t):j(t)};var P=function(t){return null!=t&&"object"==typeof t},k="[object Symbol]";var x=function(t){return"symbol"==typeof t||P(t)&&L(t)==k},D=NaN,N=/^\s+|\s+$/g,I=/^[-+]0x[0-9a-f]+$/i,C=/^0b[01]+$/i,R=/^0o[0-7]+$/i,$=parseInt;var A=function(t){if("number"==typeof t)return t;if(x(t))return D;if(l(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=l(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(N,"");var n=C.test(t);return n||R.test(t)?$(t.slice(2),n?2:8):I.test(t)?D:+t},M="Expected a function",U=Math.max,W=Math.min;var X=function(t,e,n){var r,o,i,c,u,a,f=0,s=!1,b=!1,y=!0;if("function"!=typeof t)throw new TypeError(M);function p(e){var n=r,i=o;return r=o=void 0,f=e,c=t.apply(i,n)}function d(t){var n=t-a;return void 0===a||n>=e||n<0||b&&t-f>=i}function m(){var t=v();if(d(t))return O(t);u=setTimeout(m,function(t){var n=e-(t-a);return b?W(n,i-(t-f)):n}(t))}function O(t){return u=void 0,y&&r?p(t):(r=o=void 0,c)}function h(){var t=v(),n=d(t);if(r=arguments,o=this,a=t,n){if(void 0===u)return function(t){return f=t,u=setTimeout(m,e),s?p(t):c}(a);if(b)return u=setTimeout(m,e),p(a)}return void 0===u&&(u=setTimeout(m,e)),c}return e=A(e)||0,l(n)&&(s=!!n.leading,i=(b="maxWait"in n)?U(A(n.maxWait)||0,e):i,y="trailing"in n?!!n.trailing:y),h.cancel=function(){void 0!==u&&clearTimeout(u),f=0,r=a=o=u=void 0},h.flush=function(){return void 0===u?c:O(v())},h},J="Expected a function";var V=function(t,e,n){var r=!0,o=!0;if("function"!=typeof t)throw new TypeError(J);return l(n)&&(r="leading"in n?!!n.leading:r,o="trailing"in n?!!n.trailing:o),X(t,e,{leading:r,maxWait:e,trailing:o})};function q(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function F(t){if(t.hasOwnProperty("value"))return t.value;var e=void 0;return t.subscribe(function(t){return e=t}).unsubscribe(),e}function H(t){var e=t.onState,n=t.onStateEvent,r=t.onConnectLib,o=t.onDisconnectLib,i={},c={};return{connectedLibs:{},connect:function(t,u){var a=this;if(this.connectedLibs[u])throw new Error("A library is already connected with key: ".concat(u));var f={disconnect:function(){o&&o(u,t),delete a.connectedLibs[u],i[u].unsubscribe(),delete i[u]}},l=r?r(u,t,f):f;return e&&(i[u]=t.state$.subscribe(function(n){return e(n,u,t)})),n&&(c[u]=t.stateEvent$.subscribe(function(e){return n(e,u,t)})),this.connectedLibs[u]=t,l},disconnect:function(){var t=this;Object.keys(this.connectedLibs).map(function(e){i[e]&&(i[e].unsubscribe(),delete i[e]),c[e]&&(c[e].unsubscribe(),delete c[e]),delete t.connectedLibs[e]})}}}var z="**__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],z)})}}),t.init(n)),e=H({onStateEvent:function(e,r){var o=e.event;n[r]=e.rawState,o!==z&&t&&t.send("".concat(r,"::").concat(o),n)}})}();function B(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){q(t,e,n[e])})}return t}var G,K;G={},K=H({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"),G[t]=V(function(e){localStorage.setItem(t,JSON.stringify(e))},300,{leading:!0,trailing:!0}),B({},n,{clear:function(){return localStorage.removeItem(t)}})},onStateEvent:function(t,e){G[e](t.rawState)}}),B({},K,{clear:function(){Object.keys(this.connectedLibs).map(function(t){localStorage.removeItem(t)})}});function Q(t,e){var n,r=t,o=F(e);return r.state=f({},r.state||{},o),{connect:function(){n=e.subscribe(function(t){Object.is(t,o)||(o=t,r.setState(f({},t)))})},disconnect:function(){n.unsubscribe(),r=null}}}function Y(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=[],r=!0,o=!1,i=void 0;try{for(var c,u=t[Symbol.iterator]();!(r=(c=u.next()).done)&&(n.push(c.value),!e||n.length!==e);r=!0);}catch(t){o=!0,i=t}finally{try{r||null==u.return||u.return()}finally{if(o)throw i}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}t.connect=function(t){return function(r){var a,f,l;return f=a=function(a){function f(e){var n,r,u;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,f),r=this,(n=!(u=c(f).call(this,e))||"object"!==o(u)&&"function"!=typeof u?i(r):u).link=void 0,n.state=void 0,n.link=Q(i(n),t),n}var l,s,b;return 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&&u(t,e)}(f,e.Component),l=f,(s=[{key:"componentDidMount",value:function(){this.link.connect()}},{key:"componentWillUnmount",value:function(){this.link.disconnect()}},{key:"render",value:function(){return e.createElement(r,Object.assign({},this.props,this.state))}}])&&n(l.prototype,s),b&&n(l,b),f}(),a.displayName="connect-sl(".concat((l=r).displayName||l.name||"Component",")"),f}},t.link=Q,t.use=function(t){var n=e.useRef(void 0);if(void 0===n.current&&(n.current="function"==typeof t?t():t,!n.current))throw new Error("Invalid value$: ".concat(n.current));var r=n.current,o=Y(e.useState(function(){return F(r)}),2),i=o[0],c=o[1];return e.useEffect(function(){var t=r.subscribe(c);return function(){return t.unsubscribe()}},[r]),i},Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=index.umd.production.js.map