Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 2x 2x 2x 2x 2x 2x 2x | import { MessageListener } from '@wirewheel/cmp/src/entities/MessageListener'
import { Message } from '@wirewheel/cmp/src/entities/Message'
import { MessageType } from '@wirewheel/cmp/src/entities/MessageType'
import { isChildWindow } from '@wirewheel/cmp/src/utils/isChildWindow'
import { MessageService } from '@wirewheel/cmp/src/modules/MessageService'
import { Subject } from '@wirewheel/cmp/src/entities/Subject'
import { Action } from '@wirewheel/cmp/src/entities/Action'
import { PersistentStore } from '@wirewheel/cmp/src/modules/PersistentStore'
import { PersistentStoreFactory } from '@wirewheel/cmp/src/modules/PersistentStoreFactory'
import { CookieConfig } from '@wirewheel/cmp/src/entities/CookieConfig'
import { ChildEmbeddedConfig } from '@wirewheel/cmp/src/entities/ChildEmbeddedConfig'
/** Interval to wait for the child to be ready, in milliseconds. */
const CHILD_WAIT_INTERVAL = 250
/** Timeout to stop waiting for the child window, in milliseconds. */
const CHILD_WAIT_TIMEOUT = 1000000
export class EmbeddedHandler {
private childLoaded = false
static createFromParent(targetIframe: HTMLIFrameElement, cookieConfig?: CookieConfig): EmbeddedHandler {
const messageService = MessageService.createFromParent(targetIframe)
const handler = new EmbeddedHandler(messageService, cookieConfig)
handler.initParent()
return handler
}
static createFromChild(childEmbeddedConfig?: ChildEmbeddedConfig): EmbeddedHandler {
const messageService = MessageService.createFromChild()
const handler = new EmbeddedHandler(messageService, childEmbeddedConfig?.cookieConfig)
handler.initChild()
return handler
}
private readonly store: PersistentStore
constructor(private readonly messageService: MessageService, private readonly cookieConfig?: CookieConfig) {
this.store = PersistentStoreFactory.getStore(cookieConfig)
}
on(type: MessageType, listener: MessageListener) {
this.messageService.on(type, listener)
}
send(message: Message) {
this.messageService.send(message)
}
login(subject: Subject) {
this.messageService.send({ type: MessageType.LOGIN, data: { subject } })
}
logout(subject: Subject) {
this.messageService.send({ type: MessageType.LOGOUT, data: { subject } })
}
createConsent(actions: Action[]) {
this.messageService.send({ type: MessageType.CONSENT_WRITE, data: { actions } })
}
initParent() {
Iif (isChildWindow()) {
console.log('invalid call to initParent(), this is a child window')
return
}
// Listen events in child application
this.messageService.on(MessageType.LOGIN, (message) => {
const { subject } = message.data
this.store.setSubject(subject)
})
this.messageService.on(MessageType.LOGOUT, (message) => {
const { subject } = message.data
this.store.setSubject(subject)
})
this.messageService.on(MessageType.CHILD_LOADED, (message) => {
Iif (!this.childLoaded) {
this.store.setSubject(message.data.subject)
this.childLoaded = true
}
})
const queryString = window.location.search
const params = new URLSearchParams(queryString)
const sessionId = params.get('sessionId')
setTimeout(this.sendHandshake.bind(this, undefined, sessionId), CHILD_WAIT_INTERVAL)
}
initChild(): void {
Iif (!isChildWindow()) {
console.log('invalid call to initChild(), this is a top level window')
return
}
this.messageService.on(MessageType.CHILD_INIT, (message) => {
Iif (message.data.subject) {
this.store.setSubject(message.data.subject)
}
this.store.setParentDomain(message.data.parentDomain)
this.messageService.send({
type: MessageType.CHILD_LOADED,
data: {
subject: this.store.getSubject(),
},
})
})
}
getParentDomain(): string | undefined {
return this.store.getParentDomain()
}
private sendHandshake(start: number = Date.now(), sessionId?: string | null) {
const elapsed: number = Date.now() - start
Iif (!this.childLoaded) {
Iif (elapsed > CHILD_WAIT_TIMEOUT) {
throw new Error('no answer from child window, embedded client initialization failed')
}
// Overrides the subject in the child app, if any
this.messageService.send({
type: MessageType.CHILD_INIT,
data: {
subject: this.store.getSubject(),
parentDomain: window.location.href,
sessionId: sessionId ?? undefined,
},
})
setTimeout(() => this.sendHandshake(start, sessionId), CHILD_WAIT_INTERVAL)
}
}
}
|