From f90cd754b5b6362e150bd13f8c041ad644530c7e Mon Sep 17 00:00:00 2001 From: rhenck <richard.henck@iqb.hu-berlin.de> Date: Mon, 5 Jun 2023 18:43:35 +0200 Subject: [PATCH] [player] Refactor outbound Verona-Service Most importantly remove the standalone switch and let the service always send postMessage, even when there is no surrounding application listening. This helps testability and has no downsides. Also: - Make methods static where applicable - Remove useless variables and methods - Improve variable initialisation (to allow undefined) --- .../services/verona-post.service.spec.ts | 7 ++-- .../verona/services/verona-post.service.ts | 42 +++++++------------ projects/player/src/app/app.component.ts | 9 +--- .../src/app/components/unit/unit.component.ts | 2 +- 4 files changed, 20 insertions(+), 40 deletions(-) diff --git a/projects/player/modules/verona/services/verona-post.service.spec.ts b/projects/player/modules/verona/services/verona-post.service.spec.ts index 98677f064..a3948410b 100644 --- a/projects/player/modules/verona/services/verona-post.service.spec.ts +++ b/projects/player/modules/verona/services/verona-post.service.spec.ts @@ -11,8 +11,7 @@ describe('VeronaPostService', () => { beforeEach(() => { TestBed.configureTestingModule({}); service = TestBed.inject(VeronaPostService); - service.isStandalone = false; - service.sessionId = 'test'; + service.sessionID = 'test'; service.stateReportPolicy = 'eager'; }); @@ -87,7 +86,7 @@ describe('VeronaPostService', () => { eventSubscription.unsubscribe(); done(); }); - service.sendVopReadyNotification(metadata); + VeronaPostService.sendReadyNotification(metadata); }); it('should post a VopUnitNavigationRequestedNotification', done => { @@ -121,6 +120,6 @@ describe('VeronaPostService', () => { eventSubscription.unsubscribe(); done(); }); - service.sendVopWindowFocusChangedNotification(true); + VeronaPostService.sendVopWindowFocusChangedNotification(true); }); }); diff --git a/projects/player/modules/verona/services/verona-post.service.ts b/projects/player/modules/verona/services/verona-post.service.ts index 2a39aa27a..766e4a664 100644 --- a/projects/player/modules/verona/services/verona-post.service.ts +++ b/projects/player/modules/verona/services/verona-post.service.ts @@ -15,8 +15,7 @@ import { providedIn: 'root' }) export class VeronaPostService { - private _isStandalone: boolean = true; - private _sessionId: string = 'unKnown'; + sessionID: string | undefined; private _stateReportPolicy: StateReportPolicy = 'eager'; private cachedVopStateChangedNotificationValues: { unitState?: UnitState, @@ -24,25 +23,12 @@ export class VeronaPostService { log?: LogData[] } = {}; - set sessionId(sessionId: string) { - this._sessionId = sessionId; - } - - set isStandalone(isStandalone: boolean) { - this._isStandalone = isStandalone; - } - set stateReportPolicy(stateReportPolicy: StateReportPolicy) { this._stateReportPolicy = stateReportPolicy; } - private send(message: VopMessage): void { - // prevent posts in local (dev) mode - if (!this._isStandalone) { - window.parent.postMessage(message, '*'); - } else { - LogService.debug('player: no host detected'); - } + private static sendMessage(message: VopMessage): void { + window.parent.postMessage(message, '*'); } sendVopStateChangedNotification(values: { @@ -51,7 +37,7 @@ export class VeronaPostService { log?: LogData[] }, requested: boolean = false): void { if (this._stateReportPolicy === 'eager' || requested) { - this.send(this.createVopStateChangedNotification( + VeronaPostService.sendMessage(this.createVopStateChangedNotification( { ...this.cachedVopStateChangedNotificationValues, ...values } )); } else { @@ -66,16 +52,16 @@ export class VeronaPostService { }): VopStateChangedNotification { return { type: 'vopStateChangedNotification', - sessionId: this._sessionId, + sessionId: this.sessionID as string, timeStamp: Date.now(), ...(values) }; } - sendVopReadyNotification(playerMetadata: VopMetaData): void { + static sendReadyNotification(playerMetadata: VopMetaData): void { if (playerMetadata) { LogService.debug('player: sendVopReadyNotification', playerMetadata); - this.send({ + VeronaPostService.sendMessage({ type: 'vopReadyNotification', metadata: playerMetadata }); @@ -84,19 +70,19 @@ export class VeronaPostService { } } - sendVopUnitNavigationRequestedNotification = (target: NavigationTarget): void => { - this.send({ + sendVopUnitNavigationRequestedNotification(target: NavigationTarget): void { + VeronaPostService.sendMessage({ type: 'vopUnitNavigationRequestedNotification', - sessionId: this._sessionId, + sessionId: this.sessionID as string, target: target }); - }; + } - sendVopWindowFocusChangedNotification = (focused: boolean): void => { - this.send({ + static sendVopWindowFocusChangedNotification(focused: boolean): void { + VeronaPostService.sendMessage({ type: 'vopWindowFocusChangedNotification', timeStamp: Date.now(), hasFocus: focused }); - }; + } } diff --git a/projects/player/src/app/app.component.ts b/projects/player/src/app/app.component.ts index 0f17f5192..2d90e758b 100644 --- a/projects/player/src/app/app.component.ts +++ b/projects/player/src/app/app.component.ts @@ -23,14 +23,9 @@ export class AppComponent implements OnInit { ngOnInit(): void { this.setLocales(); - this.initVeronaPostService(); + VeronaPostService.sendReadyNotification(this.metaDataService.playerMetadata); this.nativeEventService.focus - .subscribe(isFocused => this.veronaPostService.sendVopWindowFocusChangedNotification(isFocused)); - } - - private initVeronaPostService(): void { - this.veronaPostService.isStandalone = this.isStandalone; - this.veronaPostService.sendVopReadyNotification(this.metaDataService.playerMetadata); + .subscribe(isFocused => VeronaPostService.sendVopWindowFocusChangedNotification(isFocused)); } private setLocales(): void { diff --git a/projects/player/src/app/components/unit/unit.component.ts b/projects/player/src/app/components/unit/unit.component.ts index 10eb04c5d..92aeaf645 100644 --- a/projects/player/src/app/components/unit/unit.component.ts +++ b/projects/player/src/app/components/unit/unit.component.ts @@ -50,7 +50,7 @@ export class UnitComponent implements OnInit { this.playerConfig = message.playerConfig || {}; LogService.info('player: unitStateElementCodes', this.unitStateService.elementCodes); this.metaDataService.resourceURL = this.playerConfig.directDownloadUrl; - this.veronaPostService.sessionId = message.sessionId; + this.veronaPostService.sessionID = message.sessionId; this.veronaPostService.stateReportPolicy = message.playerConfig?.stateReportPolicy || 'none'; this.unitStateService.elementCodes = message.unitState?.dataParts?.elementCodes ? JSON.parse(message.unitState.dataParts.elementCodes) : []; -- GitLab