# hooks-mixin

> A small mixin for classes. Adds hooks ability to your classes

Latest version **1.3.0** (published 2021-03-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install hooks-mixin
pnpm add hooks-mixin
yarn add hooks-mixin
bun add hooks-mixin
```

## 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.3.0 |
| Published | 2021-03-22 |
| First published | 2018-04-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Constantin Tsukanov |
| Maintainers | ya-kostik |

## Links

- npm: https://www.npmjs.com/package/hooks-mixin
- Repository: git@github.com:ya-kostik/hooks-mixin
- npm.io page: https://npm.io/package/hooks-mixin

## Recent versions

- 1.3.0 (latest) — 2021-03-22
- 1.2.2 — 2020-04-07
- 1.2.1 — 2019-07-17
- 1.2.0 — 2018-04-17
- 1.1.0 — 2018-04-10
- 1.0.0 — 2018-04-10

## README

# hooks-mixin
A small mixin for classes. Adds hooks ability to your classes

## Install

### Yarn
```
yarn add hooks-mixin
```

### NPM
```
npm i hooks-mixin
```

## Use
### Add and call
```javascript
const HooksMixin = require('hooks-mixin');

class Class {
  async save() {
    // sync hook call
    this.processHooks('pre-save');
    await this.write();
    // async hook call
    await this.processHooksAsync('saved');
    return this;
  }
};

HooksMixin(Class);

const instance = new Class();
// add hook
instance.hook('pre-save', (instance) => {
  // instance — is an instance this
  // things with instance
});
instance.save();
// will call pre-save and saved hooks
```

You can add many hooks:
```javascript
instance.hook('pre-save', () => console.info('One'));
instance.hook('pre-save', () => console.info('Two'));
instance.hook('saved', () => console.info('Three'));
instance.hook('saved', () => console.info('Four'));

instance.save();
// One, Two, Three, Four
```

### Remove hook callback
```javascript
const preSaveHookFunction = () => console.info('=)');
instance.hook('pre-save', preSaveHookFunction);

instance.save();
// =)
instance.removeHook('pre-save', preSaveHookFunction);
instance.save();
// no “=)”
```

## Frontend

Just `import` or `require` from `build/index.js`.

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