anharu2394 / rubelm

[under development] A Opal library for building web applications
10 stars 1 forks source link

Actionの仕様を決める(decide the specification of Action ) #5

Open anharu2394 opened 5 years ago

anharu2394 commented 5 years ago

Actionの仕様が迷う。どうしようか。

anharu2394 commented 5 years ago

こうとか(hyperapp風

actions = {
  up: -> { |value|
     -> |state| {
      { count: state.count + value }
    }
  }
}
actions = {
  up: -> { |value|
    action do |state|
      { count: state.count + value }
    end
  }
}
anharu2394 commented 5 years ago

moduleを使うとか

Module Actions
  def up(value)
    action do |state|
       { count: state.count + value }
    end
  end
  def set_state(value)
    action do  # 必ずしもstateを取らなくていい
     { count: value}
   end
  end
end
anharu2394 commented 5 years ago

action methodでブロック取るにはいい感じなんじゃないかと思ってる。 stateはオプションで取ればいいし。

action do |state|
    { count state.count + value }
end
anharu2394 commented 5 years ago

前のstateが要らないならただhashを返すだけでもいいか。

anharu2394 commented 5 years ago

Moduleの場合は、module methodにしないと、actions.upのようにできない。それを設定するのはめんどいので、好ましくない。 Classにすればインスタンスを作ると、actions.upとできるのでClassにしようかな。

Class Actions
  def up(value)
    action do |state|
       { count: state.count + value }
    end
  end
end
anharu2394 commented 5 years ago

things action need are previous state(optional), preload(optional), eventObject(optional).

issues

anharu2394 commented 5 years ago
    class Actions
      def up(value)
        action do |state|
          { count: state.count + value }
        end
      end
    end
    view = component do |state, actions|
      div({}, [
            div({}, 'Count:' + state.count.to_s),
            input(type: 'button', onclick: actions.up(1), class: 'button', value: 'Set count')
          ])
    end

this example, can pass state and preload, but cannot pass eventObject

anharu2394 commented 5 years ago

so,this like can all elements.

    class Actions
      def up(value)
        action do |state, event|
          state.merge({ count: state.count + value })
        end
      end
    end
    view = component do |state, actions|
      div({}, [
            div({}, 'Count:' + state.count.to_s),
            input(type: 'button', onclick: actions.up(1), class: 'button', value: 'Set count')
          ])
    end
anharu2394 commented 5 years ago
   class Actions
      def up(value)
        action do |props|
          # props == { state:{ ~~~~}, event:{ ~~~}, actions: {~~}}
          state.merge({ count: props.state.count + value })
        end
      end
    end
    view = component do |state, actions|
      div({}, [
            div({}, 'Count:' + state.count.to_s),
            input(type: 'button', onclick: actions.up(1), class: 'button', value: 'Set count')
          ])
    end
anharu2394 commented 5 years ago
action do |state, event, actions|
end