Skip to content

Passing port listener to port is clunky and errorprone #87

Closed

Description

@Omnieboer

Currently, to add an event listener to a port you have to do:

port.addEventListener(new MyPortListener(port));

where you add the listener to the port and pass the same port to the new MyPortListener class.

Instead I propose the SerialPortEvent gets passed the bytes in the buffer as the event fires.

Closed by #86

Activity

  1. tresf commented on Apr 30, 2021

    @tresf

    errorprone

    Thanks @Omnieboer. I see nothing errorprone in our existing example that is fixed in the PR, I find this statement to be incorrect. If you can provide evidence that it will result in errors, please add them to the issue description.

    clunky

    In regards to "clunky", the statement specifically references an example on the wiki, which you helped create in #85 😉 . In my experience it's sane to store a pointer to the SerialPort object and then reuse that to read bytes when events are fired, which would avoid passing a reference to the constructor. I realize this makes an assumption that the listener is constructed within (or externally exposed from) the same class as the SerialPort, but I don't know an easy way to avoid one or the other, and I certainly don't find pointer encapsulation like our example uses to be either "clunky" or "errorprone" (or at least I don't see it being better in the PR).

    I think the underlying design decision to do this -- and it being perceived as "clunky" -- stems from matching (or more proper, the inability to match) a String (portName) to an Object (SerialPort instance) or inability to obtain the SerialPort reference from the caller, but this is fairly common in listeners in my experience. Since these events are fired by CPP and can be a one-to-many relationship, I'm not sure how to handle it better. Stashing a reference may also break the 1:many relationship of the listener.

    I've shared more thoughts over in #86. If the thread gets too long, it may make sense to take it over onto Slack.

  2. tresf commented on May 3, 2021

    @tresf

    I've shared more thoughts over in #86.

    I think this is workable, proposed a diff over in #86 that I think does what you're looking for, at least mostly. :)

  3. added a commit that references this issue on May 4, 2021
    8f6370a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions