Skip to content

Commit a63e71f

Browse files
refactor(core&api)!: hide internal functions and reuse them in api.js & rename tauri module to primitives (#7942)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent b89d747 commit a63e71f

File tree

40 files changed

+638
-522
lines changed

40 files changed

+638
-522
lines changed

.changes/api-primitives.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tauri-apps/api': 'major:breaking'
3+
---
4+
5+
Changed `tauri` module to `primitives` and removed the undocumented `invoke` export from the root module.

.changes/invoke-system-refactor.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:breaking
3+
---
4+
5+
The initialization script for `Builder::invoke_system` now must initialize the `window.__TAURI_INTERNALS__.postMessage` function instead of `window.__TAURI_POST_MESSAGE__`.

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
node_modules
66
target
77
dist
8-
/core/tauri/scripts
8+
/core/tauri/scripts/bundle.global.js
99
/tooling/cli/templates
1010
/tooling/cli/node
1111
/tooling/cli/schema.json

core/tauri-utils/src/pattern/isolation.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
* isolation frame -> main frame = isolation message
99
*/
1010

11-
;
12-
(async function () {
11+
;(async function () {
1312
/**
1413
* Sends the message to the isolation frame.
1514
* @param {any} message

core/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/scripts/core.js

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,48 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
; (function () {
5+
;(function () {
66
function uid() {
77
return window.crypto.getRandomValues(new Uint32Array(1))[0]
88
}
99

10-
if (!window.__TAURI__) {
11-
Object.defineProperty(window, '__TAURI__', {
12-
value: {}
13-
})
14-
}
15-
1610
const osName = __TEMPLATE_os_name__
1711

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+
})
2420

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}`
3125

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+
}
3731

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+
})
4337

44-
return identifier
45-
}
38+
return identifier
39+
}
40+
})
4641

4742
const ipcQueue = []
4843
let isWaitingForIpc = false
4944

5045
function waitForIpc() {
51-
if ('__TAURI_IPC__' in window) {
46+
if ('ipc' in window.__TAURI_INTERNALS__) {
5247
for (const action of ipcQueue) {
5348
action()
5449
}
@@ -57,35 +52,43 @@
5752
}
5853
}
5954

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)
7072

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+
})
8781
}
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+
})
9194
})()

core/tauri/scripts/init.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
; (function () {
5+
;(function () {
66
__RAW_freeze_prototype__
77

88
__RAW_pattern_script__
99

1010
__RAW_ipc_script__
11-
; (function () {
12-
__RAW_bundle_script__
13-
})()
1411

1512
__RAW_core_script__
1613

1714
__RAW_event_initialization_script__
15+
;(function () {
16+
__RAW_bundle_script__
17+
})()
1818

1919
if (window.ipc) {
20-
window.__TAURI_INVOKE__('__initialized', { url: window.location.href })
20+
window.__TAURI_INTERNALS__.invoke('__initialized', {
21+
url: window.location.href
22+
})
2123
} else {
2224
window.addEventListener('DOMContentLoaded', function () {
23-
window.__TAURI_INVOKE__('__initialized', { url: window.location.href })
25+
window.__TAURI_INTERNALS__.invoke('__initialized', {
26+
url: window.location.href
27+
})
2428
})
2529
}
2630

core/tauri/scripts/ipc-protocol.js

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
const fetchChannelDataCommand = __TEMPLATE_fetch_channel_data_command__
99
const useCustomProtocol = __TEMPLATE_use_custom_protocol__
1010

11-
Object.defineProperty(window, '__TAURI_POST_MESSAGE__', {
11+
Object.defineProperty(window.__TAURI_INTERNALS__, 'postMessage', {
1212
value: (message) => {
13-
const {
14-
cmd,
15-
callback,
16-
error,
17-
payload,
18-
options
19-
} = message
13+
const { cmd, callback, error, payload, options } = message
2014

2115
// use custom protocol for IPC if:
2216
// - the flag is set to true or
@@ -25,18 +19,16 @@
2519
// AND
2620
// - when not on macOS with an https URL
2721
if (
28-
(
29-
useCustomProtocol ||
22+
(useCustomProtocol ||
3023
cmd === fetchChannelDataCommand ||
31-
!(osName === 'linux' || osName === 'android')
32-
) &&
33-
!((osName === 'macos' || osName === 'ios') && location.protocol === 'https:')
24+
!(osName === 'linux' || osName === 'android')) &&
25+
!(
26+
(osName === 'macos' || osName === 'ios') &&
27+
location.protocol === 'https:'
28+
)
3429
) {
35-
const {
36-
contentType,
37-
data
38-
} = processIpcMessage(payload)
39-
fetch(window.__TAURI__.convertFileSrc(cmd, 'ipc'), {
30+
const { contentType, data } = processIpcMessage(payload)
31+
fetch(window.__TAURI_INTERNALS__.convertFileSrc(cmd, 'ipc'), {
4032
method: 'POST',
4133
body: data,
4234
headers: {
@@ -45,29 +37,33 @@
4537
'Tauri-Error': error,
4638
...options?.headers
4739
}
48-
}).then((response) => {
49-
const cb = response.ok ? callback : error
50-
// we need to split here because on Android the content-type gets duplicated
51-
switch ((response.headers.get('content-type') || '').split(',')[0]) {
52-
case 'application/json':
53-
return response.json().then((r) => [cb, r])
54-
case 'text/plain':
55-
return response.text().then((r) => [cb, r])
56-
default:
57-
return response.arrayBuffer().then((r) => [cb, r])
58-
}
59-
}).then(([cb, data]) => {
60-
if (window[`_${cb}`]) {
61-
window[`_${cb}`](data)
62-
} else {
63-
console.warn(`[TAURI] Couldn't find callback id {cb} in window. This might happen when the app is reloaded while Rust is running an asynchronous operation.`)
64-
}
6540
})
41+
.then((response) => {
42+
const cb = response.ok ? callback : error
43+
// we need to split here because on Android the content-type gets duplicated
44+
switch (
45+
(response.headers.get('content-type') || '').split(',')[0]
46+
) {
47+
case 'application/json':
48+
return response.json().then((r) => [cb, r])
49+
case 'text/plain':
50+
return response.text().then((r) => [cb, r])
51+
default:
52+
return response.arrayBuffer().then((r) => [cb, r])
53+
}
54+
})
55+
.then(([cb, data]) => {
56+
if (window[`_${cb}`]) {
57+
window[`_${cb}`](data)
58+
} else {
59+
console.warn(
60+
`[TAURI] Couldn't find callback id {cb} in window. This might happen when the app is reloaded while Rust is running an asynchronous operation.`
61+
)
62+
}
63+
})
6664
} else {
6765
// otherwise use the postMessage interface
68-
const {
69-
data
70-
} = processIpcMessage({
66+
const { data } = processIpcMessage({
7167
cmd,
7268
callback,
7369
error,

core/tauri/scripts/ipc.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
* @typedef {{callback: string, error: string, data: *}} IsolationPayload - a valid isolation payload
77
*/
88

9-
;
10-
(function () {
9+
;(function () {
1110
/**
1211
* @type {string}
1312
*/
14-
const pattern = window.__TAURI_PATTERN__.pattern
13+
const pattern = window.__TAURI_INTERNALS__.__TAURI_PATTERN__.pattern
1514

1615
/**
1716
* @type {string}
@@ -33,7 +32,10 @@
3332
* @return {boolean} - if the event was a valid isolation message
3433
*/
3534
function isIsolationMessage(event) {
36-
if (typeof event.data === 'object' && typeof event.data.payload === 'object') {
35+
if (
36+
typeof event.data === 'object' &&
37+
typeof event.data.payload === 'object'
38+
) {
3739
const keys = Object.keys(event.data.payload || {})
3840
return (
3941
keys.length > 0 &&
@@ -90,12 +92,12 @@
9092
)
9193
}
9294

93-
Object.defineProperty(window, '__TAURI_IPC__', {
95+
Object.defineProperty(window.__TAURI_INTERNALS__, 'ipc', {
9496
// todo: JSDoc this function
9597
value: Object.freeze((message) => {
9698
switch (pattern) {
9799
case 'brownfield':
98-
window.__TAURI_POST_MESSAGE__(message)
100+
window.__TAURI_INTERNALS__.postMessage(message)
99101
break
100102

101103
case 'isolation':
@@ -152,7 +154,7 @@
152154
}
153155

154156
if (isIsolationMessage(event)) {
155-
window.__TAURI_POST_MESSAGE__(event.data)
157+
window.__TAURI_INTERNALS__.postMessage(event.data)
156158
}
157159
},
158160
false

core/tauri/scripts/pattern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
return Object.freeze(object)
1616
}
1717

18-
Object.defineProperty(window, '__TAURI_PATTERN__', {
18+
Object.defineProperty(window.__TAURI_INTERNALS__, '__TAURI_PATTERN__', {
1919
value: __tauriDeepFreeze(__TEMPLATE_pattern__)
2020
})
2121
})()

0 commit comments

Comments
 (0)