Skip to content

Commit

Permalink
fix: events extends Map
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Jan 7, 2021
1 parent 8cb7384 commit 6944261
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ Installation can be done via package managers such as [npm] or [yarn]
% yarn add capillaries
```

or use cdn

#### Minified:

```bash
//cdn.jsdelivr.net/npm/capillaries@latest/capillaries.umd.min.js
```

#### Pretty Printed:

```bash
//cdn.jsdelivr.net/npm/capillaries@latest/capillaries.umd.js
```

### Events

```js
Expand Down Expand Up @@ -57,6 +43,8 @@ event.off('connected');

// unbind all event listeners
event.unbindAll();
// or
event.clear();
```

### Hooks
Expand All @@ -80,18 +68,21 @@ hooks.call('Hook', payload); //-> returns undefined
hooks.callWaterFall('Hook', payload); //-> returns 'Hello World!'
hooks.callAsync('AsyncHook', payload); // awaits on taps, returns undefined
hooks.callAsyncWaterFall('AsyncHook', payload); // awaits on taps, returns 'Hello World!'

// remove all hooks
hooks.clear()
```

Hooks are executed in order. The waterfall passes a return value from each function to the next function and returns final retuned data

### Browser compatibility

- Internet Explorer 9+
- Chrome 6+
- Chrome 38+
- Edge 12+
- Firefox 4+
- Opera 12+
- Safari 5.1+
- Firefox 13+
- Opera 25+
- Safari 8+
- Internet Explorer 11

[npm]: https://www.npmjs.com/
[yarn]: https://yarnpkg.com/lang/en/
Expand Down
27 changes: 16 additions & 11 deletions capillaries.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export class Events {
#events = {};

export class Events extends Map {
constructor() {
super();
Object.freeze(this);
}

Expand All @@ -10,18 +9,19 @@ export class Events {
throw new TypeError('Event Listener must be a function');
}

const event = this.#events[type] || [];
const event = this.get(type) || [];
event.push([listener, ctx]);
this.#events[type] = event;
this.set(type, event);

return () => {
this.#events[type] = (this.#events[type] || []).filter((e) => e[0] !== listener);
const events = this.get(type) || [];
this.set(type, events.filter((e) => e[0] !== listener));
};
}

emit = (type, ...args) => {
const starEvents = type === '*' ? [] : this.#events['*'] || [];
const eventList = (this.#events[type] || []).concat(starEvents);
const starEvents = type === '*' ? [] : this.get('*') || [];
const eventList = (this.get(type) || []).concat(starEvents);

eventList.forEach((event) => {
const [listenerFn, ctx] = event;
Expand All @@ -31,15 +31,20 @@ export class Events {

unbindAll = (type) => {
if (type) {
delete this.#events[type];
this.delete(type);
return;
}

this.#events = {};
this.clear();
}
}

export class Hooks extends Map {
constructor() {
super();
Object.freeze(this);
}

tap = (name, cb, ctx = null) => {
if (typeof cb !== 'function') {
throw new TypeError('Callback must be a function');
Expand All @@ -50,7 +55,7 @@ export class Hooks extends Map {
this.set(name, hook);

return () => {
const hooks = this.get(name);
const hooks = this.get(name) || [];
this.set(name, hooks.filter((e) => e[0] !== cb));
};
}
Expand Down
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const compile = async function () {
await bundle.write({
file: 'dist/capillaries.js',
format: 'cjs',
exports: 'auto',
exports: 'named',
sourcemap: true,
banner,
});
Expand All @@ -50,6 +50,7 @@ const compile = async function () {
await bundle.write({
file: 'dist/capillaries.umd.js',
format: 'umd',
exports: 'named',
name: 'Capillaries',
sourcemap: true,
banner,
Expand Down
2 changes: 1 addition & 1 deletion tests/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ it('should not throw error when event is unsbscribed already', () => {
event.unbindAll('q');
event.emit('q', payload);

expect(() => { unsbscribe(); }).not.toThrow(TypeError);
expect(() => { unsbscribe(); }).not.toThrow();
expect(callbackFnQ).not.toBeCalled();
});

Expand Down
13 changes: 13 additions & 0 deletions tests/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ it('should be able to untap a tap', () => {
expect(value).toBe(undefined);
});

it('should not throw error if untap called twice', () => {
const callbackFnQ = jest.fn().mockReturnValue(2);

const untap = hooks.tap('q', callbackFnQ);
hooks.clear();

expect(() => { untap(); }).not.toThrow();
const value = hooks.call('q', 1);

expect(callbackFnQ).not.toBeCalled();
expect(value).toBe(undefined);
});

it('should throw error when callback is not a function', () => {
expect(() => hooks.tap('q')).toThrow(TypeError);
});
Expand Down

0 comments on commit 6944261

Please sign in to comment.