From 0cdd5fdf13dda603df383ce06da6481c21007eb0 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 24 Jul 2015 10:01:01 +0300 Subject: [PATCH] registering events with objects not procs to make unregistering easier --- app/block_view.rb | 14 ++++++++++++-- lib/eventable.rb | 10 ++++++---- lib/interpreter.rb | 16 ++++++++++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/block_view.rb b/app/block_view.rb index ba89dbb..910ac42 100644 --- a/app/block_view.rb +++ b/app/block_view.rb @@ -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 diff --git a/lib/eventable.rb b/lib/eventable.rb index 6f632ac..a727430 100644 --- a/lib/eventable.rb +++ b/lib/eventable.rb @@ -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 diff --git a/lib/interpreter.rb b/lib/interpreter.rb index d9e32c0..3dcbf6f 100644 --- a/lib/interpreter.rb +++ b/lib/interpreter.rb @@ -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