react-event-hook
Version:
A library for emitting and listening to events in a React application
1 lines • 7.42 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define("react-event-hook",["React"],t):"object"==typeof exports?exports["react-event-hook"]=t(require("react")):e["react-event-hook"]=t(e.React)}(this,(e=>(()=>{"use strict";var t={729:e=>{var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function i(e,t,r,i,a){if("function"!=typeof r)throw new TypeError("The listener must be a function");var c=new o(r,i||e,a),u=n?n+t:t;return e._events[u]?e._events[u].fn?e._events[u]=[e._events[u],c]:e._events[u].push(c):(e._events[u]=c,e._eventsCount++),e}function a(e,t){0==--e._eventsCount?e._events=new r:delete e._events[t]}function c(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),c.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},c.prototype.listeners=function(e){var t=n?n+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var o=0,i=r.length,a=new Array(i);o<i;o++)a[o]=r[o].fn;return a},c.prototype.listenerCount=function(e){var t=n?n+e:e,r=this._events[t];return r?r.fn?1:r.length:0},c.prototype.emit=function(e,t,r,o,i,a){var c=n?n+e:e;if(!this._events[c])return!1;var u,s,f=this._events[c],l=arguments.length;if(f.fn){switch(f.once&&this.removeListener(e,f.fn,void 0,!0),l){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,t),!0;case 3:return f.fn.call(f.context,t,r),!0;case 4:return f.fn.call(f.context,t,r,o),!0;case 5:return f.fn.call(f.context,t,r,o,i),!0;case 6:return f.fn.call(f.context,t,r,o,i,a),!0}for(s=1,u=new Array(l-1);s<l;s++)u[s-1]=arguments[s];f.fn.apply(f.context,u)}else{var v,p=f.length;for(s=0;s<p;s++)switch(f[s].once&&this.removeListener(e,f[s].fn,void 0,!0),l){case 1:f[s].fn.call(f[s].context);break;case 2:f[s].fn.call(f[s].context,t);break;case 3:f[s].fn.call(f[s].context,t,r);break;case 4:f[s].fn.call(f[s].context,t,r,o);break;default:if(!u)for(v=1,u=new Array(l-1);v<l;v++)u[v-1]=arguments[v];f[s].fn.apply(f[s].context,u)}}return!0},c.prototype.on=function(e,t,n){return i(this,e,t,n,!1)},c.prototype.once=function(e,t,n){return i(this,e,t,n,!0)},c.prototype.removeListener=function(e,t,r,o){var i=n?n+e:e;if(!this._events[i])return this;if(!t)return a(this,i),this;var c=this._events[i];if(c.fn)c.fn!==t||o&&!c.once||r&&c.context!==r||a(this,i);else{for(var u=0,s=[],f=c.length;u<f;u++)(c[u].fn!==t||o&&!c[u].once||r&&c[u].context!==r)&&s.push(c[u]);s.length?this._events[i]=1===s.length?s[0]:s:a(this,i)}return this},c.prototype.removeAllListeners=function(e){var t;return e?(t=n?n+e:e,this._events[t]&&a(this,t)):(this._events=new r,this._eventsCount=0),this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prefixed=n,c.EventEmitter=c,e.exports=c},639:t=>{t.exports=e}},n={};function r(e){var o=n[e];if(void 0!==o)return o.exports;var i=n[e]={exports:{}};return t[e](i,i.exports,r),i.exports}r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{function e(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}r.r(o),r.d(o,{createEvent:()=>L});var t,n=r(729),i=r.n(n),a=r(639),c=!("undefined"==typeof window||null===(t=window.document)||void 0===t||!t.createElement),u=c?a.useLayoutEffect:a.useEffect,s=function(e){var t=(0,a.useRef)((function(){throw new Error("Cannot call an event handler during the first render.")}));return u((function(){t.current=e})),(0,a.useCallback)((function(){for(var e,n=arguments.length,r=new Array(n),o=0;o<n;o++)r[o]=arguments[o];return null===(e=t.current)||void 0===e?void 0:e.apply(null,r)}),[])},f=function(e){var t=s(e);u((function(){return window.addEventListener("storage",t),function(){window.removeEventListener("storage",t)}}),[t])},l=function(e){return Object.prototype.toString.call(e).slice(8,-1).toLowerCase()},v="react-event-hook:undefined",p=new Error("The input value could not be serialized"),h=function e(t){try{var n=l(t),r=JSON.stringify(t,(function(e,t){return void 0===t?v:t})),o=d(r);if(l(o)!==n)throw p;return"object"===n&&Object.values(t).forEach((function(t){return e(t)})),"array"===n&&t.forEach((function(t){return e(t)})),r}catch(e){throw p}},d=function(e){return JSON.parse(e,(function(e,t){if(t!==v)return t}))},y=function(){return"".concat(Date.now(),":").concat(parseInt(Math.random().toString().replace(".",""),10))},w=function(e,t){return h({id:y(),name:e,payload:t})},b=function(e){var t=d(e);return{name:t.name,payload:t.payload}},m="react-event-hook";function g(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function _(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,o,i=[],a=!0,c=!1;try{for(n=n.call(e);!(a=(r=n.next()).done)&&(i.push(r.value),!t||i.length!==t);a=!0);}catch(e){c=!0,o=e}finally{try{a||null==n.return||n.return()}finally{if(c)throw o}}return i}}(e,t)||function(e,t){if(e){if("string"==typeof e)return g(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?g(e,t):void 0}}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var x=function(e){var t;return[null===(t=e[0])||void 0===t?void 0:t.toUpperCase(),e.slice(1)].join("")},j=["-","_",":","/","."," "],O=function(e){return x(e.toLowerCase())},S=function e(t){var n=_(t.split(new RegExp("[".concat(j.join(""),"](.*)"))),2),r=n[0],o=n[1];return void 0!==r&&void 0!==o?"".concat(O(r)).concat(e(o)):function(e){return e.toUpperCase()===e&&e.toLowerCase()!==e}(t)?O(t):x(t)},E=new(i()),C=new Map,L=function(t){return function(){var n,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},o=r.crossTab,i=void 0!==o&&o,a=t.trim(),l=S(a),v="use".concat(l,"Listener"),p="emit".concat(l),h=y(),d=function(){C.has(a)&&C.get(a),C.set(a,h)},g=function(e){var t=s(e);f((function(t){if(i&&t.newValue&&t.key===m){var n=b(t.newValue);n.name===a&&e(n.payload)}})),u((function(){return E.addListener(a,t),function(){E.removeListener(a,t)}}),[t])},_=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.broadcast,r=void 0!==n&&n;if(c&&(d(),r||E.emit(a,e),i))try{var o=w(a,e);try{window.localStorage.setItem(m,o)}catch(e){}}catch(e){throw new Error('Could not emit "'.concat(a,'" event. The event payload might contain values that cannot be serialized.'))}},x=function(e){return _(e)};return x.broadcast=function(e){return _(e,{broadcast:!0})},e(n={},v,g),e(n,p,x),n}}})(),o})()));