Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions src/source/source_cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
const sourceCache = createSourceCache();
const spy = vi.fn();
sourceCache._source.loadTile = spy;

sourceCache.onAdd(undefined);
sourceCache._addTile(tileID);
expect(spy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -594,7 +594,7 @@
hasTile: (coord) => (coord.canonical.x !== 0)
});
const dataPromise = waitForEvent(sourceCache, 'data', e => e.sourceDataType === 'metadata');

sourceCache.onAdd(undefined);
await dataPromise;
sourceCache.update(transform);
Expand Down Expand Up @@ -1805,16 +1805,16 @@
transform.resize(512, 512);
transform.setZoom(1.05);
transform.setCenter(new LngLat(-179.9, 0.1));

const sourceCache = createSourceCache();
sourceCache._source.loadTile = async (tile) => {
tile.state = 'loaded';
};

const dataPromise = waitForEvent(sourceCache, 'data', e => e.sourceDataType === 'metadata');
sourceCache.onAdd(undefined);
await dataPromise;

sourceCache.update(transform);

expect(sourceCache.tilesIn([
Expand Down Expand Up @@ -2150,7 +2150,7 @@
sourceCache.getTiles()[tile.tileID.key] = tile;

expect(sourceCache.findLoadedSibling(new OverscaledTileID(1, 0, 1, 1, 0))).toBeNull();
expect(sourceCache.findLoadedSibling(new OverscaledTileID(1, 0, 1, 0, 0))).toEqual(tile);

Check failure on line 2153 in src/source/source_cache.test.ts

View workflow job for this annotation

GitHub Actions / Unit tests and Coverage

src/source/source_cache.test.ts > SourceCache#findLoadedSibling > adds from previously used tiles (sourceCache._tiles)

AssertionError: expected null to deeply equal { …(2) } - Expected: { "hasData": [Function hasData], "tileID": OverscaledTileID { "canonical": CanonicalTileID { "key": "011", "x": 0, "y": 0, "z": 1, }, "key": "011", "overscaledZ": 1, "terrainRttPosMatrix32f": null, "wrap": 0, }, } + Received: null ❯ src/source/source_cache.test.ts:2153:84
});

test('retains siblings', () => {
Expand All @@ -2164,7 +2164,7 @@
sourceCache.getCache().add(tile.tileID, tile);

expect(sourceCache.findLoadedSibling(new OverscaledTileID(1, 0, 1, 1, 0))).toBeNull();
expect(sourceCache.findLoadedSibling(new OverscaledTileID(1, 0, 1, 0, 0))).toBe(tile);

Check failure on line 2167 in src/source/source_cache.test.ts

View workflow job for this annotation

GitHub Actions / Unit tests and Coverage

src/source/source_cache.test.ts > SourceCache#findLoadedSibling > retains siblings

AssertionError: expected null to be Tile{ timeAdded: +0, …(15) } // Object.is equality - Expected: Tile { "buckets": {}, "dependencies": {}, "expirationTime": null, "expiredRequestCount": 0, "fadeEndTime": 0, "hasRTLText": false, "hasSymbolBuckets": false, "queryPadding": 0, "rtt": [], "rttCoords": {}, "state": "loading", "tileID": OverscaledTileID { "canonical": CanonicalTileID { "key": "011", "x": 0, "y": 0, "z": 1, }, "key": "011", "overscaledZ": 1, "terrainRttPosMatrix32f": null, "wrap": 0, }, "tileSize": 512, "timeAdded": 0, "uid": 194, "uses": 0, } + Received: null ❯ src/source/source_cache.test.ts:2167:84
expect(sourceCache.getCache().order).toHaveLength(1);
});

Expand Down Expand Up @@ -2196,7 +2196,7 @@
sourceCache.updateLoadedSiblingTileCache();

// Loaded tiles should be in the cache
expect(sourceCache.findLoadedSibling(tiles[0]).tileID).toBe(tiles[0]);

Check failure on line 2199 in src/source/source_cache.test.ts

View workflow job for this annotation

GitHub Actions / Unit tests and Coverage

src/source/source_cache.test.ts > SourceCache#findLoadedSibling > Search cache for loaded sibling tiles

TypeError: Cannot read properties of null (reading 'tileID') ❯ src/source/source_cache.test.ts:2199:54
expect(sourceCache.findLoadedSibling(tiles[1]).tileID).toBe(tiles[1]);
expect(sourceCache.findLoadedSibling(tiles[2]).tileID).toBe(tiles[2]);
expect(sourceCache.findLoadedSibling(tiles[3]).tileID).toBe(tiles[3]);
Expand All @@ -2216,6 +2216,39 @@
expect(sourceCache.findLoadedSibling(notLoadedTiles[2])).toBeNull();
expect(sourceCache.findLoadedSibling(notLoadedTiles[3])).toBeNull();
});

test('returns a loaded sibling tile with same z/x/y but different wrap', () => {
const sourceCache = createSourceCache({});
sourceCache.onAdd(undefined);
const tr = new MercatorTransform();
tr.resize(512, 512);
sourceCache.updateCacheSize(tr);

// Add a tile at z=2, x=1, y=0, wrap=0
const tile0 = {
tileID: new OverscaledTileID(2, 0, 2, 1, 0),
hasData() { return true; }
} as any as Tile;

// Add a sibling tile at z=2, x=1, y=0, wrap=1
const tile1 = {
tileID: new OverscaledTileID(2, 1, 2, 1, 0),
hasData() { return true; }
} as any as Tile;

sourceCache.getTiles()[tile0.tileID.key] = tile0;
sourceCache.getTiles()[tile1.tileID.key] = tile1;

// Should find tile1 as a sibling of tile0
expect(sourceCache.findLoadedSibling(tile0.tileID)).toBe(tile1);

// Should find tile0 as a sibling of tile1
expect(sourceCache.findLoadedSibling(tile1.tileID)).toBe(tile0);

// Should return null when no sibling exists (z=2, x=2, y=0, wrap=0)
const tile2 = new OverscaledTileID(2, 0, 2, 2, 0);
expect(sourceCache.findLoadedSibling(tile2)).toBeNull();
});
});

describe('SourceCache#reload', () => {
Expand Down Expand Up @@ -2372,7 +2405,7 @@
});

});

describe('SourceCache::refreshTiles', () => {
test('calls reloadTile when tile exists', async () => {
const coord = new OverscaledTileID(1, 0, 1, 0, 1);
Expand All @@ -2389,7 +2422,7 @@
expect(spy).toHaveBeenCalledOnce();
expect(spy.mock.calls[0][1]).toBe('expired');
});

test('does not call reloadTile when tile does not exist', async () => {
const coord = new OverscaledTileID(1, 0, 1, 1, 1);
const sourceCache = createSourceCache();
Expand Down
23 changes: 19 additions & 4 deletions src/source/source_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,24 @@ export class SourceCache extends Evented {
/**
* Find a loaded sibling of the given tile
*/
findLoadedSibling(tileID: OverscaledTileID): Tile {
// If a tile with this ID already exists, return it
return this._getLoadedTile(tileID);
findLoadedSibling(tileID: OverscaledTileID): Tile | null {
// Sibling tiles: same z/x/y, different wrap
for (const key in this._tiles) {
const candidate = this._tiles[key];
if (
candidate.tileID.canonical.z === tileID.canonical.z &&
candidate.tileID.canonical.x === tileID.canonical.x &&
candidate.tileID.canonical.y === tileID.canonical.y &&
candidate.tileID.wrap !== tileID.wrap
) {
// Only return if the candidate is loaded
const loadedCandidate = this._getLoadedTile(candidate.tileID);
if (loadedCandidate) {
return loadedCandidate;
}
}
}
return null;
}

_getLoadedTile(tileID: OverscaledTileID): Tile {
Expand Down Expand Up @@ -1042,7 +1057,7 @@ export class SourceCache extends Evented {
bounds.shrinkBy(Math.min(bounds.width(), bounds.height()) * 0.001);
const projected = bounds.map(project);

const newBounds = Bounds.fromPoints(transformed);
const newBounds = Bounds.fromPoints(transformed);

if (!newBounds.covers(projected)) {
transformed = transformed.map((coord) => coord.x > 0.5 ?
Expand Down
Loading