-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinator_mutations.py
More file actions
40 lines (33 loc) · 1021 Bytes
/
Copy pathcoordinator_mutations.py
File metadata and controls
40 lines (33 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from __future__ import annotations
from collections.abc import Callable
from typing import ParamSpec, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
class SimulationCoordinatorMutationDispatcher:
"""Runs coordinator mutations with the correct persistence behavior."""
def __init__(
self,
*,
flush_immediately: Callable[[], None],
schedule_deferred_persist: Callable[[], None],
) -> None:
self._flush_immediately = flush_immediately
self._schedule_deferred_persist = schedule_deferred_persist
def run_immediate(
self,
action: Callable[P, R],
*args: P.args,
**kwargs: P.kwargs,
) -> R:
result = action(*args, **kwargs)
self._flush_immediately()
return result
def run_deferred(
self,
action: Callable[P, R],
*args: P.args,
**kwargs: P.kwargs,
) -> R:
result = action(*args, **kwargs)
self._schedule_deferred_persist()
return result