Skip to content

Commit

Permalink
refactor: tests and topic adjusted
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jun 3, 2024
1 parent 0807e6a commit c8589f6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 76 deletions.
2 changes: 1 addition & 1 deletion libs/accelerator/src/lib/topic/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
100 changes: 50 additions & 50 deletions libs/accelerator/src/lib/topic/topic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ describe('Topic', () => {
})
})

describe('on message', () => {
let currentMessage: TopicDataMessage<string>
describe('integration with older versions of library', () => {
let previousMessage: TopicDataMessage<string>
let incomingMessage: MessageEvent<TopicDataMessage<string>>

beforeEach(() => {
currentMessage = {
previousMessage = {
type: TopicMessageType.TopicNext,
name: testTopic1.name,
version: testTopic1.version,
Expand All @@ -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
;(<any>testTopic1).data.next(previousMessage)
;(<any>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'
;(<any>previousMessage).id = undefined
previousMessage.timestamp = 1
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = undefined!
;(<any>incomingMessage.data).id = undefined
incomingMessage.data.timestamp = 3
;(<any>testTopic1).data.next(previousMessage)
;(<any>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!
;(<any>incomingMessage.data).id = undefined
incomingMessage.data.timestamp = 3
;(<any>testTopic1).data.next(previousMessage)
;(<any>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'
;(<any>previousMessage).id = undefined
previousMessage.timestamp = 1
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = 1
incomingMessage.data.timestamp = 3
;(<any>testTopic1).data.next(previousMessage)
;(<any>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'
;(<any>previousMessage).id = undefined
previousMessage.timestamp = 3
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = undefined!
;(<any>incomingMessage.data).id = undefined
incomingMessage.data.timestamp = 3
;(<any>testTopic1).data.next(previousMessage)
;(<any>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!
;(<any>incomingMessage.data).id = undefined
incomingMessage.data.timestamp = 3
;(<any>testTopic1).data.next(previousMessage)
;(<any>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'
;(<any>previousMessage).id = undefined
previousMessage.timestamp = 3
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = 1
incomingMessage.data.timestamp = 3
;(<any>testTopic1).data.next(previousMessage)
;(<any>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.'
)
})
})
Expand Down
45 changes: 20 additions & 25 deletions libs/accelerator/src/lib/topic/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Topic<T> extends TopicPublisher<T> implements Subscribable<T> {

protected isInit = false
private resolveInitPromise!: (value: void | PromiseLike<void>) => void
private eventListener = (m: MessageEvent<TopicMessage>) => this.onMessage(this.data.value, m)
private eventListener = (m: MessageEvent<TopicMessage>) => this.onMessage(m)

constructor(name: string, version: number, sendGetMessage = true) {
super(name, version)
Expand Down Expand Up @@ -142,50 +142,45 @@ export class Topic<T> extends TopicPublisher<T> implements Subscribable<T> {
window.removeEventListener('message', this.eventListener, true)
}

public onMessage(currentMessage: TopicDataMessage<T> | undefined, incomingMessage: MessageEvent<TopicMessage>): any {
switch (incomingMessage.data.type) {
private onMessage(m: MessageEvent<TopicMessage>): 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 &&
(<TopicMessage>incomingMessage.data).id !== undefined &&
currentMessage.id !== undefined &&
(<TopicMessage>incomingMessage.data).id > currentMessage.id) ||
(this.isInit && (<TopicMessage>incomingMessage.data).timestamp > currentMessage.timestamp)
(<TopicMessage>m.data).id !== undefined &&
this.data.value.id !== undefined &&
(<TopicMessage>m.data).id > this.data.value.id) ||
(this.isInit && (<TopicMessage>m.data).timestamp > this.data.value.timestamp)
) {
this.isInit = true
this.data.next(<TopicDataMessage<T>>incomingMessage.data)
this.data.next(<TopicDataMessage<T>>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 &&
(<TopicMessage>incomingMessage.data).timestamp === currentMessage.timestamp &&
((<TopicMessage>incomingMessage.data).id || currentMessage.id)
(<TopicMessage>m.data).timestamp === this.data.value.timestamp &&
((<TopicMessage>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
}
Expand Down

0 comments on commit c8589f6

Please sign in to comment.