Skip to content

Commit 1f6030f

Browse files
author
Kevin
committed
fix: refactor useAgentPosition to return current position and previous state, update related components
1 parent d1ccdbf commit 1f6030f

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

src/Status.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function Dot({ color }: { color: string }) {
4141
}
4242

4343
export function Status({ id }: { id: number }) {
44-
const a = useAgentInfo(id);
44+
const { value: a } = useAgentInfo(id);
4545
const cls = useCss({
4646
display: "flex",
4747
flexDirection: "column",

src/client/run.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export function useSolutionContents() {
4545
}),
4646
};
4747
},
48-
enabled: !!file,
4948
});
5049
}
5150

@@ -113,6 +112,9 @@ export function useRun() {
113112
case "message":
114113
setLog((t) => slice([...t, d.content], -100));
115114
break;
115+
case "adg_error":
116+
setLog((t) => [...t, `ADG Error: ${d.info}`]);
117+
break;
116118
case "adg_progress":
117119
adg = d;
118120
break;

src/client/store.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@ import { Mutex } from "async-mutex";
33
import { clear, get, set } from "idb-keyval";
44
import { atom, useAtom, useAtomValue, useSetAtom } from "jotai";
55
import { selectAtom } from "jotai/utils";
6-
import { floor, fromPairs, isEqual, round, throttle, zip } from "lodash";
6+
import {
7+
each,
8+
floor,
9+
fromPairs,
10+
isEqual,
11+
isUndefined,
12+
round,
13+
throttle,
14+
zip,
15+
} from "lodash";
716
import { store } from "main";
817
import { useEffect, useMemo, useState } from "react";
918
import { useEffectOnce } from "react-use";
1019
import { AdgProgress, Step } from "smart";
1120
import { lerp, lerpRadians } from "utils";
1221
import { useSpeed } from "./play";
22+
import { selectionAtom } from "./selection";
1323

1424
const CHUNK_SIZE = 256;
1525

@@ -76,7 +86,7 @@ export function usePreviousDefined<T>(t?: T) {
7686
useEffect(() => {
7787
if (t) setPrev(t);
7888
}, [t]);
79-
return prev;
89+
return { value: prev, current: t, isPrevious: isUndefined(t) && prev === t };
8090
}
8191

8292
const alpha = (n: number) => 0.1 - 1 / (0.1 * n + 1) + 1;
@@ -104,7 +114,7 @@ export const useAgentInfo = (i: number) => {
104114
)
105115
)
106116
);
107-
}
117+
};
108118

109119
function dist(a: [number, number, number], b: [number, number, number]) {
110120
return Math.sqrt(
@@ -113,7 +123,7 @@ function dist(a: [number, number, number], b: [number, number, number]) {
113123
}
114124

115125
export const useAgentPosition = (i: number) => {
116-
const v0 = useAgentInfo(i);
126+
const { value: v0, isPrevious } = useAgentInfo(i);
117127
const [speed] = useSpeed();
118128
const [current, setCurrent] = useState(v0);
119129
useFrame(() => {
@@ -136,7 +146,7 @@ export const useAgentPosition = (i: number) => {
136146
});
137147
}
138148
});
139-
return current;
149+
return { current, isPrevious };
140150
};
141151

142152
// Load chunk by index
@@ -180,12 +190,17 @@ export const appendAtom = atom<null, [State[]], unknown>(
180190
export const clearAtom = atom<null, never[], unknown>(null, (_get, set) => {
181191
// Don't wait for this to complete
182192
m.runExclusive(async () => {
193+
each(cache, (v, k) => {
194+
delete cache[+k];
195+
});
196+
set(cacheAtom, {});
197+
set(timeAtom, 0);
198+
set(selectionAtom, new Set());
199+
set(lengthAtom, 0);
200+
set(timespanAtom, 0);
183201
await clear();
202+
await set(setCacheAtom);
184203
}, 0);
185-
set(cacheAtom, {});
186-
set(timeAtom, 0);
187-
set(lengthAtom, 0);
188-
set(timespanAtom, 0);
189204
});
190205

191206
export const useAppend = () => {
@@ -223,12 +238,17 @@ export function useAutoSyncChunks() {
223238
running = true;
224239
await setCache();
225240
running = false;
226-
}
227-
}, [setCache])
241+
};
242+
}, [setCache]);
228243
const b = useMemo(
229-
() => throttle(() => {
230-
c()
231-
}, 1000 / 15, { trailing: true, leading: false }),
244+
() =>
245+
throttle(
246+
() => {
247+
c();
248+
},
249+
1000 / 15,
250+
{ trailing: true, leading: false }
251+
),
232252
[c]
233253
);
234254
useEffect(() => {

src/components/Agent.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ function Constraint({
3232
from?: number;
3333
to?: number;
3434
} & Partial<ComponentProps<typeof AnimatedLine>>) {
35-
const { position: a = [0, 0, 0] as const } = useAgentPosition(from) ?? {};
36-
const { position: b = [0, 0, 0] as const } = useAgentPosition(to) ?? {};
35+
const {
36+
current: { position: a = [0, 0, 0] as [number, number, number] } = {},
37+
} = useAgentPosition(from) ?? {};
38+
const {
39+
current: { position: b = [0, 0, 0] as [number, number, number] } = {},
40+
} = useAgentPosition(to) ?? {};
3741

3842
const springs = useSpring({
3943
from: { offset: 0 },
@@ -90,7 +94,7 @@ export function Path({
9094
const src = head(path);
9195
const dest = last(path);
9296

93-
const a = useAgentPosition(id);
97+
const { current: a } = useAgentPosition(id);
9498

9599
const constrained = !!a?.constraints?.length;
96100

@@ -191,7 +195,7 @@ export function Agent({ i, ...props }: { i: number } & InstanceProps) {
191195
const [selected, toggleSelected] = useSelection();
192196
const [hovered, toggleHovered] = useBoolean(false);
193197

194-
const a = useAgentPosition(i);
198+
const { current: a } = useAgentPosition(i);
195199

196200
if (!a) {
197201
return undefined;

0 commit comments

Comments
 (0)