Skip to content

Commit

Permalink
fix: add missing properties to new init method (#1338)
Browse files Browse the repository at this point in the history
fix: add missing properties to new `init` method
  • Loading branch information
andipaetzold authored Aug 2, 2022
1 parent c56037a commit 4370508
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,17 @@ export class Channel {
}
}

function createSender(sourceId: string, targetWindow: Window) {
const messageCounter = createMessageCounter()
function createMessageCounter() {
let messageCount = 0
return {
getMessageId: () => messageCount++,
}
}

function createSender(sourceId: string, targetWindow: Window) {
return function send(method: string, params: any) {
const messageId = messageCount++
const messageId = messageCounter.getMessageId()

try {
targetWindow.postMessage(
Expand All @@ -114,14 +121,19 @@ function createSender(sourceId: string, targetWindow: Window) {
}
}

export function sendInitMessage(currentGlobal: typeof globalThis) {
export function sendInitMessage(currentGlobal: typeof globalThis): number {
const messageId = messageCounter.getMessageId()
const targetWindow = currentGlobal.parent

// The app is not connected yet so we can't provide an `id` or `source`
// The app is not connected yet so we can't provide a `source`
targetWindow.postMessage(
{
id: messageId,
method: 'init',
params: [],
},
'*'
)

return messageId
}

0 comments on commit 4370508

Please sign in to comment.