@smoovy/listener
v1.0.2
Published
Helpers for listening when it gets too verbose to hear
Downloads
118
Maintainers
Readme
@smoovy/listener
Very simple subscribe/unsubscribe system.
Installation
yarn add @smoovy/listener
or
npm install --save @smoovy/listener
Usage
Listen to elements
import { listen } from '@smoovy/listener';
// single type
listen(window, 'mousemove', event => console.log(event));
// multi type
listen(document.body, ['mouseup', 'mousecancel'], event => console.log(event));
Unlistening from events
const unlisten = listen(window, 'mousemove', event => console.log(event));
// to unlisten from window mousemove simply call the unlisten function
unlisten();
In case you have many listeners and want to unsubscribe/unlisten from all at once,
you can use the listenCompose
helper function:
import { listen, listenCompose } from '@smoovy/listener';
const unlisten = listenCompose(
listen(window, 'mousemove', event => console.log(event)),
listen(window, 'click', event => console.log(event)),
// custom callback on unlisten
() => console.log('all listeners are now inactive')
);
unlisten();
License
See the LICENSE file for license rights and limitations (MIT).