|
| 1 | +import type { FeatureLike } from 'ol/Feature'; |
| 2 | +import type { Extent } from 'ol/extent'; |
| 3 | +import type Projection from 'ol/proj/Projection'; |
| 4 | +import type VectorTile from 'ol/VectorTile'; |
| 5 | +import TileState from 'ol/TileState'; |
| 6 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 7 | +import { |
| 8 | + abortPendingControllers, |
| 9 | + bucketForZoom, |
| 10 | + createAbortableVectorTileLoader, |
| 11 | + releaseController, |
| 12 | + type PendingByZoom |
| 13 | +} from './tile-loader-abort'; |
| 14 | + |
| 15 | +type TileLoader = ( |
| 16 | + extent: Extent, |
| 17 | + resolution: number, |
| 18 | + projection: Projection |
| 19 | +) => void; |
| 20 | + |
| 21 | +function makeTile(z = 9) { |
| 22 | + let loader: TileLoader | undefined; |
| 23 | + const states: number[] = []; |
| 24 | + const tile = { |
| 25 | + getTileCoord: () => [z, 0, 0], |
| 26 | + setLoader: (nextLoader: TileLoader) => { |
| 27 | + loader = nextLoader; |
| 28 | + }, |
| 29 | + setState: (state: number) => states.push(state) |
| 30 | + } as unknown as VectorTile<FeatureLike>; |
| 31 | + |
| 32 | + return { |
| 33 | + tile, |
| 34 | + states, |
| 35 | + start: () => loader?.([] as unknown as Extent, 1, {} as Projection) |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +afterEach(() => { |
| 40 | + vi.unstubAllGlobals(); |
| 41 | +}); |
| 42 | + |
| 43 | +function makePending(): PendingByZoom { |
| 44 | + return new Map(); |
| 45 | +} |
| 46 | + |
| 47 | +describe('bucketForZoom', () => { |
| 48 | + it('returns the same bucket on repeated calls at the same zoom', () => { |
| 49 | + const pending = makePending(); |
| 50 | + const bucket1 = bucketForZoom(pending, 10); |
| 51 | + const bucket2 = bucketForZoom(pending, 10); |
| 52 | + expect(bucket1).toBe(bucket2); |
| 53 | + }); |
| 54 | + |
| 55 | + it('does not abort any controller when the zoom is unchanged', () => { |
| 56 | + const pending = makePending(); |
| 57 | + const bucket = bucketForZoom(pending, 10); |
| 58 | + const controller = new AbortController(); |
| 59 | + bucket.add(controller); |
| 60 | + |
| 61 | + bucketForZoom(pending, 10); |
| 62 | + |
| 63 | + expect(controller.signal.aborted).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it('aborts all controllers from a superseded zoom', () => { |
| 67 | + const pending = makePending(); |
| 68 | + const bucket = bucketForZoom(pending, 8); |
| 69 | + const c1 = new AbortController(); |
| 70 | + const c2 = new AbortController(); |
| 71 | + bucket.add(c1); |
| 72 | + bucket.add(c2); |
| 73 | + |
| 74 | + bucketForZoom(pending, 12); |
| 75 | + |
| 76 | + expect(c1.signal.aborted).toBe(true); |
| 77 | + expect(c2.signal.aborted).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it('removes the old zoom bucket after superseding', () => { |
| 81 | + const pending = makePending(); |
| 82 | + const oldBucket = bucketForZoom(pending, 8); |
| 83 | + const c = new AbortController(); |
| 84 | + oldBucket.add(c); |
| 85 | + |
| 86 | + bucketForZoom(pending, 12); |
| 87 | + |
| 88 | + expect(pending.has(8)).toBe(false); |
| 89 | + expect(pending.has(12)).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it('aborts controllers across multiple superseded zooms in one call', () => { |
| 93 | + const pending = makePending(); |
| 94 | + const c5 = new AbortController(); |
| 95 | + const c7 = new AbortController(); |
| 96 | + pending.set(5, new Set([c5])); |
| 97 | + pending.set(7, new Set([c7])); |
| 98 | + |
| 99 | + bucketForZoom(pending, 10); |
| 100 | + |
| 101 | + expect(c5.signal.aborted).toBe(true); |
| 102 | + expect(c7.signal.aborted).toBe(true); |
| 103 | + expect(pending.has(5)).toBe(false); |
| 104 | + expect(pending.has(7)).toBe(false); |
| 105 | + expect(pending.has(10)).toBe(true); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('releaseController', () => { |
| 110 | + it('deletes the bucket when its last controller is removed', () => { |
| 111 | + const pending = makePending(); |
| 112 | + const bucket = bucketForZoom(pending, 9); |
| 113 | + const controller = new AbortController(); |
| 114 | + bucket.add(controller); |
| 115 | + |
| 116 | + releaseController(pending, 9, controller); |
| 117 | + |
| 118 | + expect(pending.has(9)).toBe(false); |
| 119 | + }); |
| 120 | + |
| 121 | + it('leaves the bucket intact when other controllers remain', () => { |
| 122 | + const pending = makePending(); |
| 123 | + const bucket = bucketForZoom(pending, 9); |
| 124 | + const c1 = new AbortController(); |
| 125 | + const c2 = new AbortController(); |
| 126 | + bucket.add(c1); |
| 127 | + bucket.add(c2); |
| 128 | + |
| 129 | + releaseController(pending, 9, c1); |
| 130 | + |
| 131 | + expect(pending.has(9)).toBe(true); |
| 132 | + expect(pending.get(9)?.has(c2)).toBe(true); |
| 133 | + expect(pending.get(9)?.has(c1)).toBe(false); |
| 134 | + }); |
| 135 | + |
| 136 | + it('is a no-op when the zoom bucket has already been removed', () => { |
| 137 | + const pending = makePending(); |
| 138 | + const controller = new AbortController(); |
| 139 | + |
| 140 | + expect(() => releaseController(pending, 9, controller)).not.toThrow(); |
| 141 | + expect(pending.has(9)).toBe(false); |
| 142 | + }); |
| 143 | + |
| 144 | + it('is a no-op when the controller is not in the bucket', () => { |
| 145 | + const pending = makePending(); |
| 146 | + const bucket = bucketForZoom(pending, 9); |
| 147 | + const resident = new AbortController(); |
| 148 | + bucket.add(resident); |
| 149 | + const stranger = new AbortController(); |
| 150 | + |
| 151 | + expect(() => releaseController(pending, 9, stranger)).not.toThrow(); |
| 152 | + expect(pending.has(9)).toBe(true); |
| 153 | + expect(pending.get(9)?.has(resident)).toBe(true); |
| 154 | + }); |
| 155 | +}); |
| 156 | + |
| 157 | +describe('abortPendingControllers', () => { |
| 158 | + it('aborts every pending controller and clears the bookkeeping', () => { |
| 159 | + const c8 = new AbortController(); |
| 160 | + const c9 = new AbortController(); |
| 161 | + const pending: PendingByZoom = new Map([ |
| 162 | + [8, new Set([c8])], |
| 163 | + [9, new Set([c9])] |
| 164 | + ]); |
| 165 | + |
| 166 | + abortPendingControllers(pending); |
| 167 | + |
| 168 | + expect(c8.signal.aborted).toBe(true); |
| 169 | + expect(c9.signal.aborted).toBe(true); |
| 170 | + expect(pending.size).toBe(0); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +describe('createAbortableVectorTileLoader', () => { |
| 175 | + it('marks an aborted tile as an error during teardown', async () => { |
| 176 | + vi.stubGlobal( |
| 177 | + 'fetch', |
| 178 | + vi.fn((_src: string, init?: RequestInit) => { |
| 179 | + return new Promise<Response>((_resolve, reject) => { |
| 180 | + init?.signal?.addEventListener('abort', () => { |
| 181 | + reject(new DOMException('Aborted', 'AbortError')); |
| 182 | + }); |
| 183 | + }); |
| 184 | + }) |
| 185 | + ); |
| 186 | + const { tileLoadFunction, abortPending } = |
| 187 | + createAbortableVectorTileLoader(); |
| 188 | + const tile = makeTile(); |
| 189 | + |
| 190 | + tileLoadFunction(tile.tile, 'https://example.test/tile.pbf'); |
| 191 | + tile.start(); |
| 192 | + expect(tile.states).toEqual([TileState.LOADING]); |
| 193 | + |
| 194 | + abortPending(); |
| 195 | + |
| 196 | + await vi.waitFor(() => { |
| 197 | + expect(tile.states).toEqual([TileState.LOADING, TileState.ERROR]); |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + it('marks ordinary fetch failures as errors', async () => { |
| 202 | + vi.stubGlobal( |
| 203 | + 'fetch', |
| 204 | + vi.fn(() => Promise.reject(new Error('offline'))) |
| 205 | + ); |
| 206 | + const { tileLoadFunction } = createAbortableVectorTileLoader(); |
| 207 | + const tile = makeTile(); |
| 208 | + |
| 209 | + tileLoadFunction(tile.tile, 'https://example.test/tile.pbf'); |
| 210 | + tile.start(); |
| 211 | + |
| 212 | + await vi.waitFor(() => { |
| 213 | + expect(tile.states).toEqual([TileState.LOADING, TileState.ERROR]); |
| 214 | + }); |
| 215 | + }); |
| 216 | +}); |
0 commit comments