improve state merger, always override lists
This commit is contained in:
@@ -135,8 +135,10 @@ def set_immidiate(framecount, channels, update, params):
|
|||||||
async def handle_state_change(old, new):
|
async def handle_state_change(old, new):
|
||||||
global effect_chain
|
global effect_chain
|
||||||
global framecount
|
global framecount
|
||||||
effect_chain = deserialize_chain(new["chain"])
|
deserialized = deserialize_chain(new["chain"])
|
||||||
await state.mutate(serialize_internal_state())
|
effect_chain = deserialized
|
||||||
|
serialized = serialize_internal_state()
|
||||||
|
await state.mutate(serialized)
|
||||||
|
|
||||||
async def main(target_ips):
|
async def main(target_ips):
|
||||||
global effect_chain
|
global effect_chain
|
||||||
|
|||||||
24
state.py
24
state.py
@@ -1,8 +1,10 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import copy
|
import copy
|
||||||
from deepmerge import always_merger
|
import time
|
||||||
|
from random import random
|
||||||
|
from deepmerge import always_merger, Merger
|
||||||
|
|
||||||
STATE = {"initialized": 0}
|
STATE = {"initialized": 0, "generation": "invalid"}
|
||||||
|
|
||||||
ON_INPUT_CALLBACKS = []
|
ON_INPUT_CALLBACKS = []
|
||||||
ON_CHANGE_CALLBACKS = []
|
ON_CHANGE_CALLBACKS = []
|
||||||
@@ -21,12 +23,23 @@ def register_on_input_callback(callback):
|
|||||||
def unregister_on_input_callback(callback):
|
def unregister_on_input_callback(callback):
|
||||||
ON_INPUT_CALLBACK = [ x for x in ON_INPUT_CALLBACKS if x != callback ]
|
ON_INPUT_CALLBACK = [ x for x in ON_INPUT_CALLBACKS if x != callback ]
|
||||||
|
|
||||||
|
# TODO: FIX ENTRIES HERE BY UNIQUE EFFECT ID OR SOMETHING
|
||||||
async def mutate(delta):
|
async def mutate(delta):
|
||||||
global STATE
|
global STATE
|
||||||
|
my_merger = Merger(
|
||||||
|
[
|
||||||
|
(list, ["override"]),
|
||||||
|
(dict, ["merge"])
|
||||||
|
],
|
||||||
|
["override"],
|
||||||
|
["override"]
|
||||||
|
)
|
||||||
|
|
||||||
old_state = copy.deepcopy(STATE)
|
old_state = copy.deepcopy(STATE)
|
||||||
STATE = always_merger.merge(STATE, delta)
|
STATE = my_merger.merge(STATE, delta)
|
||||||
|
STATE["generation"] = calculate_checksum()
|
||||||
for callback in ON_CHANGE_CALLBACKS:
|
for callback in ON_CHANGE_CALLBACKS:
|
||||||
await callback(old_state, delta)
|
await callback(old_state, STATE)
|
||||||
|
|
||||||
async def propagate(delta):
|
async def propagate(delta):
|
||||||
global STATE
|
global STATE
|
||||||
@@ -38,6 +51,9 @@ def get_data():
|
|||||||
global STATE
|
global STATE
|
||||||
return STATE
|
return STATE
|
||||||
|
|
||||||
|
def calculate_checksum():
|
||||||
|
return f"{time.time()}-${random()}"
|
||||||
|
|
||||||
async def inc_initialized():
|
async def inc_initialized():
|
||||||
global STATE
|
global STATE
|
||||||
await mutate({"initialized": STATE["initialized"]+1 })
|
await mutate({"initialized": STATE["initialized"]+1 })
|
||||||
|
|||||||
Reference in New Issue
Block a user