UNPKG

6.58 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("prop-types"),require("react")):"function"==typeof define&&define.amd?define(["exports","prop-types","react"],t):t(e.EventStack={},e.PropTypes,e.React)}(this,function(e,t,n){"use strict";function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function s(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function a(e,t,n){return t&&s(e.prototype,t),n&&s(e,n),e}function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function u(e){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function l(e,t){return(l=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function c(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}var h,d=(function(e){var t,n;t=!("undefined"==typeof window||!window.document||!window.document.createElement),n={canUseDOM:t,canUseWorkers:"undefined"!=typeof Worker,canUseEventListeners:t&&!(!window.addEventListener&&!window.attachEvent),canUseViewport:t&&!!window.screen},e.exports?e.exports=n:window.ExecutionEnvironment=n}(h={exports:{}},h.exports),h.exports).canUseDOM,f=function(){function e(t){o(this,e),i(this,"handlers",void 0),this.handlers=t.slice(0)}return a(e,[{key:"addHandlers",value:function(t){for(var n=this.handlers.slice(0),r=t.length,o=0;o<r;o+=1)n.push(t[o]);return new e(n)}},{key:"dispatchEvent",value:function(e,t){var n=this.handlers.length-1;if(t){for(var r=n;r>=0;r-=1)this.handlers[r].called||(this.handlers[r].called=!0,this.handlers[r](e));for(var o=n;o>=0;o-=1)this.handlers[o].called=!1}else{(0,this.handlers[n])(e)}}},{key:"hasHandlers",value:function(){return this.handlers.length>0}},{key:"removeHandlers",value:function(t){for(var n=[],r=this.handlers.length,o=0;o<r;o+=1){var s=this.handlers[o];-1===t.indexOf(s)&&n.push(s)}return new e(n)}}]),e}();function p(e){var t=new Map;return e.forEach(function(e,n){t.set(n,e)}),t}function v(e){return Array.isArray(e)?e:[e]}var y=function(e){return null!==e&&"object"===r(e)&&e.hasOwnProperty("current")};function m(e){return"document"===e?document:"window"===e?window:y(e)?e.current||document:e||document}var g=function(){function e(t,n){o(this,e),i(this,"handlerSets",void 0),i(this,"poolName",void 0),this.handlerSets=n,this.poolName=t}return a(e,[{key:"addHandlers",value:function(t,n){var r=p(this.handlerSets);if(r.has(t)){var o=r.get(t);r.set(t,o.addHandlers(n))}else r.set(t,new f(n));return new e(this.poolName,r)}},{key:"dispatchEvent",value:function(e,t){var n=this.handlerSets.get(e),r="default"===this.poolName;n&&n.dispatchEvent(t,r)}},{key:"hasHandlers",value:function(e){if(!e)return this.handlerSets.size>0;var t=this.handlerSets.get(e);return!!t&&t.hasHandlers()}},{key:"removeHandlers",value:function(t,n){var r=p(this.handlerSets);if(!r.has(t))return new e(this.poolName,r);var o=r.get(t).removeHandlers(n);return o.hasHandlers()?r.set(t,o):r.delete(t),new e(this.poolName,r)}}]),e}();i(g,"createByType",function(e,t,n){var r=new Map;return r.set(t,new f(n)),new g(e,r)});var b=function(){function e(t){var n=this;o(this,e),i(this,"handlers",new Map),i(this,"pools",new Map),i(this,"target",void 0),i(this,"createEmitter",function(e){return function(t){n.pools.forEach(function(n){n.dispatchEvent(e,t)})}}),this.target=t}return a(e,[{key:"addHandlers",value:function(e,t,n){if(this.pools.has(e)){var r=this.pools.get(e);this.pools.set(e,r.addHandlers(t,n))}else this.pools.set(e,g.createByType(e,t,n));this.handlers.has(t)||this.addTargetHandler(t)}},{key:"hasHandlers",value:function(){return this.handlers.size>0}},{key:"removeHandlers",value:function(e,t,n){if(this.pools.has(e)){var r=this.pools.get(e).removeHandlers(t,n);r.hasHandlers()?this.pools.set(e,r):this.pools.delete(e);var o=!1;this.pools.forEach(function(e){return o=o||e.hasHandlers(t)}),o||this.removeTargetHandler(t)}}},{key:"addTargetHandler",value:function(e){var t=this.createEmitter(e);this.handlers.set(e,t),this.target.addEventListener(e,t,!0)}},{key:"removeTargetHandler",value:function(e){this.handlers.has(e)&&(this.target.removeEventListener(e,this.handlers.get(e),!0),this.handlers.delete(e))}}]),e}(),w=new(function(){function e(){var t=this;o(this,e),i(this,"targets",new Map),i(this,"getTarget",function(e){var n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],r=m(e);if(t.targets.has(r))return t.targets.get(r);if(!n)return null;var o=new b(r);return t.targets.set(r,o),o}),i(this,"removeTarget",function(e){t.targets.delete(m(e))})}return a(e,[{key:"sub",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(d){var r=n.target,o=void 0===r?document:r,s=n.pool,a=void 0===s?"default":s;this.getTarget(o).addHandlers(a,e,v(t))}}},{key:"unsub",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(d){var r=n.target,o=void 0===r?document:r,s=n.pool,a=void 0===s?"default":s,i=this.getTarget(o,!1);i&&(i.removeHandlers(a,e,v(t)),i.hasHandlers()||this.removeTarget(o))}}}]),e}()),k=function(e){function t(){return o(this,t),c(this,u(t).apply(this,arguments))}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&l(e,t)}(t,n.PureComponent),a(t,[{key:"componentDidMount",value:function(){this.subscribe(this.props)}},{key:"componentDidUpdate",value:function(e){this.unsubscribe(e),this.subscribe(this.props)}},{key:"componentWillUnmount",value:function(){this.unsubscribe(this.props)}},{key:"subscribe",value:function(e){var t=e.name,n=e.on,r=e.pool,o=e.target;w.sub(t,n,{pool:r,target:o})}},{key:"unsubscribe",value:function(e){var t=e.name,n=e.on,r=e.pool,o=e.target;w.unsub(t,n,{pool:r,target:o})}},{key:"render",value:function(){return null}}]),t}();i(k,"defaultProps",{pool:"default",target:"document"}),k.propTypes={},e.instance=w,e.default=k,Object.defineProperty(e,"__esModule",{value:!0})});