Spaces:
Running
Running
Add a delayed execution mechanism for parameter edits.
Browse files
server/crdt.py
CHANGED
@@ -50,7 +50,7 @@ class WebsocketServer(pycrdt_websocket.WebsocketServer):
|
|
50 |
room.ws = ws
|
51 |
|
52 |
def on_change(changes):
|
53 |
-
asyncio.create_task(workspace_changed(changes, ws))
|
54 |
|
55 |
ws.observe_deep(on_change)
|
56 |
return room
|
@@ -69,6 +69,7 @@ last_ws_input = None
|
|
69 |
def clean_input(ws_pyd):
|
70 |
for node in ws_pyd.nodes:
|
71 |
node.data.display = None
|
|
|
72 |
node.position.x = 0
|
73 |
node.position.y = 0
|
74 |
if node.model_extra:
|
@@ -121,15 +122,39 @@ def try_to_load_workspace(ws, name):
|
|
121 |
crdt_update(ws, ws_pyd.model_dump(), boxes={"display"})
|
122 |
|
123 |
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
126 |
from . import workspace
|
127 |
|
128 |
ws_pyd = workspace.Workspace.model_validate(ws_crdt.to_py())
|
129 |
clean_input(ws_pyd)
|
130 |
-
if ws_pyd ==
|
131 |
return
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
await workspace.execute(ws_pyd)
|
134 |
for nc, np in zip(ws_crdt["nodes"], ws_pyd.nodes):
|
135 |
if "data" not in nc:
|
@@ -147,7 +172,7 @@ async def lifespan(app):
|
|
147 |
)
|
148 |
async with websocket_server:
|
149 |
yield
|
150 |
-
print("closing websocket server
|
151 |
|
152 |
|
153 |
def sanitize_path(path):
|
|
|
50 |
room.ws = ws
|
51 |
|
52 |
def on_change(changes):
|
53 |
+
asyncio.create_task(workspace_changed(name, changes, ws))
|
54 |
|
55 |
ws.observe_deep(on_change)
|
56 |
return room
|
|
|
69 |
def clean_input(ws_pyd):
|
70 |
for node in ws_pyd.nodes:
|
71 |
node.data.display = None
|
72 |
+
node.data.error = None
|
73 |
node.position.x = 0
|
74 |
node.position.y = 0
|
75 |
if node.model_extra:
|
|
|
122 |
crdt_update(ws, ws_pyd.model_dump(), boxes={"display"})
|
123 |
|
124 |
|
125 |
+
last_known_versions = {}
|
126 |
+
delayed_executions = {}
|
127 |
+
|
128 |
+
|
129 |
+
async def workspace_changed(name, changes, ws_crdt):
|
130 |
from . import workspace
|
131 |
|
132 |
ws_pyd = workspace.Workspace.model_validate(ws_crdt.to_py())
|
133 |
clean_input(ws_pyd)
|
134 |
+
if ws_pyd == last_known_versions.get(name):
|
135 |
return
|
136 |
+
last_known_versions[name] = ws_pyd.model_copy(deep=True)
|
137 |
+
if name in delayed_executions:
|
138 |
+
delayed_executions[name].cancel()
|
139 |
+
delay = min(
|
140 |
+
change.keys.get("__execution_delay", {}).get("newValue", 0)
|
141 |
+
for change in changes
|
142 |
+
)
|
143 |
+
if delay > 0:
|
144 |
+
task = asyncio.create_task(execute(ws_crdt, ws_pyd, delay))
|
145 |
+
delayed_executions[name] = task
|
146 |
+
else:
|
147 |
+
await execute(ws_crdt, ws_pyd)
|
148 |
+
|
149 |
+
|
150 |
+
async def execute(ws_crdt, ws_pyd, delay=0):
|
151 |
+
from . import workspace
|
152 |
+
|
153 |
+
if delay:
|
154 |
+
try:
|
155 |
+
await asyncio.sleep(delay)
|
156 |
+
except asyncio.CancelledError:
|
157 |
+
return
|
158 |
await workspace.execute(ws_pyd)
|
159 |
for nc, np in zip(ws_crdt["nodes"], ws_pyd.nodes):
|
160 |
if "data" not in nc:
|
|
|
172 |
)
|
173 |
async with websocket_server:
|
174 |
yield
|
175 |
+
print("closing websocket server")
|
176 |
|
177 |
|
178 |
def sanitize_path(path):
|
web/src/workspace/Workspace.tsx
CHANGED
@@ -92,12 +92,20 @@ function LynxKiteFlow() {
|
|
92 |
if (!node) continue;
|
93 |
// Position events sometimes come with NaN values. Ignore them.
|
94 |
if (ch.type === 'position' && !isNaN(ch.position.x) && !isNaN(ch.position.y)) {
|
95 |
-
|
|
|
|
|
96 |
} else if (ch.type === 'select') {
|
97 |
} else if (ch.type === 'dimensions') {
|
98 |
} else if (ch.type === 'replace') {
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
} else {
|
102 |
console.log('Unknown node change', ch);
|
103 |
}
|
|
|
92 |
if (!node) continue;
|
93 |
// Position events sometimes come with NaN values. Ignore them.
|
94 |
if (ch.type === 'position' && !isNaN(ch.position.x) && !isNaN(ch.position.y)) {
|
95 |
+
getYjsDoc(state).transact(() => {
|
96 |
+
Object.assign(node.position, ch.position);
|
97 |
+
});
|
98 |
} else if (ch.type === 'select') {
|
99 |
} else if (ch.type === 'dimensions') {
|
100 |
} else if (ch.type === 'replace') {
|
101 |
+
// Ideally we would only update the parameter that changed. But ReactFlow does not give us that detail.
|
102 |
+
getYjsDoc(state).transact(() => {
|
103 |
+
Object.assign(node.data, {
|
104 |
+
collapsed: ch.item.data.collapsed,
|
105 |
+
params: { ...ch.item.data.params },
|
106 |
+
__execution_delay: ch.item.data.__execution_delay,
|
107 |
+
});
|
108 |
+
});
|
109 |
} else {
|
110 |
console.log('Unknown node change', ch);
|
111 |
}
|
web/src/workspace/nodes/NodeParameter.tsx
CHANGED
@@ -42,7 +42,8 @@ export default function NodeParameter({ name, value, meta, onChange }) {
|
|
42 |
<ParamName name={name} />
|
43 |
<input className="input input-bordered w-full max-w-xs"
|
44 |
value={value || ""}
|
45 |
-
onChange={(evt) => onChange(evt.currentTarget.value)}
|
|
|
46 |
/>
|
47 |
</>
|
48 |
}
|
|
|
42 |
<ParamName name={name} />
|
43 |
<input className="input input-bordered w-full max-w-xs"
|
44 |
value={value || ""}
|
45 |
+
onChange={(evt) => onChange(evt.currentTarget.value, { delay: 2 })}
|
46 |
+
onBlur={(evt) => onChange(evt.currentTarget.value, { delay: 0 })}
|
47 |
/>
|
48 |
</>
|
49 |
}
|
web/src/workspace/nodes/NodeWithParams.tsx
CHANGED
@@ -1,15 +1,14 @@
|
|
1 |
-
import { useContext } from 'react';
|
2 |
-
import { LynxKiteState } from '../LynxKiteState';
|
3 |
import LynxKiteNode from './LynxKiteNode';
|
4 |
import { useReactFlow } from '@xyflow/react';
|
5 |
import NodeParameter from './NodeParameter';
|
6 |
|
|
|
|
|
7 |
function NodeWithParams(props: any) {
|
8 |
const reactFlow = useReactFlow();
|
9 |
const metaParams = props.data.meta?.params;
|
10 |
-
|
11 |
-
|
12 |
-
reactFlow.updateNodeData(props.id, { params: { ...props.data.params, [name]: newValue } });
|
13 |
}
|
14 |
const params = props.data?.params ? Object.entries(props.data.params) : [];
|
15 |
|
@@ -21,7 +20,7 @@ function NodeWithParams(props: any) {
|
|
21 |
key={name}
|
22 |
value={value}
|
23 |
meta={metaParams?.[name]}
|
24 |
-
onChange={(value: any) => setParam(name, value)}
|
25 |
/>
|
26 |
)}
|
27 |
</LynxKiteNode >
|
|
|
|
|
|
|
1 |
import LynxKiteNode from './LynxKiteNode';
|
2 |
import { useReactFlow } from '@xyflow/react';
|
3 |
import NodeParameter from './NodeParameter';
|
4 |
|
5 |
+
export type UpdateOptions = { delay?: number };
|
6 |
+
|
7 |
function NodeWithParams(props: any) {
|
8 |
const reactFlow = useReactFlow();
|
9 |
const metaParams = props.data.meta?.params;
|
10 |
+
function setParam(name: string, newValue: any, opts: UpdateOptions) {
|
11 |
+
reactFlow.updateNodeData(props.id, { params: { ...props.data.params, [name]: newValue }, __execution_delay: opts.delay || 0 });
|
|
|
12 |
}
|
13 |
const params = props.data?.params ? Object.entries(props.data.params) : [];
|
14 |
|
|
|
20 |
key={name}
|
21 |
value={value}
|
22 |
meta={metaParams?.[name]}
|
23 |
+
onChange={(value: any, opts?: UpdateOptions) => setParam(name, value, opts || {})}
|
24 |
/>
|
25 |
)}
|
26 |
</LynxKiteNode >
|