|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | // SPDX-License-Identifier: MIT |
4 | 4 |
|
5 | | -; (function () { |
| 5 | +;(function () { |
6 | 6 | function uid() { |
7 | 7 | return window.crypto.getRandomValues(new Uint32Array(1))[0] |
8 | 8 | } |
9 | 9 |
|
10 | | - if (!window.__TAURI__) { |
11 | | - Object.defineProperty(window, '__TAURI__', { |
12 | | - value: {} |
13 | | - }) |
14 | | - } |
15 | | - |
16 | 10 | const osName = __TEMPLATE_os_name__ |
17 | 11 |
|
18 | | - window.__TAURI__.convertFileSrc = function convertFileSrc(filePath, protocol = 'asset') { |
19 | | - const path = encodeURIComponent(filePath) |
20 | | - return osName === 'windows' || osName === 'android' |
21 | | - ? `http://${protocol}.localhost/${path}` |
22 | | - : `${protocol}://localhost/${path}` |
23 | | - } |
| 12 | + Object.defineProperty(window.__TAURI_INTERNALS__, 'convertFileSrc', { |
| 13 | + value: function (filePath, protocol = 'asset') { |
| 14 | + const path = encodeURIComponent(filePath) |
| 15 | + return osName === 'windows' || osName === 'android' |
| 16 | + ? `http://${protocol}.localhost/${path}` |
| 17 | + : `${protocol}://localhost/${path}` |
| 18 | + } |
| 19 | + }) |
24 | 20 |
|
25 | | - window.__TAURI__.transformCallback = function transformCallback( |
26 | | - callback, |
27 | | - once |
28 | | - ) { |
29 | | - var identifier = uid() |
30 | | - var prop = `_${identifier}` |
| 21 | + Object.defineProperty(window.__TAURI_INTERNALS__, 'transformCallback', { |
| 22 | + value: function transformCallback(callback, once) { |
| 23 | + var identifier = uid() |
| 24 | + var prop = `_${identifier}` |
31 | 25 |
|
32 | | - Object.defineProperty(window, prop, { |
33 | | - value: (result) => { |
34 | | - if (once) { |
35 | | - Reflect.deleteProperty(window, prop) |
36 | | - } |
| 26 | + Object.defineProperty(window, prop, { |
| 27 | + value: (result) => { |
| 28 | + if (once) { |
| 29 | + Reflect.deleteProperty(window, prop) |
| 30 | + } |
37 | 31 |
|
38 | | - return callback && callback(result) |
39 | | - }, |
40 | | - writable: false, |
41 | | - configurable: true |
42 | | - }) |
| 32 | + return callback && callback(result) |
| 33 | + }, |
| 34 | + writable: false, |
| 35 | + configurable: true |
| 36 | + }) |
43 | 37 |
|
44 | | - return identifier |
45 | | - } |
| 38 | + return identifier |
| 39 | + } |
| 40 | + }) |
46 | 41 |
|
47 | 42 | const ipcQueue = [] |
48 | 43 | let isWaitingForIpc = false |
49 | 44 |
|
50 | 45 | function waitForIpc() { |
51 | | - if ('__TAURI_IPC__' in window) { |
| 46 | + if ('ipc' in window.__TAURI_INTERNALS__) { |
52 | 47 | for (const action of ipcQueue) { |
53 | 48 | action() |
54 | 49 | } |
|
57 | 52 | } |
58 | 53 | } |
59 | 54 |
|
60 | | - window.__TAURI_INVOKE__ = function invoke(cmd, payload = {}, options) { |
61 | | - return new Promise(function (resolve, reject) { |
62 | | - const callback = window.__TAURI__.transformCallback(function (r) { |
63 | | - resolve(r) |
64 | | - delete window[`_${error}`] |
65 | | - }, true) |
66 | | - const error = window.__TAURI__.transformCallback(function (e) { |
67 | | - reject(e) |
68 | | - delete window[`_${callback}`] |
69 | | - }, true) |
| 55 | + Object.defineProperty(window.__TAURI_INTERNALS__, 'invoke', { |
| 56 | + value: function (cmd, payload = {}, options) { |
| 57 | + return new Promise(function (resolve, reject) { |
| 58 | + const callback = window.__TAURI_INTERNALS__.transformCallback(function ( |
| 59 | + r |
| 60 | + ) { |
| 61 | + resolve(r) |
| 62 | + delete window[`_${error}`] |
| 63 | + }, |
| 64 | + true) |
| 65 | + const error = window.__TAURI_INTERNALS__.transformCallback(function ( |
| 66 | + e |
| 67 | + ) { |
| 68 | + reject(e) |
| 69 | + delete window[`_${callback}`] |
| 70 | + }, |
| 71 | + true) |
70 | 72 |
|
71 | | - const action = () => { |
72 | | - window.__TAURI_IPC__({ |
73 | | - cmd, |
74 | | - callback, |
75 | | - error, |
76 | | - payload, |
77 | | - options |
78 | | - }) |
79 | | - } |
80 | | - if (window.__TAURI_IPC__) { |
81 | | - action() |
82 | | - } else { |
83 | | - ipcQueue.push(action) |
84 | | - if (!isWaitingForIpc) { |
85 | | - waitForIpc() |
86 | | - isWaitingForIpc = true |
| 73 | + const action = () => { |
| 74 | + window.window.__TAURI_INTERNALS__.ipc({ |
| 75 | + cmd, |
| 76 | + callback, |
| 77 | + error, |
| 78 | + payload, |
| 79 | + options |
| 80 | + }) |
87 | 81 | } |
88 | | - } |
89 | | - }) |
90 | | - } |
| 82 | + if ('ipc' in window.__TAURI_INTERNALS__) { |
| 83 | + action() |
| 84 | + } else { |
| 85 | + ipcQueue.push(action) |
| 86 | + if (!isWaitingForIpc) { |
| 87 | + waitForIpc() |
| 88 | + isWaitingForIpc = true |
| 89 | + } |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | + }) |
91 | 94 | })() |
0 commit comments