Coder Social home page Coder Social logo

Comments (6)

notgull avatar notgull commented on September 27, 2024 1

Rust's documentation seems to imply that a panic is the preferred option in this case.

from event-listener.

notgull avatar notgull commented on September 27, 2024

It seems you are using the future like this:

let mut listener = event.listen();
loop {
    if 0 == n.load(Ordering::SeqCst) {
        return Poll::Ready(());
    };
    ready!(Pin::new(&mut listener).poll(cx));
} 

It should be used like this:

let mut listener = None;
loop {
    if 0 == n.load(Ordering::SeqCst) {
        return Poll::Ready(());
    };
    if let Some(listener) = listener.as_mut() {
        ready!(Pin::new(&mut listener).poll(cx));
        listener = None;
    } else {
        listener = Some(event.listen());
    }
}

Polling the EventListener after it has returned Ready will panic, and further polls should use a completely new EventListener instead.

The v4 API kept track of this state inside of the EventListener, but this caused confusion and was removed in v5.

from event-listener.

notgull avatar notgull commented on September 27, 2024

If you think this could be made clearer in documentation I would accept a PR.

from event-listener.

jbr avatar jbr commented on September 27, 2024

Would it make sense to just always return ready once it has ever been ready instead of panicking? That seems a lot easier to document and explain.

from event-listener.

jbr avatar jbr commented on September 27, 2024

It looks like EventListener holds an InnerListener that contains a reference to the Event. Would it work to have a configurable option that allows the listener to reregister itself on completion? It feels awkward to require user code to match Pin::new(&mut listener).poll(cx) { Poll::Ready(()) => { *listener = event.listen(); ... }, ... }

from event-listener.

notgull avatar notgull commented on September 27, 2024

It's a bad idea to have the EventListener automatically insert itself into the list. Generally, the flow should be:

  • Check your condition.
  • Register the listener into the list.
  • Check your condition, ideally with stronger ordering.
  • Wait on the listener.

If you register the listener into the list without actively polling an event, it can lead to strange behavior. E.g. your future received a notification meant for another task

from event-listener.

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.