mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-19 00:21:32 +03:00
548ab94810
* feat(tauri.js) add API endpoint proxy * feat(tauri.js) always resolve/reject proxy promise * chore(proxy) wait for onTauriInit to start direct proxy to window.tauri
57 lines
1.1 KiB
JavaScript
57 lines
1.1 KiB
JavaScript
const cache = {}
|
|
let initialized = false
|
|
|
|
const proxy = new Proxy({
|
|
__consume () {
|
|
for (const key in cache) {
|
|
if (key in window.tauri) {
|
|
const queue = cache[key]
|
|
for (const call of queue) {
|
|
try {
|
|
const ret = window.tauri[key].apply(window.tauri, call.arguments)
|
|
if (ret instanceof Promise) {
|
|
ret.then(call.resolve, call.reject)
|
|
} else {
|
|
call.resolve()
|
|
}
|
|
} catch (e) {
|
|
call.reject(e)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
initialized = true
|
|
}
|
|
}, {
|
|
get (obj, prop) {
|
|
if (prop === '__consume') {
|
|
return obj[prop]
|
|
}
|
|
|
|
if (initialized && 'tauri' in window) {
|
|
return window.tauri[prop].bind(window.tauri)
|
|
}
|
|
|
|
if (!(prop in cache)) {
|
|
cache[prop] = []
|
|
}
|
|
return function () {
|
|
const promise = new Promise((resolve, reject) => {
|
|
cache[prop].push({
|
|
arguments: arguments,
|
|
resolve,
|
|
reject
|
|
})
|
|
});
|
|
return promise;
|
|
}
|
|
|
|
}
|
|
})
|
|
|
|
window.onTauriInit = () => {
|
|
proxy.__consume()
|
|
}
|
|
|
|
export default proxy
|