Comments (10)
from summit.
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.
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.
Would love to attend this remotely or discuss offline
from summit.
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.
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.
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.
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.
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.
Closing as the session has ended.
Links to the recordings:
- Day 1 morning: https://zoom.us/rec/share/UmLZkxbAqBGgEUh8GptGvS_5qXXxKqEpT-rcTMX4ncTTS715-OTIJFv4nLZ40Tj5.Q4gNxJPbpTLH0W-X?startTime=1712132193000
- Day 1 afternoon: https://zoom.us/rec/share/UmLZkxbAqBGgEUh8GptGvS_5qXXxKqEpT-rcTMX4ncTTS715-OTIJFv4nLZ40Tj5.Q4gNxJPbpTLH0W-X?startTime=1712148446000
- Day 2 morning: https://zoom.us/rec/share/UcbGphrblwjuE2jL9dlsv-4hMv6uC0M_dS96N1xByyJcXfELAvrakmjHNguiS-Nc.5wjMJg55LA891PxO?startTime=1712218676000
- Day 2 afternoon: https://zoom.us/rec/share/UcbGphrblwjuE2jL9dlsv-4hMv6uC0M_dS96N1xByyJcXfELAvrakmjHNguiS-Nc.5wjMJg55LA891PxO?startTime=1712234786000
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)
- Session Proposal: Documentation Improvements HOT 5
- Open Visualization Session Proposal: <Project Name> <Session Title>
- Open Visualization Collaborator Summit 2024 / London HOT 1
- Open Visualization Session Proposal: deck.gl, GeoArrow and Lonboard HOT 5
- Session+Proposal:+<Project+Name>+<Session+Title> HOT 1
- Open Visualization Session Proposal: MapLibre - Captain Hook - Mastering MapLibre's Hooks HOT 5
- Open Visualization Session Proposal: MapLibre Text Rendering With WebGL HOT 6
- Open Visualization Session Proposal: Real-time Collaborative Map Drawing with deck.gl HOT 5
- Open Visualization Session Proposal: React Renderer for Deck.gl HOT 2
- Open Visualization Session Proposal: Going Global with 3D Tiles HOT 4
- Open Visualization Session Proposal: Deck migration log book 🚢 HOT 4
- Open Visualization Session Proposal: Loaders.gl - current state, latest features and 2024 roadmap HOT 2
- Node.js Collaborator Summit 2024 / Dublin HOT 7
- Session Proposal: Next 10 - Deep dive on funding
- Open Visualization Session Proposal: WeatherLayers - Weather Visualization with deck.gl
- Session Proposal: module loading customization/optimization and CJS/ESM interoperability
- Session Proposal: Node.js Next-10 Survey Results
- Session Proposal: Node.js Next-10 Healthy Collaborator Base HOT 1
- Session Proposal: To know you is to love you. Diversifying Node.js.
- Session Proposal: Facilitating userland migrations to new features and breaking changes
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from summit.