;(function () {
const pattern = window.__TAURI_INTERNALS__.__TAURI_PATTERN__.pattern
const isolationOrigin = __TEMPLATE_isolation_origin__
const isolation = Object.create(null)
isolation.queue = []
isolation.ready = false
isolation.frame = null
function isIsolationMessage(event) {
if (
typeof event.data === 'object' &&
typeof event.data.payload === 'object'
) {
const keys = Object.keys(event.data.payload || {})
return (
keys.length > 0 &&
keys.every(
(key) => key === 'contentType' || key === 'nonce' || key === 'payload'
)
)
}
return false
}
function isIsolationPayload(data) {
return (
typeof data === 'object' &&
'callback' in data &&
'error' in data &&
!isIsolationMessage(data)
)
}
function sendIsolationMessage(data) {
if (!isolation.frame) {
const frame = document.querySelector('iframe#__tauri_isolation__')
if (frame.src.startsWith(isolationOrigin)) {
isolation.frame = frame
} else {
console.error(
'Tauri IPC found an isolation iframe, but it had the wrong origin'
)
}
}
if (!isolation.frame || !isolation.frame.contentWindow) {
console.error(
'Tauri "Isolation" Pattern could not find the Isolation iframe window'
)
return
}
isolation.frame.contentWindow.postMessage(
data,
'*'
)
}
Object.defineProperty(window.__TAURI_INTERNALS__, 'ipc', {
value: Object.freeze((message) => {
switch (pattern) {
case 'brownfield':
window.__TAURI_INTERNALS__.postMessage(message)
break
case 'isolation':
if (!isIsolationPayload(message)) {
console.error(
'Tauri "Isolation" Pattern found an invalid isolation message payload',
message
)
break
}
if (isolation.ready) {
sendIsolationMessage(message)
} else {
isolation.queue.push(message)
}
break
case 'error':
console.error(
'Tauri IPC found a Tauri Pattern, but it was an error. Check for other log messages to find the cause.'
)
break
default:
console.error(
'Tauri IPC did not find a Tauri Pattern that it understood.'
)
break
}
})
})
if (pattern === 'isolation') {
window.addEventListener(
'message',
(event) => {
if (event.data === '__TAURI_ISOLATION_READY__') {
isolation.ready = true
for (const message of isolation.queue) {
sendIsolationMessage(message)
}
isolation.queue = []
return
}
if (isIsolationMessage(event)) {
window.__TAURI_INTERNALS__.postMessage(event.data)
}
},
false
)
}
})()