initial commit
This commit is contained in:
33
state.py
Normal file
33
state.py
Normal file
@@ -0,0 +1,33 @@
|
||||
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
|
||||
Reference in New Issue
Block a user