Skip to content

Commit

Permalink
feat: HoukBus
Browse files Browse the repository at this point in the history
  • Loading branch information
krmax44 committed Sep 11, 2019
1 parent 5bab1fa commit 50ffaeb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://travis-ci.com/krmax44/houk.svg?branch=master)](https://travis-ci.com/krmax44/houk)
[![install size](https://packagephobia.now.sh/badge?p=houk)](https://packagephobia.now.sh/result?p=houk)
![npm version](https://img.shields.io/npm/v/houk)
[![npm version](https://img.shields.io/npm/v/houk)](https://www.npmjs.com/package/houk)

A super simple event bus built for hook chains.

Expand Down Expand Up @@ -47,8 +47,24 @@ myInstance.on('myEvent', function(value) {
myInstance.fire();
```

If you just want an open hook bus, you can use `HoukBus`, where all methods are public.

```js
import { HoukBus } from 'houk';

const bus = new HoukBus();

bus.on('newUser', user => console.log(`Hello ${user}!`));

bus.emit('newUser', undefined, 'Max');

// --> Hello Max!
```

## API

The same API applies to `HoukBus` as well, with the difference being that it's not an abstract class and all methods are public.

### `Houk.on`

Listen to a particular event.
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default abstract class Houk {
...args: any
): Promise<any> {
const listeners = this.getListeners(event);
console.log(listeners);

if (listeners.size === 0) {
return args.length === 1 ? args[0] : args;
Expand Down Expand Up @@ -67,8 +66,19 @@ function toArray(input: any): any[] {
return Array.isArray(input) ? input : [input];
}

/**
* HoukBus allows you to use Houk without creating an extended class.
* All methods are public.
*/
export class HoukBus extends Houk {
public emit = super.emit;

public getListeners = super.getListeners;
}

if (typeof module !== 'undefined') {
module.exports = Houk;
module.exports.default = Houk;
module.exports.Houk = Houk;
module.exports.HoukBus = HoukBus;
}
33 changes: 33 additions & 0 deletions src/tests/bus.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from 'chai';
import * as random from './utils/random';
import { HoukBus } from '..';
import 'mocha';

const randomString = random.randomString();
const randomObject = random.randomObject();

describe('basic hookchain with bus', () => {
it('should run in order', async () => {
const test = new HoukBus();
let thisArg;
let arg;

test.on('test', function(string: string) {
thisArg = this;
arg = string;

return string + string;
});

test.on('test', async function(string: string) {
await new Promise(resolve => setTimeout(resolve, 5));
return string.toUpperCase();
});

const value = await test.emit('test', randomObject, randomString);

expect(thisArg).to.eql(randomObject);
expect(arg).to.equal(randomString);
expect(value).to.equal((randomString + randomString).toUpperCase());
});
});

0 comments on commit 50ffaeb

Please sign in to comment.