@@ -3,13 +3,23 @@ import { Mutex } from "async-mutex";
33import { clear , get , set } from "idb-keyval" ;
44import { atom , useAtom , useAtomValue , useSetAtom } from "jotai" ;
55import { 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" ;
716import { store } from "main" ;
817import { useEffect , useMemo , useState } from "react" ;
918import { useEffectOnce } from "react-use" ;
1019import { AdgProgress , Step } from "smart" ;
1120import { lerp , lerpRadians } from "utils" ;
1221import { useSpeed } from "./play" ;
22+ import { selectionAtom } from "./selection" ;
1323
1424const 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
8292const 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
109119function 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
115125export 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>(
180190export 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
191206export 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 ( ( ) => {
0 commit comments