Giter VIP home page Giter VIP logo

Comments (7)

krisleech avatar krisleech commented on August 12, 2024

I not sure I understand, can you give an example of what the API would look like please?

from wisper.

chrisnicola avatar chrisnicola commented on August 12, 2024

Something like this.

Wisper.subscribe(LoggerSubscriber.new, with: :log_event)

class LoggerSubscriber

  def log_event(*args, event_name)
    logger.info "#{event_name} occurred"
  end
end

from wisper.

krisleech avatar krisleech commented on August 12, 2024

My gut feeling is the PR would add too much complexity in terms of code branching to be acceptable.

But there is a way. You could write a custom broadcaster which passes the event name in addition to the args.

Have a look here for more information. You could even release a gem :)

Let me know if you have any problems.

from wisper.

chrisnicola avatar chrisnicola commented on August 12, 2024

No problem. As I said I found a simple enough solution to the issue.

from wisper.

krisleech avatar krisleech commented on August 12, 2024

I think one of your comments has gone missing :)

Feel free to update the Wiki if you think the solution would be helpful to others.

from wisper.

nepalez avatar nepalez commented on August 12, 2024

Except for method_missing there is one more way to solve the problem
of this type.

I define a listener as a decorator, providing necessary callbacks. In the
decorator I define two additional methods: #otherwise and #finalize.

The first one does some default actions, the second one calls #otherwise
when no expected notification has been received beforehand.

  class Listener < SimpleDelegator

    def on_success
      # ...do something
    end

    # ... other callbacks the listener looks for

    def otherwise
      # ...do something
    end

    def finalize
      otherwise unless @notified
    end

    # the publisher checks callback existense before calling it,
    # and @notified remembers the fact
    def respond_to?(*)
      super ? (@notified = true) : false
    end
  end

The bright side of this trick is I can separate callbacks the listener looks for
from any others (including the case the publisher dies silently and sends
nothing).

The dark side is I need to explicitly call #finalize after service call.

  publisher.subscribe listener
  publisher.call # does something and sends notifications
  listener.finalize

I don't think it is a universal solution, but in controllers or
when one publisher calls another and listens to its notification, it
works fine.

from wisper.

chrisnicola avatar chrisnicola commented on August 12, 2024

@krisleech I was referring to using method_missing

from wisper.

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.