import asyncio import copy from deepmerge import always_merger STATE = {"initialized": 0} state_lock = asyncio.Lock() NOTIFY_CALLBACKS = [] def register_callback(callback): global NOTIFY_CALLBACKS NOTIFY_CALLBACKS = NOTIFY_CALLBACKS + [callback] def unregister_callback(callback): NOTIFY_CALLBACK = [ x for x in NOTIFY_CALLBACKS if x != callback ] async def mutate(delta): global STATE async with state_lock: old_state = copy.deepcopy(STATE) STATE = always_merger.merge(STATE, delta) for callback in NOTIFY_CALLBACKS: await callback(old_state, STATE) async def get_data(): global STATE async with state_lock: return STATE async def inc_initialized(): global STATE async with state_lock: STATE["initialized"] += 1