Giter Club home page Giter Club logo

Comments (10)

ronag avatar ronag commented on August 28, 2024 1

@mcollina

from summit.

benjamingr avatar benjamingr commented on August 28, 2024 1

I'm wondering if it's possible to solve generically by adding the capability to emit an event after someone is listening to it to the abstraction itself - e.g. by providing a helper to defer emitting an event up to setImmediate if there are no existing listeners for it.

This is similar to what we do for promises at the moment with unhanadled rejection detection.

from summit.

ronag avatar ronag commented on August 28, 2024

Refs: nodejs/node#51471
Refs: nodejs/node#51114
Refs: nodejs/node#51070
Refs: nodejs/node#51156
Refs: nodejs/node#51280
Refs: nodejs/undici#2497

from summit.

benjamingr avatar benjamingr commented on August 28, 2024

Would love to attend this remotely or discuss offline

from summit.

ronag avatar ronag commented on August 28, 2024

Might be also relevant to make sure that the docs are correct:

https://www.builder.io/blog/visual-guide-to-nodejs-event-loop
https://nodejs.org/en/learn/asynchronous-work/understanding-processnexttick

from summit.

ronag avatar ronag commented on August 28, 2024

Example of problem to discuss.

import { EventEmitter } from 'events'

// Unhandled exception
setImmediate(async () => {
  const e = await new Promise(resolve => {
    const e = new EventEmitter()
    resolve(e)
    process.nextTick(() => {
      e.emit('error', new Error('nextTick'))
    })
  })
  e.on('error', () => {})
})

// Unhandled exception
setImmediate(async () => {
  const e = await new Promise(resolve => {
    const e = new EventEmitter()
    resolve(e)
    queueMicrotask(() => {
      e.emit('error', new Error('queueMicrotask'))
    })
  })
  e.on('error', () => {})
})

// OK, but slow
setImmediate(async () => {
  const e = await new Promise(resolve => {
    const e = new EventEmitter()
    resolve(e)
    setImmediate(() => {
      e.emit('error', new Error('setImmediate'))
    })
  })
  e.on('error', () => {})
})

Please don't get hung up the 'error' event. This is a problem with other events as well. In many cases they can be worse, e.g. a similar example with waiting for a 'ready' event would deadlock instead of crash.

from summit.

littledan avatar littledan commented on August 28, 2024

Can we toss into this topic, how are web platform APIs for managing asynchronicity like AbortController working for your needs in Node? Maybe that is too much of a tangent and can stick in the standards part, though.

from summit.

joyeecheung avatar joyeecheung commented on August 28, 2024

For the remote participants: we've scheduled Zoom Webinars for the sessions, please register using the links provided in #387 and you'll get a link in an email to join the sessions. More info in the issue mentioned.

EDIT: You can also get invited to be a panelist beforehand to save the registration & promotion step. Ping in the OpenJS slack with your email or send a email to the email in my GitHub profile to get an invitation.

from summit.

Qard avatar Qard commented on August 28, 2024

A possible solution is to allow EventEmitter to take a buffer strategy which can optionally buffer events to be flushed later. For example:

const events = require('events')

class EventEmitter extends events.EventEmitter {
  constructor(options) {
    super(options)
    this.buffer = options?.bufferStrategy || new NoBufferStrategy()
  }

  emit(name, ...args) {
    // No listeners yet. Try to buffer the event.
    if (!this.listeners(name).length && this.buffer.buffer(name, args)) {
      return
    }

    return super.emit(name, ...args)
  }

  on(name, ...args) {
    try {
      return super.on(name, ...args)
    } finally {
      // After attaching a handler, flush any buffered events.
      this.buffer.flush(name, (...args) => {
        this.emit(...args)
      })
    }
  }
}

// Default strategy does no buffering.
class NoBufferStrategy {
  buffer(name, task) {
    return false
  }

  flush() {}
}

// A possible alternative strategy could buffer the events until a handler is given.
class BufferStrategy {
  #buffered = new Map()

  buffer(name, args) {
    const map = this.#buffered

    if (!map.has(name)) {
      map.set(name, [])
    }
    map.get(name).push(args)

    return true
  }

  flush(name, handle) {
    const map = this.#buffered
    const queue = map.get(name)
    map.delete(name)

    for (const args of queue) {
      handle(name, ...args)
    }
  }
}

const p = new Promise((resolve) => {
  const e = new EventEmitter({
    bufferStrategy: new BufferStrategy()
  })

  process.nextTick(() => {
    e.emit('error', 'nextTick')
  })

  resolve(e)
})

p.then(e => {
  return new Promise(resolve => {
    setImmediate(resolve, e)
  })
}).then(e => {
  e.on('error', (error) => console.log('error', error))
})

It's worth noting that the BufferStrategy here is just very simplistic example to demonstrate the idea. It probably should not buffer indefinitely, it should have limits. At the least it should probably ensure error events don't just get held forever when there is no handler.

from summit.

joyeecheung avatar joyeecheung commented on August 28, 2024

Closing as the session has ended.

Links to the recordings:

Links to the notes (very inaccurate, we should try harder at getting dedicated note takers the next time):

Drafted trip report, would appreciate some reviews. Expect to publish on the official blog next week: https://hackmd.io/5vvP8o5bTmqcbNh-3oHpag

from summit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.