Created
June 25, 2018 02:09
-
-
Save MarkMT/a983fd7c23414fb28de3e883c24af309 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Publisher | |
def self.included(base) | |
base.instance_eval do | |
@class_registrations ||= Set.new | |
include Wisper.model | |
# ensures we override Wisper's definitions | |
include InstanceMethods | |
extend ClassMethods | |
end | |
end | |
module InstanceMethods | |
private | |
def registrations | |
local_registrations + class_registrations + global_registrations + temporary_registrations | |
end | |
def class_registrations | |
self.class.registrations | |
end | |
end | |
module ClassMethods | |
def subscribe(listener, options = {}) | |
@class_registrations << ::Wisper::ObjectRegistration.new(listener, options.merge(:scope => self)) | |
end | |
def registrations | |
@class_registrations + (superclass.try(:registrations) || []) | |
end | |
end | |
end | |
module Wisper | |
class ObjectRegistration | |
# For the registration to broadcast a specified event we require: | |
# - 'should_broadcast?' - If the listener susbcribed with an ':on' option, ensure that the event | |
# is included in the 'on' list. | |
# - 'listener.respond_to?' - The listener contains a handler for the event | |
# - 'publisher_in_scope?' - If the listener subscribed with a ':scope' option, ensure that the | |
# publisher's class is included in the 'scope' list. | |
def broadcast(event, publisher, *args) | |
method_to_call = map_event_to_method(event) | |
if should_broadcast?(event) && listener.respond_to?(method_to_call) && publisher_in_scope?(publisher) | |
broadcaster.broadcast(listener, publisher, method_to_call, args) | |
end | |
end | |
private | |
def publisher_in_scope?(publisher) | |
allowed_classes.empty? || (allowed_classes.include? publisher.class.name) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment