Fusion icon indicating copy to clipboard operation
Fusion copied to clipboard

Allow immediate invokation for `Observer:onChange()`

Open dphfox opened this issue 2 years ago • 1 comments

Oftentimes the same function will be used for initialising an observer and updating it in response to a change:

local theValue = Value(5)

local function updateFoo()
    foo.Color = theValue:get()
end

updateFoo()
Observer(theValue):onChange(updateFoo)

Sometimes this may not be expressed explicitly:

local theValue = Value(5)

foo.Color = theValue:get()
Observer(theValue):onChange(function()
    foo.Color = theValue:get()
end)

To reduce code duplication and the potential for errors, we could introduce a shorthand which immediately invokes the callback function in a spawned thread:

local theValue = Value(5)

Observer(theValue):onBind(function()
    foo.Color = theValue:get()
end)

dphfox avatar May 30 '22 22:05 dphfox

I have a wrapper function for just this, and I basically use it in 99% of my Observer uses, so it'd be really nice addition!

Zyrakia avatar May 31 '22 07:05 Zyrakia

This can be closed due to the above being merged!

krypt102 avatar Feb 01 '23 23:02 krypt102