mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-05 10:53:59 +00:00
Merge branch 'main' into i18n-spanish
This commit is contained in:
53 files changed
+968
-151
No files matched your search
@@ -131,7 +131,8 @@ export class MapSourceController {
|
||||
* @returns {() => void} Unsubscribe.
|
||||
*/
|
||||
subscribe(listener) {
|
||||
if (typeof listener !== 'function') return () => {};
|
||||
// A destroyed controller never settles again, so it takes no listeners.
|
||||
if (typeof listener !== 'function' || this._destroyed) return () => {};
|
||||
this._subscribers.add(listener);
|
||||
return () => {
|
||||
this._subscribers.delete(listener);
|
||||
@@ -142,6 +143,8 @@ export class MapSourceController {
|
||||
if (this._destroyed || !this._subscribers.size) return;
|
||||
const state = this.getState();
|
||||
for (const listener of [...this._subscribers]) {
|
||||
if (this._destroyed) return;
|
||||
if (!this._subscribers.has(listener)) continue;
|
||||
try {
|
||||
listener(state);
|
||||
} catch (error) {
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { MapSourceController } from './controller.js';
|
||||
function fixture() {
|
||||
return new MapSourceController(
|
||||
{
|
||||
scene: {
|
||||
globe: { show: true },
|
||||
requestRender() {},
|
||||
frameState: {
|
||||
creditDisplay: { addStaticCredit() {}, removeStaticCredit() {} },
|
||||
},
|
||||
primitives: { add() {}, remove() {} },
|
||||
},
|
||||
imageryLayers: { add() {}, remove() {} },
|
||||
},
|
||||
{
|
||||
registry: {
|
||||
defaultId: 'fixture',
|
||||
sources: [
|
||||
{
|
||||
descriptor: { id: 'fixture', label: 'Fixture', kind: 'imagery' },
|
||||
imagery: async () => ({}),
|
||||
},
|
||||
],
|
||||
},
|
||||
createImageryLayer: (provider) => ({ provider }),
|
||||
},
|
||||
);
|
||||
}
|
||||
test('an unsubscribed owner is skipped in the same map settlement delivery', async () => {
|
||||
const controller = fixture();
|
||||
const calls = [];
|
||||
let detach;
|
||||
controller.subscribe(() => {
|
||||
calls.push('first');
|
||||
detach();
|
||||
});
|
||||
detach = controller.subscribe(() => calls.push('detached'));
|
||||
controller.subscribe(() => calls.push('last'));
|
||||
await controller.setStack('fixture');
|
||||
assert.deepEqual(calls, ['first', 'last']);
|
||||
controller.destroy();
|
||||
});
|
||||
test('destroying the controller during delivery stops remaining listeners', async () => {
|
||||
const controller = fixture();
|
||||
const calls = [];
|
||||
controller.subscribe(() => {
|
||||
calls.push('destroy');
|
||||
controller.destroy();
|
||||
});
|
||||
controller.subscribe(() => calls.push('detached'));
|
||||
await controller.setStack('fixture');
|
||||
assert.deepEqual(calls, ['destroy']);
|
||||
});
|
||||
|
||||
test('a listener re-added after destroy during delivery is not called', async () => {
|
||||
const controller = fixture();
|
||||
const calls = [];
|
||||
const later = () => calls.push('later');
|
||||
controller.subscribe(() => {
|
||||
calls.push('destroy');
|
||||
controller.destroy();
|
||||
controller.subscribe(later);
|
||||
});
|
||||
controller.subscribe(later);
|
||||
await controller.setStack('fixture');
|
||||
assert.deepEqual(calls, ['destroy']);
|
||||
});
|
||||
|
||||
test('delivery order: added during delivery waits, duplicates run once, re-added runs', async () => {
|
||||
const controller = fixture();
|
||||
const calls = [];
|
||||
const twice = () => calls.push('twice');
|
||||
const readded = () => calls.push('readded');
|
||||
let offReadded;
|
||||
controller.subscribe(() => {
|
||||
calls.push('first');
|
||||
controller.subscribe(() => calls.push('added-late'));
|
||||
offReadded();
|
||||
controller.subscribe(readded);
|
||||
});
|
||||
controller.subscribe(twice);
|
||||
controller.subscribe(twice);
|
||||
offReadded = controller.subscribe(readded);
|
||||
await controller.setStack('fixture');
|
||||
assert.deepEqual(calls, ['first', 'twice', 'readded']);
|
||||
controller.destroy();
|
||||
});
|
||||
Reference in new issue
Block a user