# subscribe-event

> Easiest way to subscribe and unsubscribe browser / node events

Latest version **1.1.2** (published 2024-04-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install subscribe-event
pnpm add subscribe-event
yarn add subscribe-event
bun add subscribe-event
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2024-04-26 |
| First published | 2016-03-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Nikita Mostovoy |
| Maintainers | xnimorz |
| Keywords | event, subscribe, subscribe-event, event-listener, listen, browser-events, addEventListener, attachEvent, unsubscribe, removeEventListener |

## Links

- npm: https://www.npmjs.com/package/subscribe-event
- Repository: https://github.com/xnimorz/subscribe-event
- Homepage: https://github.com/xnimorz/subscribe-event#readme
- Issues: https://github.com/xnimorz/subscribe-event/issues
- npm.io page: https://npm.io/package/subscribe-event

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.1.2 (latest) — 2024-04-26
- 1.1.1 — 2018-03-10
- 1.1.0 — 2018-03-09
- 1.0.0 — 2016-03-26

## README

## Subscribe-event

Subscribe-event is the easiest way to subscribe either dom events in browser or node.js events.

Live example: http://xnimorz.github.io/subscribe-event/

#### Install

```
npm install --save subscribe-event
// or for yarn:
yarn add subscribe-event
```

Library support es6, CommonJs, AMD modules

#### Getting started

Step 1: import module:

```javascript
import subscribe from "subscribe-event";
```

Step 2: call subscrube function:

```javascript
const unsubscribe = subscribe(document, "scroll", () => {
  /* Do some job */
});
```

Step 3: call unsubscibe, which returned from subscribe function, to dispose:

```javascipt
  unsubscribe();
```

#### API

`subscribe` is a function, which receives 4 arguments:

1.  element — is a HTMLElement such as document.body, document, etc., Node.js object or any another object, that triggers events.
2.  event — is an event, such as `scroll`, `click`, `mousemove` etc.
3.  eventCallback — a function called when event triggers
4.  options — unessesary parameter, which provides more data to subscribe function. For example, when you using `subscribe` for HTMLElements you can capture events: `subscibe(document.querySelector('#a'), 'click', () => console.log('clicked'), true)`

#### Usage with React

```javascript
import React, { Component } from "react";
import subscribe from "subscribe-event";

class MyAmazingComponent extends Component {
  componentDidMount() {
    this.unsubscribeScroll = subscribe(document, "scroll", () => {
      // do some job
    });
  }

  componentWillUnmount() {
    this.unsubscribeScroll();
  }

  render() {
    const { children } = this.props;

    return <div>{children}</div>;
  }
}
```

Subscribe-event is useful for capturing events:

```javascript
import React, { Component } from "react";
import subscribe from "subscribe-event";

class MyAmazingComponent extends Component {
  componentDidMount() {
    this.unsubscribe = subscribe(
      document,
      "scroll",
      () => {
        // do some job
      },
      true
    );
  }

  componentWillUnmount() {
    this.unsubscribe();
  }

  render() {
    const { children } = this.props;

    return <div>{children}</div>;
  }
}
```

#### Examples

For example in browser:

```javascript
import subscribe from "subscribe-event";

var element = document.querySelector(".my-button");

var unsubscribe = subscribe(element, "click", e => {
  console.log(e);
});

// code

unsubscribe();
```

or

```javascript
import subscribe from "subscribe-event";

var unsubscribe = subscribe(document, "scroll", e => {
  console.log(e);
});

// code

unsubscribe();
```

This library supports old ie also (attachEvent/detachEvent).

You can define your custom event subscribe function:

```javascript
import subscribe from 'subscribe-event';

var obj: {
  eventSubscribe: function() {...},
  eventUnsubscribe: function() {...}
}

var customSubscribe = subscribe.define('eventSubscribe', 'eventUnsubscribe');

var customUnsubscribe = customSubscribe(obj, event, handler);

// ...

customUnsubscribe();
```

By default subscribe-event works with: `addEventListener`, `detachEvent`, `on`

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