# @flk/events

> A powerful yet easy event emitter.

Latest version **2.0.2** (published 2021-01-16) · 0 weekly downloads

## Install

```sh
npm install @flk/events
pnpm add @flk/events
yarn add @flk/events
bun add @flk/events
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2021-01-16 |
| First published | 2019-09-17 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Hasan Zohdy |
| Maintainers | hassanzohdy |

## Links

- npm: https://www.npmjs.com/package/@flk/events
- Repository: https://github.com/falakjs/events
- Homepage: https://github.com/falakjs/events#readme
- Issues: https://github.com/falakjs/events/issues
- npm.io page: https://npm.io/package/@flk/events

## Recent versions

- 2.0.2 (latest) — 2021-01-16
- 2.0.1 — 2021-01-16
- 2.0.0 — 2021-01-16
- 1.3.1 — 2020-03-13
- 1.3.0 — 2019-12-31
- 0.1.57 — 2019-12-01
- 0.1.56 — 2019-09-17

## README

# Events

A simple lightweight event handler package for Falak JS.


> Note this package could be used as a standalone package

# Installation

> This package is automatically installed on each new application so you don't have to install it.

For standalone usage
`npm i -s flk-events`

# Package info

**Alias:** `events`

The `Events` class is used to move between all components and packages with events without any conflict;


# Usage

If you want to use `events` in any component class, just inject it into the constructor with `events` parameter

`home-page.component.js`

```js
class HomePage {
    constructor(events) {
        this.events = events;
    }
}
```

For outer usage use the [DI.resolve('events')](https://github.com/falakjs/Falak/wiki/Dependency-Injection#resolving-class).

# Event listeners
To add event listener to any event use the `on` method, for example in the `main.js` file:

`main.js`
```js
let events = DI.resolve('events');
events.on('app.ready', function () {
    // do something when app is ready
});
```

> `addEventListener` and `subscribe` are alias methods to `on` method.

You may use the `subscribe` if you're more familiar with it than the `on` method

`main.js`
```js
let events = DI.resolve('events');
events.subscribe('app.ready', function () {
    // do something when app is ready
});
```

# Triggering event
To trigger event use the `trigger` method or `emit` as an alias.

```js
// listen to the user waking up
let events = DI.resolve('events');

events.on('user.wake-up', user => {
    console.log(user.id); // 12
});

setTimeout(() => {
    events.trigger('user.wake-up', {
        id: 12
    });
}, 4000);
```

You can pass any number of arguments to the `trigger` method as it will be passed to the `callback` function in the `on` method.

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