Skip to content

Redesign listener system #3

Closed
Closed

Description

@jstasiak
No description provided.

Activity

  1. self-assigned this
    on Jul 8, 2014
  2. jstasiak commented on Feb 15, 2015

    @jstasiak
    CollaboratorAuthor

    So d162e54 goes in the direction I intended the interface to go but I think I'll restore the old interface at least until the next release but mark it as deprecated.

  3. lexruee commented on Feb 16, 2015

    @lexruee

    @jstasiak Hi! I'm curious. What's the reason to change the listener system?

  4. jstasiak commented on Feb 17, 2015

    @jstasiak
    CollaboratorAuthor

    Hey @lexruee my primary reason it's class based to you need to create a class to listen to events or use some hackery to pass your handler methods in some other way - I think we can do better. I'm looking for some ideas other than what I implemented in d162e54 because I'm not entirely happy with it though.

  5. lexruee commented on Feb 17, 2015

    @lexruee

    @jstasiak The old implementation uses the observer pattern applied on the ServiceBrowser. But I can understand why one could argue not use it. Link

    Do you have in mind to implement something like that:

    browser = ServiceBrowser(zeroconf, "_http._tcp.local.")
    browser.on_event('add_service', callback)
    browser.on_event('remove_service', callback)

    Anyways, I'm new to python, so I don't know the pythonic way :-).

  6. jstasiak commented on Feb 20, 2015

    @jstasiak
    CollaboratorAuthor

    Right now the secondary, experimental, listener system looks like this:

    def handler(zeroconf, service_type, name, state_change):
        pass
    
    # then you can do this
    browser = ServiceBrowser(zeroconf, '_http._tcp.local.', handlers=[handler])
    
    # or that
    
    browser = ServiceBrowser(zeroconf, '_http._tcp.local.')
    browser.service_state_changed.register_handler(handler)

    It doesn't require you to create a class to have a handler but I'm not sure how Pythonic it is. As another alternative I'm thinking about is to expose an interface returning iterators, such iterators would yield events discovered by the ServiceBrowser:

    browser = ServiceBrowser(zeroconf, '_http._tcp.local.')
    events = browser.get_iterator()
    for e in events:
        print(e.state_change, e.name, ...)
  7. lexruee commented on Feb 20, 2015

    @lexruee

    In my opinion it's fine to have a generic service state handler.

    But I would also provide a shortcut to register specific event handlers like 'add_service' or 'remove_service' which are called if such an event occurs. In that case you could avoid to write a generic event handler where you need if-elsif-else statements to distinguish between events.

    And instead I of exposing the 'service_state_changed.' property to register new handlers I would use a method like 'register_listener'.

    However, that's just my opinion :-) (based on java / ruby experience)

  8. jstasiak commented on Mar 29, 2015

    @jstasiak
    CollaboratorAuthor

    @lexruee Thanks for the notes, I'll definitely keep this in mind

  9. jstasiak commented on Dec 1, 2015

    @jstasiak
    CollaboratorAuthor

    Closing as I have no plan to do this at the moment.

  10. added a commit that references this issue on Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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