test(event): polyfill map insertion in fixtures

This commit is contained in:
unanmed 2026-09-09 13:06:27 +08:00
parent 63d4d37ff3
commit 33966fb4dd
4 changed files with 37 additions and 14 deletions

View File

@ -16,6 +16,17 @@ let modules: TestModules;
beforeAll(async () => {
vi.stubGlobal('main', { replayChecking: true });
Map.prototype.getOrInsertComputed ??= function <K, V>(
this: Map<K, V>,
key: K,
callback: (key: K) => V
): V {
const existing = this.get(key);
if (existing !== undefined) return existing;
const value = callback(key);
this.set(key, value);
return value;
};
const mapModule = await import('./mapState');
const commonModule = await import('@user/data-common');
const loggerModule = await import('@motajs/common');

View File

@ -213,20 +213,10 @@ export class MapLayer
event(x: number, y: number): ILayerEventView | null {
if (!this.inMap(x, y)) return null;
const index = y * this.width + x;
const getOrInsertComputed = this.pointEvents.getOrInsertComputed;
if (getOrInsertComputed) {
return getOrInsertComputed.call(
this.pointEvents,
index,
() => new LayerEventView()
);
}
let eventView = this.pointEvents.get(index);
if (!eventView) {
eventView = new LayerEventView();
this.pointEvents.set(index, eventView);
}
return eventView;
return this.pointEvents.getOrInsertComputed(
index,
() => new LayerEventView()
);
}
getPointEvent(x: number, y: number): ReadonlyMap<number, string> | null {

View File

@ -9,6 +9,17 @@ import {
vi.hoisted(() => {
vi.stubGlobal('main', { replayChecking: true });
vi.stubGlobal('location', { origin: 'http://localhost' });
Map.prototype.getOrInsertComputed ??= function <K, V>(
this: Map<K, V>,
key: K,
callback: (key: K) => V
): V {
const existing = this.get(key);
if (existing !== undefined) return existing;
const value = callback(key);
this.set(key, value);
return value;
};
});
interface TestModules {

View File

@ -18,6 +18,17 @@ import { AnonTokyoInterpreter } from 'anon-tokyo';
vi.hoisted(() => {
vi.stubGlobal('main', { replayChecking: true });
vi.stubGlobal('location', { origin: 'http://localhost' });
Map.prototype.getOrInsertComputed ??= function <K, V>(
this: Map<K, V>,
key: K,
callback: (key: K) => V
): V {
const existing = this.get(key);
if (existing !== undefined) return existing;
const value = callback(key);
this.set(key, value);
return value;
};
});
const eventTriggers = {