Skip to content

Commit 66e6325

Browse files
fix: channel callback never cleaned up from window (#13136)
* Fix channel cb never cleaned up from `window` * Should be `_{id}` * Still need to manually impl clone * Regenerate bundle.global.js * Remove current_index from ChannelInner * Move phantom to `Channel` * `Channel` not `Self` * Clean up * Clean up * Fix missing end quote * Add change file * Rename id to index to match js side * Improve channel speed on small data * do the same perf check for IPC responses and raw bytes --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 0aa48fb commit 66e6325

10 files changed

Lines changed: 226 additions & 122 deletions

File tree

.changes/channel-ctor-callback.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": minor:feat
3+
---
4+
5+
Allow passing the callback as the parameter of constructor of `Channel` so you can use it like this `new Channel((message) => console.log(message))`
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": minor:bug
3+
"@tauri-apps/api": minor:bug
4+
---
5+
6+
Fix `Channel`'s callback attached to `window` never cleaned up
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": minor:perf
3+
---
4+
5+
Improve `Channel`'s performance when sending small amount of data (e.g. sending a number)

Cargo.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/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.

crates/tauri/scripts/core.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
Object.defineProperty(window.__TAURI_INTERNALS__, 'transformCallback', {
2323
value: function transformCallback(callback, once) {
24-
var identifier = uid()
25-
var prop = `_${identifier}`
24+
const identifier = uid()
25+
const prop = `_${identifier}`
2626

2727
Object.defineProperty(window, prop, {
2828
value: (result) => {
@@ -56,15 +56,11 @@
5656
Object.defineProperty(window.__TAURI_INTERNALS__, 'invoke', {
5757
value: function (cmd, payload = {}, options) {
5858
return new Promise(function (resolve, reject) {
59-
const callback = window.__TAURI_INTERNALS__.transformCallback(function (
60-
r
61-
) {
59+
const callback = window.__TAURI_INTERNALS__.transformCallback((r) => {
6260
resolve(r)
6361
delete window[`_${error}`]
6462
}, true)
65-
const error = window.__TAURI_INTERNALS__.transformCallback(function (
66-
e
67-
) {
63+
const error = window.__TAURI_INTERNALS__.transformCallback((e) => {
6864
reject(e)
6965
delete window[`_${callback}`]
7066
}, true)

crates/tauri/scripts/process-ipc-message-fn.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
(function (message) {
88
if (
9-
message instanceof ArrayBuffer ||
10-
ArrayBuffer.isView(message) ||
11-
Array.isArray(message)
9+
message instanceof ArrayBuffer
10+
|| ArrayBuffer.isView(message)
11+
|| Array.isArray(message)
1212
) {
1313
return {
1414
contentType: 'application/octet-stream',
@@ -27,15 +27,13 @@
2727
return Array.from(val)
2828
} else if (val instanceof ArrayBuffer) {
2929
return Array.from(new Uint8Array(val))
30-
} else if (typeof val === "object" && val !== null && SERIALIZE_TO_IPC_FN in val) {
31-
return val[SERIALIZE_TO_IPC_FN]()
3230
} else if (
33-
val instanceof Object &&
34-
'__TAURI_CHANNEL_MARKER__' in val &&
35-
typeof val.id === 'number'
31+
typeof val === 'object'
32+
&& val !== null
33+
&& SERIALIZE_TO_IPC_FN in val
3634
) {
37-
return `__CHANNEL__:${val.id}`
38-
} else {
35+
return val[SERIALIZE_TO_IPC_FN]()
36+
} else {
3937
return val
4038
}
4139
})

0 commit comments

Comments
 (0)