{"name":"subscribe-ui-event","version":"3.0.0","description":"A single, throttle built-in solution to subscribe to browser UI Events.","main":"index.js","module":"index.es.js","scripts":{"clean":"rm -rf dist dist-es","dev":"grunt functional-debug","dist":"npm run clean && npm run dist:commonjs && npm run dist:es","dist:commonjs":"babel --env-name commonjs src -d dist","dist:es":"babel --env-name es src -d dist-es","format":"prettier --write .","format:check":"prettier --check .","func":"grunt functional","lint":"eslint . && npm run format:check","prepublish":"npm run dist","test":"nyc ./node_modules/.bin/mocha tests/unit --recursive --reporter spec"},"repository":{"type":"git","url":"git+https://github.com/yahoo/subscribe-ui-event.git"},"keywords":["addEventListener","browser","events","requestAnimationFrame","resize","scroll","subscribe","throttle"],"author":{"name":"Hank Hsiao","email":"hankxiao@yahoo-inc.com"},"dependencies":{"eventemitter3":"^5.0.0","raf":"^3.0.0"},"devDependencies":{"@babel/cli":"^7.14.5","@babel/core":"^7.14.6","@babel/plugin-transform-runtime":"^7.14.5","@babel/preset-env":"^7.14.7","@babel/preset-react":"^7.14.5","@babel/register":"^7.14.5","async":"^3.2.0","babel-plugin-add-module-exports":"^1.0.4","chai":"^4.3.4","es5-shim":"^4.0.0","eslint":"^8.0.1","eslint-plugin-import":"^2.25.2","eslint-plugin-jsx-a11y":"^6.4.1","eslint-plugin-react":"^7.24.0","grunt":"^1.0.0","grunt-atomizer":"^3.0.0","grunt-cli":"^1.0.0","grunt-contrib-clean":"^2.0.0","grunt-contrib-connect":"^5.0.0","grunt-contrib-watch":"^1.0.0","grunt-saucelabs":"^9.0.0","grunt-webpack":"^6.0.0","jsdom":"^25.0.0","mocha":"^10.0.0","nyc":"^17.0.0","pre-commit":"^1.0.0","prettier":"^3.0.0","prop-types":"^15.6.1","react":"^18.2.0","react-dom":"^18.2.0","webpack":"^5.44.0","webpack-dev-server":"^4.0.0"},"precommit":["lint","test"],"license":"BSD-3-Clause","prettier":{"singleQuote":true,"tabWidth":4},"browserslist":"> 0.25%, not dead","_id":"subscribe-ui-event@3.0.0","gitHead":"ee5c7abc71607425b9de7132ca307498d2d53dc2","bugs":{"url":"https://github.com/yahoo/subscribe-ui-event/issues"},"homepage":"https://github.com/yahoo/subscribe-ui-event#readme","_nodeVersion":"20.10.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-Rgop/8yYQjgK47HsPKSxGLJnL/n2ejtAl0PAW9Bvt1kt+pEDN9SB3RP5JVUPjjuYZoFcGb3MOFNIf06uIzrazg==","shasum":"18f7c3e87a5007bc79550b04d9f0f596277c20e4","tarball":"http://registry-npm.ywwl.com/subscribe-ui-event/download/subscribe-ui-event-3.0.0.tgz","fileCount":23,"unpackedSize":43912,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDbX8QNmWfHIyzh2NJKUcwzaqRFneM8eOHCcTduPggasAIhAMEBg1PDF+7k83jBq7MUME+YuaUFTcMA6uTCKm45kbYe"}],"size":9135,"noattachment":false,"key":"/subscribe-ui-event/-/subscribe-ui-event-3.0.0.tgz"},"_npmUser":{"name":"redonkulus","email":"seth@bertalotto.net"},"directories":{},"maintainers":[{"name":"redonkulus","email":"seth@bertalotto.net"},{"name":"src-code","email":"yasteve@yahoo-inc.com"},{"name":"kaesonho","email":"kaesonho@yahoo-inc.com"},{"name":"hankhsiao","email":"kidhank@gmail.com"},{"name":"roderickhsiao","email":"roderickhsiao@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/subscribe-ui-event_3.0.0_1734375319196_0.9348109306776609"},"_hasShrinkwrap":false,"readme":"# subscribe-ui-event\n\n[![npm version](https://badge.fury.io/js/subscribe-ui-event.svg)](http://badge.fury.io/js/subscribe-ui-event)\n![github actions](https://github.com/yahoo/subscribe-ui-event/actions/workflows/test.js.yml/badge.svg)\n\nWith `subscribe-ui-event`, instead of calling multiple `window.addEventListener('scroll', eventHandler);` by different components, call `subscribe('scroll', eventHandler)`. It will only add a single event listener and dispatch event to those who subscribe to the event via [eventemitter3](https://github.com/primus/EventEmitter3).\n\n## Install\n\n```bash\nnpm install subscribe-ui-event\n```\n\n## Single Event Listener v.s. Multiple Event Listeners\n\nWhy a single event? More performant and less memory consumption.\n\nThe [jsperf ](http://jsperf.com/subscribe-v-s-addeventlistener/2) runs 10 `addEventListener` and 10 non-throttling `subscribe`, and the outcome is that the ops/sec of `subscribe` is slightly less. But in regular case, you will use throttling `subscribe`, and it will be more performant.\n\n![comparison](https://cloud.githubusercontent.com/assets/2044960/9611594/6167df1c-5095-11e5-8abc-c81ff4d13ce6.png)\n\nFor 10 `addEventListener`, the difference of memory consumption between peak and trough is about 4.1K.\n\n![addEventListener](https://cloud.githubusercontent.com/assets/2044960/9611614/778bc452-5095-11e5-80d9-be9379df9956.png)\n\nFor 10 `subscribe`, the difference of memory consumption between peak and trough is about 1.0K.\n\n![subscribe](https://cloud.githubusercontent.com/assets/2044960/9611619/7c293652-5095-11e5-8d27-29a0d2d167cc.png)\n\n## Other Benefits\n\n1.  Throttling by default.\n2.  Access `document.body.scrollTop`, `window.innerWidth` once.\n3.  Provide `requestAnimationFrame` throttle for high performance.\n4.  Be able to use events like `scrollStart` (see below) those edge events.\n\n## API\n\n### subscribe\n\n```ts\nsubscribe(eventType: String, callback: Function, options: Object?): Subscription\n```\n\nProvide throttled version of `window` or `document` events, such like `scroll`, `resize`, `touch` and `visibilitychange` to subscribe, see below.\n\n**Note on IE8 or the below, the throttle will be turned off because the event object is global and will be deleted for setTimeout or rAF.**\n\nExample:\n\n```ts\nimport { subscribe } from 'subscribe-ui-event';\nfunction eventHandler (e, payload) {\n    // e is the native event object and\n    // payload is the additional information\n    ...\n}\n// 50ms throttle by default\nconst subscription = subscribe('scroll', eventHandler);\n// remove later\nsubscription.unsubscribe();\n```\n\n#### Optional Payload\n\nThe format of the payload is:\n\n```js\n{\n    type: <String>, // could be 'scroll', 'resize' ...\n    // you need to pass options.enableScrollInfo = true to subscribe to get the following data\n    scroll: {\n        top: <Number>, // The scroll position, i.g., document.body.scrollTop\n        delta: <Number> // The delta of scroll position, it is helpful for scroll direction\n    },\n    // you need to pass options.enableResizeInfo = true to subscribe to get the following data\n    resize: {\n        width: <Number>, // The client width\n        height: <Number> // The client height\n    },\n    // you need to pass options.enableTouchInfo = true to subscribe to get the following data\n    touch: {\n        axisIntention: <String>, // 'x', 'y', or ''.\n        startX: <Number>,\n        startY: <Number>,\n        deltaX: <Number>,\n        deltaY: <Number>\n    }\n}\n```\n\n#### Options\n\n`options.throttleRate` allows of changing the throttle rate, and the default value is 50 (ms). Set 0 for no throttle. **On IE8, there will be no throttle, because throttling will use setTimeout or rAF to achieve, and the event object passed into event handler will be overwritten.**\n\n`options.context` allows of setting the caller of callback function.\n\n`options.useRAF = true` allows of using `requestAnimationFrame` instead of `setTimeout`.\n\n`options.enableScrollInfo = true` allows of getting `scrollTop`.\n\n`options.enableResizeInfo = true` allows of getting `width` and `height` of client.\n\n`options.enableTouchInfo = true` allows of getting touch information (see above).\n\n`eventType` could be one of the following:\n\n1.  scroll - window.scoll\n2.  scrollStart - The start of window.scoll\n3.  scrollEnd - The end of window.scoll\n4.  resize - window.resize\n5.  resizeStart - The start window.resize\n6.  resizeEnd - The end window.resize\n7.  visibilitychange - document.visibilitychange (IE8 doesn't support)\n8.  touchmoveStart - The start of window.touchmove\n9.  touchmoveEnd - The end of window.touchmove\n10. touchmove - window.touchmove\n11. touchstart - window.touchstart\n12. touchend - window.touchend\n\n`options.eventOptions`: An options object that specifies characteristics about the event listener (if passive event is supported by the browser)\n\n### unsubscribe\n\n```js\nunsubscribe(eventType: String, callback: Function): Void\n```\n\nUnsubscribe to an event listener, we suggest you use `subscription.unsubscribe()`, because it may accidentally unsubscribe those events having the same `eventType` and `callback`, but different `throttleRate`.\n\n## Credits\n\n-   This library runs full browser test suite using Sauce Labs.\n\n## License\n\nThis software is free to use under the BSD license.\nSee the [LICENSE file](./LICENSE.md) for license text and copyright information.\n","publish_time":1734375319494,"_cnpm_publish_time":1734375319494,"dist-tags":{"latest":"3.0.0"}}