Sha256: 0028d06032193d3e50a61e44cf744b95441255781b59667e4c2770319eae807b
Contents?: true
Size: 931 Bytes
Versions: 7
Compression:
Stored size: 931 Bytes
Contents
# frozen_string_literal: true module SlackRubyBot module Hooks class Set attr_accessor :handlers, :client def initialize(client = nil) self.handlers = {} self.client = client @pending_flush = client.blank? end def add(hook_name, handler) handlers[hook_name] ||= ::Set.new handlers[hook_name] << handler register_callback(hook_name) end def client=(client) @client = client flush_handlers if @pending_flush end protected def register_callback(hook_name) return unless client # We'll delay this until client is set client.on hook_name do |data| handlers[hook_name].each do |handler| handler.call(client, data) end end end def flush_handlers handlers.each_key { |hook| register_callback(hook) } end end end end
Version data entries
7 entries across 7 versions & 2 rubygems