registering events with objects not procs

to make unregistering easier
This commit is contained in:
Torsten Ruger 2015-07-24 10:01:01 +03:00
parent 8183dfd6b1
commit 0cdd5fdf13
3 changed files with 30 additions and 10 deletions

View File

@ -6,17 +6,27 @@ class BlockView
include React::Component include React::Component
required_param :interpreter required_param :interpreter
before_mount do
interpreter.register_event(event: :block_changed, listener: self, callback: :block_changed)
end
def block_changed block
block! block
end
def render def render
div.row do div.row do
div.col_md_6 do div.col_md_5 do
SourceView :source => interpreter.block.codes.first.source SourceView :source => interpreter.block.codes.first.source
end end
div.col_md_6 do div.col_md_5 do
interpreter.block.codes.each do |code| interpreter.block.codes.each do |code|
code.to_s.br code.to_s.br
end end
end end
div.col_md_2 do
button.btn.btn_default { "next" }.on(:click) { interpreter.tick }
end
end end
end end
end end

View File

@ -3,17 +3,19 @@
module Eventable module Eventable
# Register a handler for the given event name. # Register a handler for the given event name.
# The event name is the method name called on the handler object
# #
# obj.on(:foo) { puts "foo was called" } # obj.on(:foo , some_object_that_implements foo( whateverargs)
# #
# @param [String, Symbol] name event name # @param [String, Symbol] name event name
# @param [Object] object handling the event, ie implement the function name
# @return handler # @return handler
def on(name, &handler) def register_event(name, handler)
event_table[name] << handler event_table[name] << handler
handler handler
end end
def off(name, handler) def unregister_event(name, handler)
event_table[name].delete handler event_table[name].delete handler
end end
@ -30,6 +32,6 @@ module Eventable
# #
# @param [String, Symbol] name event name to trigger # @param [String, Symbol] name event name to trigger
def trigger(name, *args) def trigger(name, *args)
event_table[name].each { |handler| handler.call(*args) } event_table[name].each { |handler| handler.send( name.to_sym , *args) }
end end
end end

View File

@ -17,18 +17,26 @@ class Interpreter
end end
def set_block block def set_block block
return if @block == block return if @block == block
old = @block
@block = block @block = block
trigger(:block, block) trigger(:block_changed , old , block)
set_instruction block.codes.first set_instruction block.codes.first
end end
def set_instruction i def set_instruction i
return if @instruction == i return if @instruction == i
old = @instruction
@instruction = i @instruction = i
trigger(:instruction, i) trigger(:instruction_changed, old , i)
end end
def tick def tick
name = @instruction.class.name.split("::").last name = @instruction.class.name.split("::").last
send "execute_#{name}" fetch = send "execute_#{name}"
return unless fetch
get_next_intruction
end end
def execute_Branch
target = @instruction.to
set_block target
end
end end