Skip to content

Commit

Permalink
fixup! Implement AsyncEventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Jun 9, 2019
1 parent ea554c2 commit 6450613
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 44 deletions.
65 changes: 32 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,74 +305,73 @@ Non-blocking synchronous map

#### AsyncEmitter.prototype.constructor()

#### AsyncEmitter.prototype.clear(name)
#### AsyncEmitter.prototype.event(name)

- `name`: [`<string>`][string] event name

Remove all listeners or by name
_Returns:_ { on: [`<Set>`][set], once: [`<Set>`][set] } }

#### AsyncEmitter.prototype.count(name)
Get or create event

- `name`: [`<string>`][string] event name
#### AsyncEmitter.prototype.on(name, fn)

_Returns:_ [`<number>`][number]
- `name`: [`<string>`][string] event name
- `fn`: [`<Function>`][function] listener

Get listeners count by event name
Add listener

#### async AsyncEmitter.prototype.emit(name, args)
#### AsyncEmitter.prototype.once(name, fn)

- `name`: [`<string>`][string] event name
- `args`: `<any[]>`
- `fn`: [`<Function>`][function] listener

_Returns:_ [`<Promise>`][promise]|[`<undefined>`][undefined]
_Returns:_ [`<Promise>`][promise]|[`<null>`][null]

Emit event
Add listener

#### AsyncEmitter.prototype.event(name)
#### AsyncEmitter.prototype.emit(name, args)

- `name`: [`<string>`][string] event name
- `args`: `<any[]>`

_Returns:_ { on [`<Function>`][function], once [`<Function>`][function] }
_Returns:_ [`<Promise>`][promise]|[`<null>`][null]

Get or create event
Emit event

#### AsyncEmitter.prototype.listeners(name)
#### AsyncEmitter.prototype.remove(name, fn)

- `name`: [`<string>`][string] event name
- `fn`: [`<Function>`][function] listener to remove

_Returns:_ [`<Function[]>`][function]

Get listeners array by event name
Remove event listener

#### AsyncEmitter.prototype.names()
#### AsyncEmitter.prototype.clear(name)

_Returns:_ [`<string[]>`][string] names
- `name`: [`<string>`][string] event name

Get event names array
Remove all listeners or by name

#### AsyncEmitter.prototype.on(name, fn)
#### AsyncEmitter.prototype.count(name)

- `name`: [`<string>`][string] event name
- `fn`: [`<Function>`][function] listener

Add listener
_Returns:_ [`<number>`][number]

#### AsyncEmitter.prototype.once(name, fn)
Get listeners count by event name

#### AsyncEmitter.prototype.listeners(name)

- `name`: [`<string>`][string] event name
- `fn`: [`<Function>`][function] listener

_Returns:_ [`<Promise>`][promise]|[`<undefined>`][undefined]
_Returns:_ [`<Function[]>`][function]

Add listener
Get listeners array by event name

#### AsyncEmitter.prototype.remove(name, fn, onceOnly)
#### AsyncEmitter.prototype.names()

- `name`: [`<string>`][string] event name
- `fn`: [`<Function>`][function] listener to remove
- `onceOnly`: [`<boolean>`][boolean] remove once only
_Returns:_ [`<string[]>`][string] names

Remove event listener
Get event names array

### asyncIter(base)

Expand Down Expand Up @@ -994,11 +993,11 @@ Set timeout for asynchronous function execution
[object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
[function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
[promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
[set]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
[array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
[error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type
[null]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type
[undefined]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type
[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type
[iterable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
Expand Down
10 changes: 3 additions & 7 deletions lib/async-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AsyncEmitter {

// Get or create event
// name <string> event name
// Returns: { on: Set { <Function> }, once: Set{ <Function> } }
// Returns: { on: <Set>, once: <Set> } }
event(name) {
const { events } = this;
const event = events.get(name);
Expand Down Expand Up @@ -72,12 +72,8 @@ class AsyncEmitter {
// name <string> event name
clear(name) {
const { events } = this;
if (!name) {
events.clear();
return;
}
const event = events.get(name);
if (event) events.delete(name);
if (!name) events.clear();
else events.delete(name);
}

// Get listeners count by event name
Expand Down
4 changes: 0 additions & 4 deletions test/async-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ metatests.test('AsyncEmitter on/emit', async test => {

test.strictSame(ae.count('e1'), 1);
test.strictSame(ae.names().length, 1);

test.end();
});

metatests.test('AsyncEmitter once', async test => {
Expand All @@ -37,8 +35,6 @@ metatests.test('AsyncEmitter once', async test => {

test.strictSame(ae.count('e1'), 0);
test.strictSame(ae.names().length, 0);

test.end();
});

metatests.test('AsyncEmitter await once', async test => {
Expand Down

0 comments on commit 6450613

Please sign in to comment.