Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ruby/hyper-state/lib/hyperstack/state/observable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def self.included(base)
Internal::Receiver.mount(self, *args, &block)
end
base.send(:"define_#{kind}", :observe) do |*args, &block|
result = block && block.call || args.last
result = block ? block.call : args.last
Internal::State::Mapper.observed! self
result
end
base.send(:"define_#{kind}", :mutate) do |*args, &block|
# any args will be ignored thus allowing us to say `mutate @foo = 123, @bar[:x] = 7` etc
result = block && block.call || args.last
result = block ? block.call : args.last
Internal::State::Mapper.mutated! self
result
end
Expand Down
9 changes: 9 additions & 0 deletions ruby/hyper-state/spec/api/observable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Store
store.instance_eval { @var = 12 }
expect(store.instance_eval { observe { @var } }).to eq(12)
end
it "can be passed a block that returns a falsy value" do
expect(Hyperstack::Internal::State::Mapper).to receive(:observed!).with(store)
store.instance_eval { @var = false }
expect(store.instance_eval { observe { @var } }).to eq(false)
end
it "can be passed args" do
expect(Hyperstack::Internal::State::Mapper).to receive(:observed!).with(store)
store.instance_eval { @var = 12 }
Expand All @@ -30,6 +35,10 @@ class Store
expect(Hyperstack::Internal::State::Mapper).to receive(:mutated!).with(store)
expect(store.instance_eval { mutate { @var = 12 } }).to eq(12)
end
it "can be passed a block that returns a falsy value" do
expect(Hyperstack::Internal::State::Mapper).to receive(:mutated!).with(store)
expect(store.instance_eval { mutate { @var = false } }).to eq(false)
end
it "can be passed args" do
expect(Hyperstack::Internal::State::Mapper).to receive(:mutated!).with(store)
expect(store.instance_eval { mutate @other_var = 13, @var = 12 }).to eq(12)
Expand Down