Skip to content

observe block won't return false #322

Closed
@adamcreekroad

Description

@adamcreekroad

Manually defining state getters with an observe block will return nil when the return value is false

class MyStore
  include Hyperstack::State::Observable

  def my_state
    observe { @my_state }
  end

  def my_state=(value)
    mutate { @my_state = value }
  end
end

store = MyStore.new

store.my_state
# => nil

store.my_state = true
# => true

store.my_state = false
#=> nil

Looking at the code for observe, if the block doesn't return a truthy value it instead returns the last arg passed in

# lib/hyperstack/state/observable.rb:14
          base.send(:"define_#{kind}", :observe) do |*args, &block|
            result = block && block.call || args.last
            Internal::State::Mapper.observed! self
            result
          end

I think it should instead set to the result to block.call if the block exists, otherwise use args.last instead of relying on the return value of the block to determine whether or not to use args.last

# lib/hyperstack/state/observable.rb:14
          base.send(:"define_#{kind}", :observe) do |*args, &block|
            result = block_given? ? block.call : args.last
            Internal::State::Mapper.observed! self
            result
          end

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions