Skip to content

Conversation

@tshemsedinov
Copy link
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.

@tshemsedinov tshemsedinov changed the title AsyncEventEmitter AsyncEmitter Jun 3, 2019
@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
@tshemsedinov tshemsedinov requested review from belochub and nechaido June 4, 2019 21:57
@tshemsedinov tshemsedinov requested a review from lundibundi June 5, 2019 10:54
@tshemsedinov tshemsedinov requested a review from lundibundi June 5, 2019 11:57
@tshemsedinov tshemsedinov requested a review from lundibundi June 5, 2019 13:21
@tshemsedinov
Copy link
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
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

@tshemsedinov tshemsedinov requested a review from nechaido June 8, 2019 00:59
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
const event = this.events.get(name);
if (!event) return 0;
const { on, once } = event;
return on.size + once.size;
Copy link
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;

// Get or create event
// name <string> event name
// Returns: { on: <Set>, once: <Set> } }
event(name) {
Copy link
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".


class AsyncEmitter {
constructor() {
this.events = new Map();
Copy link
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();

if (!event) return Promise.resolve();
const { on, once } = event;
const promises = [...on, ...once].map(fn => fn(...args));
once.clear();
Copy link
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.

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

const fn = test.mustCall(async (a, b, c, d) => {
Copy link
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]) });


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

Copy link
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.

once(name, fn) {
if (fn === undefined) {
return new Promise(resolve => {
this.once(name, resolve);
Copy link
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)
);

});

setTimeout(fn, 0);
await ae.once('e1');
Copy link
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.

5 participants