New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): add IPC channel #6813
Changes from 15 commits
cfed95a
6737c68
c4f7c14
c578b17
f6a0907
f5f3174
2a19f01
cffd9ce
c71f4fa
1cf7f9a
a40e9cd
088e049
40a8422
d7c0552
8a5240a
3ea4892
36cb3bf
b435550
2ee8303
4d28f04
6359fc9
0da8170
75e735d
823ac0a
9122e27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"api": patch | ||
"tauri": patch | ||
--- | ||
|
||
Add channel API for sending data across the IPC. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
package app.tauri.plugin | ||
|
||
class Channel(val id: Long, private val handler: (data: JSObject) -> Unit) { | ||
fun send(data: JSObject) { | ||
handler(data) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
public class Channel { | ||
var callback: UInt64 | ||
var handler: (JsonValue) -> Void | ||
|
||
public init(callback: UInt64, handler: @escaping (JsonValue) -> Void) { | ||
self.callback = callback | ||
self.handler = handler | ||
} | ||
|
||
public func send(_ data: JsonObject) { | ||
handler(.dictionary(data)) | ||
} | ||
} |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,29 @@ function transformCallback( | |
return identifier | ||
} | ||
|
||
class Channel { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would refactor this to be more similar to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, pushed. |
||
id: number | ||
|
||
constructor(id: number) { | ||
this.id = id | ||
} | ||
|
||
toJSON(): string { | ||
return `__CHANNEL__:${this.id}` | ||
} | ||
} | ||
|
||
/** | ||
* Creates a channel using the given handler function. | ||
* | ||
* @returns the channel to send to the IPC. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
function channel(fn: (response: any) => void): Channel { | ||
return new Channel(transformCallback(fn)) | ||
} | ||
|
||
/** | ||
* Command arguments. | ||
* | ||
|
@@ -133,6 +156,6 @@ function convertFileSrc(filePath: string, protocol = 'asset'): string { | |
: `${protocol}://localhost/${path}` | ||
} | ||
|
||
export type { InvokeArgs } | ||
export type { InvokeArgs, Channel } | ||
|
||
export { transformCallback, invoke, convertFileSrc } | ||
export { transformCallback, channel, invoke, convertFileSrc } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use
value.split_once(CHANNEL_PREFIX)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed.