Sha256: 5fa486da7549076c83f8a6395dac027fe85af49ec709c535f41c41a8359328eb

Contents?: true

Size: 887 Bytes

Versions: 2

Compression:

Stored size: 887 Bytes

Contents

# frozen_string_literal: true

module God
  class Trigger
    class << self
      attr_accessor :triggers # {task.name => condition}
    end

    # init
    self.triggers = {}
    @mutex = Mutex.new

    def self.register(condition)
      @mutex.synchronize do
        triggers[condition.watch.name] ||= []
        triggers[condition.watch.name] << condition
      end
    end

    def self.deregister(condition)
      @mutex.synchronize do
        triggers[condition.watch.name].delete(condition)
        triggers.delete(condition.watch.name) if triggers[condition.watch.name].empty?
      end
    end

    def self.broadcast(task, message, payload)
      return unless triggers[task.name]

      @mutex.synchronize do
        triggers[task.name].each do |t|
          t.process(message, payload)
        end
      end
    end

    def self.reset
      triggers.clear
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
resurrected_god-1.1.1 lib/god/trigger.rb
resurrected_god-1.1.0 lib/god/trigger.rb