1.0.2 • Published 1 year ago

@smoovy/listener v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@smoovy/listener

Version Size

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).