Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronized updates #109

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ffreyer
Copy link
Contributor

@ffreyer ffreyer commented Jun 3, 2023

This is an attempt at a more formal solution to problems such as

input1 = Observable([1, 2])
input2 = Observable([1, 2])
output = Observable([(1, 1)])
f = onany(input1, input2) do a, b
    output[] = tuple.(a, b)
    return  
end

input1[] = [1, 2, 3]
input2[] = [1, 2, 3]

triggering a DimensionMismatch error due to input1 executing first. The current solution to this is to write to the Observable directly with input1.val = [1, 2, 3] first, and then trigger an update with the second observable as usual.

While that works it requires you to modify a field of the observable which isn't great in my opinion. In a more complex network of observables you may also need to trigger both observables to run everything you need, which will double up on updates attached to both.

This pr is adding a different way to handle this:

@combine_update begin
    input1[] = [1, 2, 3, 4]
    input2[] = [1, 2, 3, 4]
end

which lowers to

begin
    prepare_update!(input1, [1, 2, 3, 4])
    prepare_update!(input2, [1, 2, 3, 4])
    execute_update!(input1)
    execute_update!(input2)
end

This will update observable.val and mark listeners as out-of-date via prepare_update! and then update them through execute_update!. The latter will skip over listeners that have already been executed and exit when it hits a consuming listener (consuming now or before). So it is effectively the same as

input1.val = [1, 2, 3, 4]
input2.val = [1, 2, 3, 4]
notify(input1)
notify(input2)

except that it skips duplicate execution of listeners.

TODO:

  • 1.6 compat (const in struct)

@Datseris
Copy link

bump here, if this is a solution, perhaps it can go in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants