# Mutable values with multiple setters "bounce" forever

**URL:** https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560
**Category:** Uncategorized
**Created:** [March 26, 2018, 9:48pm UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560 "2018-03-26T21:48:13Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![BBischof](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bbischof/32/419_2.png) [@BBischof](https://talk.observablehq.com/u/BBischof)
#### Post date: [March 26, 2018, 9:48pm UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/1 "2018-03-26T21:48:13Z")

</div>

I have a notebook [here](https://beta.observablehq.com/@bbischof/images-of-monomials-over-z_p-mod-n) where I have a few variables set by sliders, and elsewhere I have some increment and decrement buttons.

I’ve noticed that if I adjust the values quickly, sometimes they get caught in loops and the value will bounce back and forth. Not only do I then lose the ability to control that variable, but sometimes the entire notebook gets stuck and I have to refresh.

Kudos to the team for this not exploding my browser when it happens, but it’s still annoying.

Thanks!

---

<div class="post-metadata">

### Author: ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)
#### Post date: [March 26, 2018, 10:40pm UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/2 "2018-03-26T22:40:07Z")

</div>

Hello! The problem here is a circular definition:

```auto
{
  if (p !== mutable p_value) {
    mutable p_value = p;
    viewof p.input.p_value = p;
    viewof p.dispatchEvent(new CustomEvent("input"));
  }
}

```

The above cell references _p_ and _viewof p_, so it will run whenever either of those change. But it also sets the value of _p_ (indirectly) by dispatching an _input_ event on _viewof p_. And, it sets _mutable p\_value_, which triggers _viewof p_ to be re-initialized, because _viewof p_ depends on _p\_value_.

You should avoid circular definitions. I’d delete the _mutable p\_value_ cell and instead use only `viewof`. Make _viewof p_ the sole definition of the value of _p_, rather than trying to synchronize a view and a mutable. Here’s a short notebook showing how to mutate a view such as a slider from other cells:

> **[Mutating Views](https://observablehq.com/@mbostock/mutating-views)**
>
> An Observable notebook by Mike Bostock.

Alternatively, you could use a custom view as your definition of _p_. Here’s my notebook on custom views:

> **[Views are Mutable Values](https://observablehq.com/@mbostock/views-are-mutable-values)**
>
> An Observable notebook by Mike Bostock.

With a custom view, both the slider and the buttons would be considered “secondary” views: they would mutate the primary view, and listen non-reactively to the main view for value changes.

---

<div class="post-metadata">

### Author: ![BBischof](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bbischof/32/419_2.png) [@BBischof](https://talk.observablehq.com/u/BBischof)
#### Post date: [March 27, 2018, 12:17am UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/3 "2018-03-27T00:17:46Z")

</div>

This is definitely where I expected the issue to lie, but one of the reasons I got into all this mess with values and variables is because I couldn’t figure out how to:

- update range of other variables when p changes
- leave them alone if they were still within the bounds

Before, they were changing back to their default every time p changed. Additionally, I couldn’t get the max range to update without rerunning the cells.

---

<div class="post-metadata">

### Author: ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)
#### Post date: [March 27, 2018, 2:32am UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/4 "2018-03-27T02:32:40Z")

</div>

Can you elaborate on what you mean by “update range of other variables when p changes”?

---

<div class="post-metadata">

### Author: ![BBischof](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bbischof/32/419_2.png) [@BBischof](https://talk.observablehq.com/u/BBischof)
#### Post date: [March 27, 2018, 4:18am UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/5 "2018-03-27T04:18:41Z")

</div>

Yes, sorry.

When the value of p changes, it impacts the possible values for n, a, and k; n can be no larger than p-1, a can be no larger than (p-1)/2, and k can be no larger than p-1. Thus I’d like to adjust the range sliders.

---

<div class="post-metadata">

### Author: ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)
#### Post date: [March 27, 2018, 7:08pm UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/6 "2018-03-27T19:08:27Z")

</div>

I recommend using `this`:

> **[Sticky Views](https://observablehq.com/@mbostock/sticky-views)**
>
> An Observable notebook by Mike Bostock.

---

<div class="post-metadata">

### Author: ![BBischof](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bbischof/32/419_2.png) [@BBischof](https://talk.observablehq.com/u/BBischof)
#### Post date: [March 27, 2018, 10:01pm UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/7 "2018-03-27T22:01:07Z")

</div>

This is awesome! Thank you!

---

<div class="post-metadata">

### Author: ![BBischof](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bbischof/32/419_2.png) [@BBischof](https://talk.observablehq.com/u/BBischof)
#### Post date: [March 28, 2018, 6:36pm UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/8 "2018-03-28T18:36:41Z")

</div>

Closer, but still stuck actually. Using your example, I fixed up the sliders, which is great. But now my increment and decrement buttons dont work(if I try to increment and decrement viewof I get a SyntaxError: Assigning to r value). Following the example from the Change Log and [here](https://beta.observablehq.com/@jashkenas/manual-mutable-values) I tried making them mutable, which isn’t compatible with the sliders. 🤔

---

<div class="post-metadata">

### Author: ![jashkenas](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/jashkenas/32/1778_2.png) [@jashkenas](https://talk.observablehq.com/u/jashkenas)
#### Post date: [March 28, 2018, 7:55pm UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/9 "2018-03-28T19:55:48Z")

</div>

Hey Bryan.

Here’s a fork of your sketch that implements the pattern Mike described above — avoiding the circularity in the definitions:

> **[Images of monomials over Z\_p mod n (Fork for the forums)](https://observablehq.com/@jashkenas/images-of-monomials-over-z_p-mod-n-fork-for-the-forums)**
>
> An Observable notebook by Jeremy Ashkenas.

Now, the values (p, n, a …) are the single sources of truth, and the views are mutated directly by the increment and decrement buttons.

You’ll notice two new helper functions: `update`, and `incdec`, to DRY up the code a bit for mutating the slider views.

Let me know if anything in there isn’t making sense.

Cheers,  
Jeremy

---

<div class="post-metadata">

### Author: ![BBischof](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bbischof/32/419_2.png) [@BBischof](https://talk.observablehq.com/u/BBischof)
#### Post date: [March 28, 2018, 11:00pm UTC](https://talk.observablehq.com/t/mutable-values-with-multiple-setters-bounce-forever/560/10 "2018-03-28T23:00:54Z")

</div>

That was extremely helpful, thank you.

Here’s an updated one. Open to other feedback if you have it!
