Skip to content

Commit

Permalink
initialize SSE after everything else loads
Browse files Browse the repository at this point in the history
  • Loading branch information
granny committed Dec 22, 2023
1 parent 1affd7a commit b8801a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
17 changes: 8 additions & 9 deletions webmap/src/Pl3xMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Pl3xMap {
private readonly _playerManager: PlayerManager;
private readonly _worldManager: WorldManager;

private _eventSource: EventSource;
private _eventSource?: EventSource;

private _langPalette: Map<string, string> = new Map();
private _settings?: Settings;
Expand All @@ -35,11 +35,9 @@ export class Pl3xMap {
Pl3xMap._instance = this;

this._map = new Pl3xMapLeafletMap(this);

this._eventSource = this.initSSE();


window.addEventListener('beforeunload', function () {
if (Pl3xMap.instance.eventSource != null) {
if (Pl3xMap.instance.eventSource != undefined) {
Pl3xMap.instance.eventSource.close();
}
});
Expand All @@ -61,6 +59,7 @@ export class Pl3xMap {
});
this.controlManager.sidebarControl = new SidebarControl(this);
const promise: Promise<void> = this.worldManager.init(this._settings);
this._eventSource = this.initSSE();
this.update();
return promise;
});
Expand All @@ -71,14 +70,14 @@ export class Pl3xMap {
}

private update(): void {
getJSON('tiles/settings.json').then((json): void => {
getJSON('tiles/settings.json').then( (json): void => {
this._settings = json as Settings;

if (!this._settings.useSSE) {
Pl3xMap.instance.eventSource.close();
this.eventSource?.close();
}

if (Pl3xMap.instance.eventSource.readyState === EventSource.CLOSED) {
if (this.eventSource === undefined || this.eventSource.readyState === EventSource.CLOSED) {
this.playerManager.update(this._settings.players);
}

Expand Down Expand Up @@ -131,7 +130,7 @@ export class Pl3xMap {
return this._worldManager;
}

get eventSource(): EventSource {
get eventSource(): EventSource | undefined {
return this._eventSource;
}

Expand Down
2 changes: 1 addition & 1 deletion webmap/src/layergroup/MarkerLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class MarkerLayer extends L.LayerGroup {
}

update(world: World): void {
if (Pl3xMap.instance.eventSource.readyState !== EventSource.CLOSED) {
if (Pl3xMap.instance.eventSource !== undefined && Pl3xMap.instance.eventSource.readyState !== EventSource.CLOSED) {
return;
}

Expand Down

0 comments on commit b8801a5

Please sign in to comment.