Giter VIP home page Giter VIP logo

Comments (9)

krisleech avatar krisleech commented on May 22, 2024

How about on accepts a regex?

Wisper.add_listener(ItemListener.new, on: /item_*_rs/, with: :add_item)

from wisper.

apotema avatar apotema commented on May 22, 2024

Makes more sense @krisleech.

from wisper.

krisleech avatar krisleech commented on May 22, 2024

@apotema can you try #80 please?

gem 'wisper', github: 'krisleech/wisper', branch: 'add-regex-support-for-on-arg'

from wisper.

krisleech avatar krisleech commented on May 22, 2024

By the way I don't think /item_*_rs/ is the correct regexp to match your events: http://rubular.com/r/2farGcgXD0

from wisper.

apotema avatar apotema commented on May 22, 2024

@krisleech the regex-support is working, all my 22 tests passed.

One thing that I noticed, is that all methods on the Listener stop working, once you set the 'on' condition.

This is my listener

class ItemListener
  def add_item user, models
    ItemService.new(user, models).save
  end
end

It works great if I call him like this.

Wisper.subscribe(ItemListener.new, on: /item_(.*)(add|query)_rs/, with: :add_item)

But if I require that Listener to respond to another call, like 'item_remove_rs', I can't put another function in there, and expect that it will respond to it.

class ItemListener
  def add_item user, models
    ItemService.new(user, models).save
  end
  def item_remove_rs user,models
    ItemService.new(user, models).save
  end
end

I have to subscribe this listener twice.

Wisper.subscribe(ItemListener.new, on: /item_(.*)(add|query)_rs/, with: :add_item)
Wisper.subscribe(ItemListener.new)

Now it will respond for my 'regex' and for the 'item_remove_rs'.

The 'on' parameter has the 'side effect' of making all the other methods on the listener inert.

Maybe we could think the 'on' parameter, not as something that will exclude other methods on the listener, but give a specific method a rule and let the other methods works a usual.

from wisper.

krisleech avatar krisleech commented on May 22, 2024

One thing that I noticed, is that all methods on the Listener stop working, once you set the 'on' condition.

Correct, the on option is used to restrict which events are broadcast to the subscribers. When used in conjuction with the with option this restriction remains.

You could indeed subscribe the listener twice, but I suspect you will end up with some events being broadcast twice also.

Maybe we could think the 'on' parameter, not as something that will exclude other methods on the listener, but give a specific method a rule and let the other methods works a usual.

At this stage I can't change the behaviour of these options.

What you might consider doing in your listener is just aliasing the methods:

class ItemListener
  def add_item
    # ...
  end

  alias_method :add_item, :item_query_rs
  alias_method :add_item, :item_discount_add_rs
  # etc...
end

from wisper.

krisleech avatar krisleech commented on May 22, 2024

@apotema Any further thoughts on this? I'd like to merge the PR.

from wisper.

apotema avatar apotema commented on May 22, 2024

No @krisleech. That is it for me.

Thanks

from wisper.

krisleech avatar krisleech commented on May 22, 2024

Thanks, PR merged. @apotema if you ever fancy blogging about how you are using eventing or even posting a gist or example to the Wiki I would be very intrested to take a look.

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.