From c8589f6eb5381c213a4d03322c87d4292a73c410 Mon Sep 17 00:00:00 2001 From: markuczy Date: Mon, 3 Jun 2024 13:28:29 +0200 Subject: [PATCH] refactor: tests and topic adjusted --- libs/accelerator/src/lib/topic/message.ts | 2 +- libs/accelerator/src/lib/topic/topic.spec.ts | 100 +++++++++---------- libs/accelerator/src/lib/topic/topic.ts | 45 ++++----- 3 files changed, 71 insertions(+), 76 deletions(-) diff --git a/libs/accelerator/src/lib/topic/message.ts b/libs/accelerator/src/lib/topic/message.ts index bf67a78b..63e488d1 100644 --- a/libs/accelerator/src/lib/topic/message.ts +++ b/libs/accelerator/src/lib/topic/message.ts @@ -8,7 +8,7 @@ window['onecxMessageId'] = 0 export class Message { timestamp: number - id: number + id: number // id can be undefined while used via old implementation constructor(public type: string) { this.timestamp = window.performance.now() diff --git a/libs/accelerator/src/lib/topic/topic.spec.ts b/libs/accelerator/src/lib/topic/topic.spec.ts index 834d4363..b6c006ea 100644 --- a/libs/accelerator/src/lib/topic/topic.spec.ts +++ b/libs/accelerator/src/lib/topic/topic.spec.ts @@ -158,12 +158,12 @@ describe('Topic', () => { }) }) - describe('on message', () => { - let currentMessage: TopicDataMessage + describe('integration with older versions of library', () => { + let previousMessage: TopicDataMessage let incomingMessage: MessageEvent> beforeEach(() => { - currentMessage = { + previousMessage = { type: TopicMessageType.TopicNext, name: testTopic1.name, version: testTopic1.version, @@ -187,99 +187,99 @@ describe('Topic', () => { }) it('should have value if incoming id is greater than previous id', () => { - currentMessage.data = 'msg1' - currentMessage.id = 0 + previousMessage.data = 'msg1' + previousMessage.id = 0 incomingMessage.data.data = 'msg2' incomingMessage.data.id = 1 + ;(testTopic1).data.next(previousMessage) + ;(testTopic1).onMessage(incomingMessage) - testTopic1.onMessage(currentMessage, incomingMessage) - - expect(values1).toEqual(['initMsg', 'msg2']) + expect(values1).toEqual(['initMsg', 'msg1', 'msg2']) }) it('should have value if incoming timestamp is greater than previous timestamp with no ids provided', () => { - currentMessage.data = 'msg1' - currentMessage.id = undefined! - currentMessage.timestamp = 1 + previousMessage.data = 'msg1' + ;(previousMessage).id = undefined + previousMessage.timestamp = 1 incomingMessage.data.data = 'msg2' - incomingMessage.data.id = undefined! + ;(incomingMessage.data).id = undefined incomingMessage.data.timestamp = 3 + ;(testTopic1).data.next(previousMessage) + ;(testTopic1).onMessage(incomingMessage) - testTopic1.onMessage(currentMessage, incomingMessage) - - expect(values1).toEqual(['initMsg', 'msg2']) + expect(values1).toEqual(['initMsg', 'msg1', 'msg2']) }) it('should have value if incoming timestamp is greater than previous timestamp when current message has id', () => { - currentMessage.data = 'msg1' - currentMessage.id = 1 - currentMessage.timestamp = 1 + previousMessage.data = 'msg1' + previousMessage.id = 1 + previousMessage.timestamp = 1 incomingMessage.data.data = 'msg2' - incomingMessage.data.id = undefined! + ;(incomingMessage.data).id = undefined incomingMessage.data.timestamp = 3 + ;(testTopic1).data.next(previousMessage) + ;(testTopic1).onMessage(incomingMessage) - testTopic1.onMessage(currentMessage, incomingMessage) - - expect(values1).toEqual(['initMsg', 'msg2']) + expect(values1).toEqual(['initMsg', 'msg1', 'msg2']) }) it('should have value if incoming timestamp is greater than previous timestamp when incoming message has id', () => { - currentMessage.data = 'msg1' - currentMessage.id = undefined! - currentMessage.timestamp = 1 + previousMessage.data = 'msg1' + ;(previousMessage).id = undefined + previousMessage.timestamp = 1 incomingMessage.data.data = 'msg2' incomingMessage.data.id = 1 incomingMessage.data.timestamp = 3 + ;(testTopic1).data.next(previousMessage) + ;(testTopic1).onMessage(incomingMessage) - testTopic1.onMessage(currentMessage, incomingMessage) - - expect(values1).toEqual(['initMsg', 'msg2']) + expect(values1).toEqual(['initMsg', 'msg1', 'msg2']) }) it('should have no value if incoming timestamp is equal to the previous timestamp with no ids provided', () => { - currentMessage.data = 'msg1' - currentMessage.id = undefined! - currentMessage.timestamp = 3 + previousMessage.data = 'msg1' + ;(previousMessage).id = undefined + previousMessage.timestamp = 3 incomingMessage.data.data = 'msg2' - incomingMessage.data.id = undefined! + ;(incomingMessage.data).id = undefined incomingMessage.data.timestamp = 3 + ;(testTopic1).data.next(previousMessage) + ;(testTopic1).onMessage(incomingMessage) - testTopic1.onMessage(currentMessage, incomingMessage) - - expect(values1).toEqual(['initMsg']) + expect(values1).toEqual(['initMsg', 'msg1']) }) it('should have no value if incoming timestamp is equal to the previous timestamp when current message has id', () => { jest.spyOn(console, 'warn') - currentMessage.data = 'msg1' - currentMessage.id = 1 - currentMessage.timestamp = 3 + previousMessage.data = 'msg1' + previousMessage.id = 1 + previousMessage.timestamp = 3 incomingMessage.data.data = 'msg2' - incomingMessage.data.id = undefined! + ;(incomingMessage.data).id = undefined incomingMessage.data.timestamp = 3 + ;(testTopic1).data.next(previousMessage) + ;(testTopic1).onMessage(incomingMessage) - testTopic1.onMessage(currentMessage, incomingMessage) - - expect(values1).toEqual(['initMsg']) + expect(values1).toEqual(['initMsg', 'msg1']) expect(console.warn).toHaveBeenLastCalledWith( - 'Message was swallowed because of equal timestamps. Please upgrate to the latest version to ensure messages are correctly timed' + 'Message was dropped because of equal timestamps, because there was an old style message in the system. Please upgrade all libraries to the latest version.' ) }) it('should have no value if incoming timestamp is equal to previous timestamp when incoming message has id', () => { jest.spyOn(console, 'warn') - currentMessage.data = 'msg1' - currentMessage.id = undefined! - currentMessage.timestamp = 3 + previousMessage.data = 'msg1' + ;(previousMessage).id = undefined + previousMessage.timestamp = 3 incomingMessage.data.data = 'msg2' incomingMessage.data.id = 1 incomingMessage.data.timestamp = 3 + ;(testTopic1).data.next(previousMessage) + ;(testTopic1).onMessage(incomingMessage) - testTopic1.onMessage(currentMessage, incomingMessage) - - expect(values1).toEqual(['initMsg']) + expect(values1).toEqual(['initMsg', 'msg1']) expect(console.warn).toHaveBeenLastCalledWith( - 'Message was swallowed because of equal timestamps. Please upgrate to the latest version to ensure messages are correctly timed' + 'Message was dropped because of equal timestamps, because there was an old style message in the system. Please upgrade all libraries to the latest version.' ) }) }) diff --git a/libs/accelerator/src/lib/topic/topic.ts b/libs/accelerator/src/lib/topic/topic.ts index a03c7461..e5aa5822 100644 --- a/libs/accelerator/src/lib/topic/topic.ts +++ b/libs/accelerator/src/lib/topic/topic.ts @@ -19,7 +19,7 @@ export class Topic extends TopicPublisher implements Subscribable { protected isInit = false private resolveInitPromise!: (value: void | PromiseLike) => void - private eventListener = (m: MessageEvent) => this.onMessage(this.data.value, m) + private eventListener = (m: MessageEvent) => this.onMessage(m) constructor(name: string, version: number, sendGetMessage = true) { super(name, version) @@ -142,50 +142,45 @@ export class Topic extends TopicPublisher implements Subscribable { window.removeEventListener('message', this.eventListener, true) } - public onMessage(currentMessage: TopicDataMessage | undefined, incomingMessage: MessageEvent): any { - switch (incomingMessage.data.type) { + private onMessage(m: MessageEvent): any { + switch (m.data.type) { case TopicMessageType.TopicNext: - if (incomingMessage.data.name !== this.name || incomingMessage.data.version !== this.version) { + if (m.data.name !== this.name || m.data.version !== this.version) { break } if ( - !currentMessage || + !this.data.value || (this.isInit && - (incomingMessage.data).id !== undefined && - currentMessage.id !== undefined && - (incomingMessage.data).id > currentMessage.id) || - (this.isInit && (incomingMessage.data).timestamp > currentMessage.timestamp) + (m.data).id !== undefined && + this.data.value.id !== undefined && + (m.data).id > this.data.value.id) || + (this.isInit && (m.data).timestamp > this.data.value.timestamp) ) { this.isInit = true - this.data.next(>incomingMessage.data) + this.data.next(>m.data) this.resolveInitPromise() - const publishPromiseResolver = this.publishPromiseResolver[incomingMessage.data.timestamp] + const publishPromiseResolver = this.publishPromiseResolver[m.data.timestamp] if (publishPromiseResolver) { publishPromiseResolver() - delete this.publishPromiseResolver[incomingMessage.data.timestamp] + delete this.publishPromiseResolver[m.data.timestamp] } } else if ( - currentMessage && + this.data.value && this.isInit && - (incomingMessage.data).timestamp === currentMessage.timestamp && - ((incomingMessage.data).id || currentMessage.id) + (m.data).timestamp === this.data.value.timestamp && + ((m.data).id || this.data.value.id) ) { console.warn( - 'Message was swallowed because of equal timestamps. Please upgrate to the latest version to ensure messages are correctly timed' + 'Message was dropped because of equal timestamps, because there was an old style message in the system. Please upgrade all libraries to the latest version.' ) } break case TopicMessageType.TopicGet: - if ( - incomingMessage.data.name === this.name && - incomingMessage.data.version === this.version && - this.isInit && - currentMessage - ) { - window.postMessage(currentMessage, '*') - incomingMessage.stopImmediatePropagation() - incomingMessage.stopPropagation() + if (m.data.name === this.name && m.data.version === this.version && this.isInit && this.data.value) { + window.postMessage(this.data.value, '*') + m.stopImmediatePropagation() + m.stopPropagation() } break }