Skip to content

Instantly share code, notes, and snippets.

@finetjul
Created January 28, 2025 15:33
Show Gist options
  • Save finetjul/f9ce2363c20fcc15a560eefc444a11e1 to your computer and use it in GitHub Desktop.
Save finetjul/f9ce2363c20fcc15a560eefc444a11e1 to your computer and use it in GitHub Desktop.
Tests for state change

When flush happens ?

state.a = 2 # assignations do not flush
state.update({'a': 1, 'b': 0})  # updates do not flush
state.dirty("c")  # `dirty()` does not flush
state.flush() # flush happens here

with state:
    state.d = 3
    state.e = "lion"
# flush happens here, on `with` exit

state.f = 6
# will eventually automatically be flushed by the trame asyncio event loop

Equality #1

Does the last flush() triggers ?

state.a = 0
state.flush()
state.a = 1
state.a = 0
state.flush()

→ no, because old and new values are the same: 0 == 0

Reference #1

Does the last flush() triggers ?

state.a = []
state.flush()
state.a.append(10)
state.flush()

→ no, because a did not change: ref([10]) == ref([10]]). To force a flush, use dirty("a")

Reference #2

Does the last flush() triggers ?

state.a = []
state.flush()
state.a.append(10)
state.dirty("a")
state.flush()

→ yes, because dirty() was called: ref([10]) == ref([10]]) but a is dirty

Reference #3

Does the last flush() triggers b ?

state.a = []
state.b = state.a
state.flush()
state.dirty("a")
state.flush()

→ no, because dirty() was called on a only.

Equality #2

Does the last flush() triggers ?

state.a = [0]
state.flush()
state.a = [0]
state.flush()

→ no, because old and new values are the same: ref([0]) != ref([0]) but [0] == [0]

Equality #3

Does the last flush() triggers ?

state.a = (3.14957, 42)
state.flush()
state.a = (3.14957 + 0, 42)
state.flush()

→ no, because old and new values are the same: (3.14957, 42) == (3.14957, 42)

Dirty #1

Trigger or not on last flush ?

state.a = 0
state.flush()
state.dirty("a")
state.flush()

→ yes, triggers because a is dirty

Dirty #2

Trigger or not on last flush ?

state.a = 0
state.a = flush()
state.a = 1
state.dirty("a")
state.a = 0
state.flush()

→ no trigger because old and new values are compared

Recursivity #1

Infinite loop or not ? If not, what state.a is worth after the first flush()?

@state.change("a")
def on_change(a, **kwargs):
    state.a += 1
    state.flush()

state.a = 1
state.flush()

→ ∞ infinite recursive loop

Recursivity #2

Infinite loop or not ? If not, what state.a is worth after the first flush()?

@state.change("a")
def on_change(a, **kwargs):
    state.a += 1

state.a = 1
state.flush()

→ ∞ infinite recursive loop

Recursivity #3

Infinite loop or not ? If not, what state.a and state.b are worth after the first flush()?

@state.change("a")
def on_a_change(a, **kwargs):
    state.b = a + 1

@state.change("b")
def on_b_change(b, **kwargs):
    state.a = b + 1

state.a = 1
state.flush()

→ ∞ infinite recursive loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment