Skip to content

AsyncEmitter#427

Open
tshemsedinov wants to merge 3 commits intomasterfrom
async-ee
Open

AsyncEmitter#427
tshemsedinov wants to merge 3 commits intomasterfrom
async-ee

Conversation

@tshemsedinov
Copy link
Copy Markdown
Member

ee.on('e1', async () => true);
await ee.emit('a1');

I suppose we need to hide counters when waiting for multiple event handlers to this abstraction.

Comment thread lib/aee.js Outdated
Comment thread lib/aee.js Outdated
Comment thread lib/aee.js Outdated
Comment thread lib/aee.js Outdated
Comment thread lib/aee.js Outdated
Comment thread lib/aee.js Outdated
@tshemsedinov tshemsedinov changed the title AsyncEventEmitter AsyncEmitter Jun 3, 2019
Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js
Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js Outdated
@tshemsedinov tshemsedinov self-assigned this Jun 3, 2019
@tshemsedinov tshemsedinov force-pushed the async-ee branch 2 times, most recently from 1d27b1e to 4102ef3 Compare June 4, 2019 07:58
Comment thread metasync.js Outdated
Comment thread test/async-emitter.js Outdated
@tshemsedinov tshemsedinov requested review from belochub and nechaido June 4, 2019 21:57
Comment thread test/async-emitter.js Outdated
Comment thread test/async-emitter.js
Comment thread test/async-emitter.js
@tshemsedinov tshemsedinov requested a review from lundibundi June 5, 2019 10:54
Comment thread test/async-emitter.js Outdated
Comment thread test/async-emitter.js Outdated
@tshemsedinov tshemsedinov requested a review from lundibundi June 5, 2019 11:57
Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js
Comment thread test/async-emitter.js Outdated
Comment thread test/async-emitter.js Outdated
Comment thread test/async-emitter.js
Comment thread test/async-emitter.js
@tshemsedinov tshemsedinov requested a review from lundibundi June 5, 2019 13:21
Comment thread lib/async-emitter.js Outdated
Comment thread .eslintignore
Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js Outdated
@tshemsedinov
Copy link
Copy Markdown
Member Author

I added tests that reveal problems and now it’s clear that we need to change data structure for wrappers. Maybe better store them mixed to functions: fn[symbolWrapper] = wrapper ? @nechaido @belochub @lundibundi

@tshemsedinov
Copy link
Copy Markdown
Member Author

Implemented in a different way. Now we hold on and once in different data structures for each event separately:

AsyncEmitter {
  events:
    Map {
      'eventName' => {
        on: Set { listener<Function> },
        once: Map { listener<Function> => wrapper<Function> }
      },
  }
}

@nechaido @belochub @lundibundi

Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js Outdated
@tshemsedinov tshemsedinov requested a review from nechaido June 8, 2019 00:59
Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js Outdated
Comment thread test/async-emitter.js
Comment thread lib/async-emitter.js Outdated
Comment thread lib/async-emitter.js Outdated
Co-Authored-By: Denys Otrishko <shishugi@gmail.com>
Co-Authored-By: Mykola Bilochub <nbelochub@gmail.com>
Co-Authored-By: Dmytro Nechai <nechaido@gmail.com>
@tshemsedinov tshemsedinov requested a review from nechaido June 14, 2019 15:06
Comment thread lib/async-emitter.js
const event = this.events.get(name);
if (!event) return 0;
const { on, once } = event;
return on.size + once.size;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can be simplified to:

return event ? event.on.size + event.once.size : 0;

Comment thread lib/async-emitter.js
// Get or create event
// name <string> event name
// Returns: { on: <Set>, once: <Set> } }
event(name) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to use Symbol('getListeners') for this, to make this property "private".

Comment thread lib/async-emitter.js

class AsyncEmitter {
constructor() {
this.events = new Map();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.events = new Map();
this.listeners = new Map();

Comment thread lib/async-emitter.js
if (!event) return Promise.resolve();
const { on, once } = event;
const promises = [...on, ...once].map(fn => fn(...args));
once.clear();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to move this one line up, because some of the listeners may create additional once listeners.

Comment thread test/async-emitter.js
metatests.test('AsyncEmitter on/emit', async test => {
const ae = new AsyncEmitter();

const fn = test.mustCall(async (a, b, c, d) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Suggested change
const fn = test.mustCall(async (a, b, c, d) => {
const fn = test.mustCall(async (...args) => { test.strictSame(args, [1, 2, 3, 4]) });

Comment thread test/async-emitter.js

metatests.test('AsyncEmitter once', async test => {
const ae = new AsyncEmitter();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a test, to ensure that emit args are forwarded to once listener.

Comment thread lib/async-emitter.js
once(name, fn) {
if (fn === undefined) {
return new Promise(resolve => {
this.once(name, resolve);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.once(name, resolve);
this.once(name, (...args) => resolve(args.length <= 1 ? args[0] : args)
);

Comment thread test/async-emitter.js
});

setTimeout(fn, 0);
await ae.once('e1');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants