Code Monkey home page Code Monkey logo

Comments (7)

bvx89 avatar bvx89 commented on May 9, 2024

Maybe I don't understand this correctly, but how would one unsubscribe from these events?

from emittery.

dinoboff avatar dinoboff commented on May 9, 2024

By breaking out of the loop.

But I don't think it's a good fit. Emitter push data, while you pull data from an Iterator. To work the emitter would have to either queue events or to skip events emitted while the async loop block is getting processed.

from emittery.

sindresorhus avatar sindresorhus commented on May 9, 2024

@dinoboff It would queue events. That's how Emittery works already though, as events are emitted async, so they're placed on the event loop (queue). It's probably not useful for the common case, true, but sometimes I just want to gather a certain amount of events from an emitter or all, and then iterating like this would be a nice way of handling it.

from emittery.

novemberborn avatar novemberborn commented on May 9, 2024

Weak references would allow the iterator to be garbage collected, and thus the queue as well. Support for that is probably still a long way off though.

from emittery.

dinoboff avatar dinoboff commented on May 9, 2024

@sindresorhus they are queued to some extends but the queue get flushed on the next tick whether the consumer is ready or not.

from emittery.

dinoboff avatar dinoboff commented on May 9, 2024

@bvx89 It would be something like that

function iterator(emitter, eventName) {
  return {
    async *[Symbol.asyncIterator]() {
      const queue = [];
      const off = emitter.on(eventName, (data) => queue.push(data));

      try {
        while (true) {
          if (queue.length == 0) {
            await emitter.once(eventName);
          }

          yield queue.shift();
        }
      } finally {
        // Called once the iterator result `return` or `throw` methods are called;
        // e.g. if the loop block `break` or `throw`.
        off();
      }
    }
  }
}

from emittery.

sindresorhus avatar sindresorhus commented on May 9, 2024

Fixed by #40

from emittery.

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.