@smoovy/listener
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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

Package Sidebar

Install

npm i @smoovy/listener

Weekly Downloads

19

Version

1.0.2

License

MIT

Unpacked Size

5.07 kB

Total Files

10

Last publish

Collaborators

  • davideperozzi