# one-listener

> more control over global event listeners

Latest version **0.5.1** (published 2016-09-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install one-listener
pnpm add one-listener
yarn add one-listener
bun add one-listener
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2016-09-10 |
| First published | 2016-06-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | pixelass |

## Links

- npm: https://www.npmjs.com/package/one-listener
- Repository: https://github.com/pixelass/one-listener
- Homepage: https://github.com/pixelass/one-listener#readme
- Issues: https://github.com/pixelass/one-listener/issues
- npm.io page: https://npm.io/package/one-listener

## Dependencies (1)

- [throttle-debounce](https://npm.io/package/throttle-debounce.md) ^1.0.1

## Recent versions

- 0.5.1 (latest) — 2016-09-10
- 0.5.0 — 2016-07-12
- 0.4.16 — 2016-06-28
- 0.4.15 — 2016-06-28
- 0.4.14 — 2016-06-28
- 0.4.13 — 2016-06-28
- 0.4.12 — 2016-06-28
- 0.4.11 — 2016-06-28
- 0.4.10 — 2016-06-26
- 0.4.9 — 2016-06-25
- 0.4.8 — 2016-06-25
- 0.4.7 — 2016-06-24
- 0.4.6 — 2016-06-24
- 0.4.4 — 2016-06-24
- 0.4.3 — 2016-06-24
- … 7 more at https://npm.io/package/one-listener/versions

## README

# one-listener


big project?  
many contributors?  

too many event listeners on `window`?

This library aims to gather all handlers in one listener. This way the number of listeners are reduced.  
Performance intense listeners are wrapped in a `requestAnimationFrame`.  
If a certain limit of handlers is reached events will be throttled.
Everything is configurable. You can even create your own module and lock certain features.

available listeners

* scroll
* resize
* mousewheel
* mousemove
* mouseup (default behavior)
* touchmove
* touchend (default behavior)

## API

A detailed documentation can be found [here](http://pixelass.github.io/one-listener/docs)  
You can also take a look at the [examples](https://github.com/pixelass/one-listener/blob/master/examples/examples.src.js)


## Pride

[![npm](https://img.shields.io/npm/v/one-listener.svg)](https://www.npmjs.com/package/one-listener)
[![Coveralls branch](https://img.shields.io/coveralls/pixelass/one-listener.svg)](https://coveralls.io/github/pixelass/one-listener)
[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)  
[![Travis](https://img.shields.io/travis/pixelass/one-listener.svg)](https://travis-ci.org/pixelass/one-listener)
[![David](https://img.shields.io/david/pixelass/one-listener.svg)](https://david-dm.org/pixelass/one-listener)
[![David](https://img.shields.io/david/dev/pixelass/one-listener.svg)](https://david-dm.org/pixelass/one-listener#info=devDependencies&view=table)  
[![GitHub license](https://img.shields.io/github/license/pixelass/one-listener.svg)](https://github.com/pixelass/one-listener/blob/master/LICENSE)
[![GitHub issues](https://img.shields.io/github/issues/pixelass/one-listener.svg)](https://github.com/pixelass/one-listener/issues)
[![GitHub forks](https://img.shields.io/github/forks/pixelass/one-listener.svg)](https://github.com/pixelass/one-listener/network)
[![GitHub stars](https://img.shields.io/github/stars/pixelass/one-listener.svg)](https://github.com/pixelass/one-listener/stargazers)  

## installation
```bash
npm install one-listener
```

## Examples

Look at the examples folder for a detailed example

### simple

```js
import OneListener from 'one-listener';

const one = new OneListener({
    limit: 6,
    throttle: 200
});
const {requestEventListener, cancelEventListener} = one;
```

### API
```js
import OneListener from 'one-listener';
const one = new OneListener()
const {requestEventListener, cancelEventListener} = one;


// request mousemove 
const stopMoveTracking = requestEventListener('mousemove', (e) => {
    console.log({
        x: e.pageX,
        y: e.pageY
    });
});

// request scroll
// and cancel mousemove on condition 
const trackScroll = (e) => {
    console.log(window.scrollY);
    if (window.scrollY > 100) {
        stopMoveTracking();
    }
};

requestEventListener('scroll', trackScroll);

// remove scroll tracking after 5 seconds
setTimeout(() => {
    cancelEventListener('scroll', trackScroll);
}, 5000);
```

### Setters
```js
import OneListener from 'one-listener';

const one = new OneListener();
one.limit = 20
one.throttle = 200
```

### Getters
```js
import OneListener from 'one-listener';

const one = new OneListener();
console.log(one.limit);
console.log(one.throttle);
console.log(one.debug);
```



### locked instances

```js
import OneListener from 'one-listener';

// create locked instances (listeners.js)
const o = new OneListener({
    limit: 6,
    throttle: 200
});
const n = new OneListener({
    limit: Infinity
});
const a = new OneListener({
    limit: 0,
    throttle: 100
});

export const one = {
  requestEventListener: o.requestEventListener,
  cancelEventListener: o.cancelEventListener
}
export const always = {
  requestEventListener: a.requestEventListener,
  cancelEventListener: a.cancelEventListener
}
export const never = {
  requestEventListener: n.requestEventListener,
  cancelEventListener: n.cancelEventListener
}


// then import (somefile.js)

import {one,never,always} from './listeners';
const {requestEventListener, cancelEventListener} = one;

//requestEventListener() || one.requestEventListener()
//always.requestEventListener()
//never.requestEventListener()

```

---
_Source: https://npm.io/package/one-listener · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
