Options
All
  • Public
  • Public/Protected
  • All
Menu

tp-register-listener

Build Status Coverage Status npm version monthly downloads code style: prettier semantic-release UNPKG liense

Intro

Built to use modern event listener options register a listener and return a method to remove the listener.

Example

import registerListener from 'tp-register-listener';

const handler = () => {
  // do something
};
const body = document.body;
const unReg = registerListener(body, 'click', handler);
// remove this listener
unReg();

Install

NPM Badge

API

registerListener(ele, eventName, callback, opts, unRegisterListenersCollection)

Returns a unRegister function to remove the listener.

  • @param {HTMLElement} ele - Elements to binding event;
  • @param {string} eventName - The event name;
  • @param {EventListener} callback - The event listener;
  • @param {EventListenerOptions} [opts={}] - The event listener options;
  • @param {Function[]} [unRegisterListenersCollection] - The collections of unRegister listeners.
const unRegisterListenersCollection = []

const handler = () => {
  // empty
}
const ele = document.body
registerListener(ele, 'click', handler, {}, unRegisterListenersCollection)
registerListener(ele, 'click', handler, {}, unRegisterListenersCollection)
registerListener(ele, 'click', handler, {}, unRegisterListenersCollection)

// remove all listeners.
unRegisterListenersCollection.forEach(fn => fn())

registerListener._uiEvtOpts

  • @return {boolean} - Return true when the environment support "passive" EventListenerOptions
if((registerListener as any)._uiEvtOpts) {
  // code here
}

Development

  • npm t: Run test suite
  • npm start: Run npm run build in watch mode
  • npm run test:watch: Run test suite in interactive watch mode
  • npm run test:prod: Run linting and generate coverage
  • npm run build: Generate bundles and typings, create docs
  • npm run lint: Lints code
  • npm run commit: Commit using conventional commit style (husky will tell you to use it if you haven't :wink:)

License

MIT

Index

Interfaces

Variables

Functions

Variables

_uiEvtOpts

_uiEvtOpts: boolean = false

Functions

NOOP

  • NOOP(): void

registerListener

  • registerListener(ele: any, eventName: string, callback: EventListener, opts?: EventListenerOptions, unRegisterListenersCollection?: Function[]): Function

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc