registering events with objects not procs
to make unregistering easier
This commit is contained in:
parent
8183dfd6b1
commit
0cdd5fdf13
@ -6,17 +6,27 @@ class BlockView
|
||||
include React::Component
|
||||
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
|
||||
div.row do
|
||||
div.col_md_6 do
|
||||
div.col_md_5 do
|
||||
SourceView :source => interpreter.block.codes.first.source
|
||||
end
|
||||
div.col_md_6 do
|
||||
div.col_md_5 do
|
||||
interpreter.block.codes.each do |code|
|
||||
code.to_s.br
|
||||
end
|
||||
end
|
||||
div.col_md_2 do
|
||||
button.btn.btn_default { "next" }.on(:click) { interpreter.tick }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -3,17 +3,19 @@
|
||||
module Eventable
|
||||
|
||||
# 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 [Object] object handling the event, ie implement the function name
|
||||
# @return handler
|
||||
def on(name, &handler)
|
||||
def register_event(name, handler)
|
||||
event_table[name] << handler
|
||||
handler
|
||||
end
|
||||
|
||||
def off(name, handler)
|
||||
def unregister_event(name, handler)
|
||||
event_table[name].delete handler
|
||||
end
|
||||
|
||||
@ -30,6 +32,6 @@ module Eventable
|
||||
#
|
||||
# @param [String, Symbol] name event name to trigger
|
||||
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
|
||||
|
@ -17,18 +17,26 @@ class Interpreter
|
||||
end
|
||||
def set_block block
|
||||
return if @block == block
|
||||
old = @block
|
||||
@block = block
|
||||
trigger(:block, block)
|
||||
set_instruction block.codes.first
|
||||
trigger(:block_changed , old , block)
|
||||
set_instruction block.codes.first
|
||||
end
|
||||
def set_instruction i
|
||||
return if @instruction == i
|
||||
old = @instruction
|
||||
@instruction = i
|
||||
trigger(:instruction, i)
|
||||
trigger(:instruction_changed, old , i)
|
||||
end
|
||||
def tick
|
||||
name = @instruction.class.name.split("::").last
|
||||
send "execute_#{name}"
|
||||
fetch = send "execute_#{name}"
|
||||
return unless fetch
|
||||
get_next_intruction
|
||||
end
|
||||
|
||||
def execute_Branch
|
||||
target = @instruction.to
|
||||
set_block target
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user