@@ -19,9 +19,21 @@ export type TileSet = {|
1919 atlasImage : string ,
2020| } ;
2121
22+ type TileMapCoordinate = { | x : number , y : number | } ;
23+
24+ type FreehandTileMapCacheEntry = { |
25+ processedCount : number ,
26+ tileMap : Map < string , TileMapTilePatch> ,
27+ | } ;
28+
29+ const freehandTileMapCache : WeakMap <
30+ Array < TileMapCoordinate > ,
31+ FreehandTileMapCacheEntry
32+ > = new WeakMap ( ) ;
33+
2234const areSameCoordinates = (
23- tileA : { | x : number , y : number | } ,
24- tileB : { | x : number , y : number | }
35+ tileA : TileMapCoordinate ,
36+ tileB : TileMapCoordinate
2537) : boolean => tileA . x === tileB . x && tileA . y === tileB . y ;
2638
2739/**
@@ -232,7 +244,7 @@ export const getTilesGridCoordinatesFromPointerSceneCoordinates = ({
232244 sceneToTileMapTransformation,
233245} : { |
234246 tileMapTileSelection : TileMapTileSelection ,
235- coordinates : Array < { | x : number , y : number | } > ,
247+ coordinates : Array < TileMapCoordinate > ,
236248 tileSize : number ,
237249 sceneToTileMapTransformation : AffineTransformation ,
238250| } ) : TileMapTilePatch [ ] => {
@@ -313,23 +325,19 @@ export const getTilesGridCoordinatesFromPointerSceneCoordinates = ({
313325 const selectionHeight =
314326 selectionBottomRightCorner . y - selectionTopLeftCorner . y + 1 ;
315327
316- // Process only coordinates that haven't been processed yet.
317- // We use a cache attached to the coordinates array to track progress.
318- let processedCount = coordinates . _processedCount || 0 ;
328+ const cachedEntry = freehandTileMapCache . get ( coordinates ) ;
329+ let processedCount = 0 ;
330+ let tileMap = new Map < string , TileMapTilePatch > ( ) ;
319331
320- // If the array length decreased, it means the array was reset or reused.
321- // Clear the cache and start fresh.
322- if ( processedCount > coordinates . length ) {
323- processedCount = 0 ;
324- coordinates . _processedCount = 0 ;
325- coordinates . _cachedTileMap = null ;
332+ if ( cachedEntry ) {
333+ // If the array length decreased, it means the array was reset or reused.
334+ // Clear the cache and start fresh.
335+ if ( cachedEntry . processedCount <= coordinates . length ) {
336+ processedCount = cachedEntry . processedCount ;
337+ tileMap = new Map ( cachedEntry . tileMap ) ;
338+ }
326339 }
327340
328- // Start with cached result if it exists, otherwise create new map
329- const tileMap = coordinates . _cachedTileMap
330- ? new Map ( coordinates . _cachedTileMap )
331- : new Map < string , TileMapTilePatch > ( ) ;
332-
333341 // Process only new coordinates since last call
334342 for ( let i = processedCount ; i < coordinates . length ; i ++ ) {
335343 const coord = coordinates [ i ] ;
@@ -366,8 +374,10 @@ export const getTilesGridCoordinatesFromPointerSceneCoordinates = ({
366374 }
367375
368376 // Update cache for next incremental call
369- coordinates . _processedCount = coordinates . length ;
370- coordinates . _cachedTileMap = tileMap ;
377+ freehandTileMapCache . set ( coordinates , {
378+ processedCount : coordinates . length ,
379+ tileMap,
380+ } ) ;
371381
372382 return [ ...tileMap . values ( ) ] ;
373383 }
0 commit comments