Sha256: c63e15e15c9711c38735aaa061572bef2bc327e5d710ff4baf745595f50cad87

Contents?: true

Size: 796 Bytes

Versions: 5

Compression:

Stored size: 796 Bytes

Contents

require 'promise'

module Ovto
  class WiredActions
    def initialize(actions, app, runtime)
      @actions, @app, @runtime = actions, app, runtime
    end

    def method_missing(name, args_hash={})
      invoke_action(name, args_hash)
    end

    def respond_to?(name)
      @actions.respond_to?(name)
    end

    private

    # Call action and schedule rendering
    def invoke_action(name, args_hash)
      kwargs = {state: @app.state}.merge(args_hash)
      state_diff = @actions.__send__(name, **kwargs)
      return if state_diff.nil? || state_diff.is_a?(Promise) || `!!state_diff.then`

      new_state = @app.state.merge(state_diff)
      if new_state != @app.state
        @runtime.scheduleRender
        @app._set_state(new_state)
      end
      return new_state
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ovto-0.3.0 lib/ovto/wired_actions.rb
ovto-0.2.3 lib/ovto/wired_actions.rb
ovto-0.2.2 lib/ovto/wired_actions.rb
ovto-0.2.1 lib/ovto/wired_actions.rb
ovto-0.2.0 lib/ovto/wired_actions.rb