# Makie and observables

**URL:** <https://discourse.julialang.org/t/makie-and-observables/105261>\
**Category:** General Usage\
**Tags:** question, plotting, makie, observables\
**Created:** [October 21, 2023, 10:01am UTC](https://discourse.julialang.org/t/makie-and-observables/105261 "2023-10-21T10:01:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [October 21, 2023, 10:01am UTC](https://discourse.julialang.org/t/makie-and-observables/105261/1 "2023-10-21T10:01:35Z")

</div>

**Disclaimer : I’m totally unsure if this question is sound or not.**

In the [Makie - Problems with synchronous updates](https://docs.makie.org/stable/explanations/nodes/#problems_with_synchronous_updates) documentation, it is clearly stated that multiple updates of the plots can occur when only one really is needed, and that this situation can may be error-prone.

The strategy involving calling `x.val = ...` instead of `x[] = ...` allows to avoid this kind of situation, but first, these have to be properly detected.

So I was wondering, is there an _official_ or at least recommended way to detect such unwanted updates ? A real like example could be

- a custom recipe `myplot`
- `myplot` has an attribute `prop1`
- `prop1` is used to obtain an observable `o1`
- both `prop1` and `o1` are lifted to obtain another observable `o2`  
So far, if I understood correctly (but maybe I’m totally wrong), this should result in two updates
- `o1` and `o2` update due to `prop1` update
- `o2` update due to `o1` update

If I interpreted the `Observable` behavior correctly (which I’m totally unsure of), I think it would be practical to have something, probably a macro, for development purposes, that would result in the following behavior.

```julia
f,ax,p = @track_observable_updates myplot(...)
p.prop1 = 0

Updated observables :
- prop1 -> 0
    |- o1 -> v1
    |- o2 -> v21
- o1 -> v1
    |- o2 -> v22

```

Is this feasible, or at least useful compared to currently available tools ?  
I’m in the dark here. I’m guessing @jules @sdanisch or @ffreyer are the best suited to clarify that.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 21, 2023, 7:50pm UTC](https://discourse.julialang.org/t/makie-and-observables/105261/2 "2023-10-21T19:50:56Z")

</div>

One problem is that an observable doesn’t directly store all the necessary information to compute that graph. Consider:

```julia
o1 = Observable(1)
o2 = Observable(2)

on(o1) do val
    o2[] = val
end

```

Now you’d have to somehow analyze this closure’s code to detect that a triggering of `o2` is in there which would make the graph `o1 -> o2`. And I don’t think that’s easily possible.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [October 23, 2023, 12:39pm UTC](https://discourse.julialang.org/t/makie-and-observables/105261/3 "2023-10-23T12:39:01Z")

</div>

> [@jules](#):
>
> Now you’d have to somehow analyze this closure’s code

I’m very unfamiliar with macros, but isn’t it precisely their typical use case ? I mean, to be able to modify the code of their argument ?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 23, 2023, 1:15pm UTC](https://discourse.julialang.org/t/makie-and-observables/105261/4 "2023-10-23T13:15:31Z")

</div>

Macros would see an empty `setindex!` but they wouldn’t know what the argument types are.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [October 23, 2023, 1:38pm UTC](https://discourse.julialang.org/t/makie-and-observables/105261/5 "2023-10-23T13:38:01Z")

</div>

You are right indeed ! I’m wondering if retrieving all [`ObserverFunction`](https://juliagizmos.github.io/Observables.jl/stable/#Observables.ObserverFunction)s declared in a scope could help.

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [October 23, 2023, 2:47pm UTC](https://discourse.julialang.org/t/makie-and-observables/105261/6 "2023-10-23T14:47:12Z")

</div>

It should be possible to write a macro that converts something like

```julia
@track_updates on(o1) do val
    o2[] = val
end

```

to

```julia
track_updates!(o1, o2) # register o2 as dependent of o1
on(o1) do val
    o2[] = val
end

```

The only difficult part is to scan the `Expr` for `[]` calls in the `do` body, but this is doable using `MacroTools.jl`.  
And the `track_updates!` method should specialize to only track `Observables` and ignore things like `Ref`s.

However, this becomes tricky (or inaccurate) quite easily

```julia
@track_updates on(o1) do val
    if rand(Bool)
       o2[] = val
    else
       o3[] = val
    end
end

```
