Skip to content

Commit ab0dbb2

Browse files
authored
Fix flow and fix formatting (#8277)
Fix `npm run flow` and formatting in `newIDE/app` by refactoring a Flow-unsafe cache and applying Prettier corrections. The `TileMap.js` change addresses a Flow typing error where `_processedCount` and `_cachedTileMap` properties were being attached directly to a `coordinates` array, which Flow rejects. This was refactored to use a typed `WeakMap` for safe caching.
1 parent 6522cac commit ab0dbb2

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

newIDE/app/src/InstancesEditor/ClickInterceptor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class ClickInterceptor {
174174
const MIN_DISTANCE = 2; // pixels
175175
const dx = sceneCoordinates[0] - lastPoint.x;
176176
const dy = sceneCoordinates[1] - lastPoint.y;
177-
if (Math.abs(dx) < MIN_DISTANCE && Math.abs(dy) < MIN_DISTANCE) {
177+
if (Math.abs(dx) < MIN_DISTANCE && Math.abs(dy) < MIN_DISTANCE) {
178178
return;
179179
}
180180
}

newIDE/app/src/Utils/TileMap.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2234
const 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

Comments
 (0)