core
Invoke your custom commands.
This package is also accessible with window.__TAURI__.tauri when app.withGlobalTauri in tauri.conf.json is set to true.
Classes
Channel
Type parameters
| Parameter | Default |
|---|---|
T | unknown |
Constructors
constructor()
new Channel<T>(): Channel< T >Type parameters
| Parameter | Default |
|---|---|
T | unknown |
Returns
Channel< T >
Source: core.ts:37
Properties
| Property | Type |
|---|---|
private #nextMessageId | number |
private #onmessage | (response) => void |
private #pendingMessages | Record< string, T > |
private readonly __TAURI_CHANNEL_MARKER__ | true |
id | number |
Accessors
onmessage
get onmessage(): (response) => voidSources: core.ts:75, core.ts:79
Methods
toJSON()
toJSON(): stringReturns
string
Source: core.ts:83
PluginListener
Constructors
constructor()
new PluginListener( plugin, event, channelId): PluginListenerParameters
| Parameter | Type |
|---|---|
plugin | string |
event | string |
channelId | number |
Returns
Source: core.ts:93
Properties
| Property | Type |
|---|---|
channelId | number |
event | string |
plugin | string |
Methods
unregister()
unregister(): Promise< void >Returns
Promise< void >
Source: core.ts:99
Resource
A rust-backed resource stored through tauri::Manager::resources_table API.
The resource lives in the main process and does not exist
in the Javascript world, and thus will not be cleaned up automatiacally
except on application exit. If you want to clean it up early, call Resource.close
Example
import { Resource, invoke } from '@tauri-apps/api/core';export class DatabaseHandle extends Resource { static async open(path: string): Promise<DatabaseHandle> { const rid: number = await invoke('open_db', { path }); return new DatabaseHandle(rid); }
async execute(sql: string): Promise<void> { await invoke('execute_sql', { rid: this.rid, sql }); }}Extended By
Constructors
constructor()
new Resource(rid): ResourceParameters
| Parameter | Type |
|---|---|
rid | number |
Returns
Source: core.ts:226
Properties
| Property | Type |
|---|---|
private readonly #rid | number |
Accessors
rid
get rid(): numberSource: core.ts:222
Methods
close()
close(): Promise< void >Destroys and cleans up this resource from memory. You should not call any method on this object anymore and should drop any reference to it.
Returns
Promise< void >
Source: core.ts:234
Interfaces
InvokeOptions
Since
2.0.0
Properties
| Property | Type |
|---|---|
headers | Headers | Record< string, string > |
Type Aliases
InvokeArgs
InvokeArgs: Record< string, unknown > | number[] | ArrayBuffer | Uint8ArrayCommand arguments.
Since
1.0.0
Source: core.ts:131
Functions
addPluginListener()
addPluginListener<T>( plugin, event, cb): Promise< PluginListener >Adds a listener to a plugin event.
Since
2.0.0
Type parameters
| Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
plugin | string |
event | string |
cb | (payload) => void |
Returns
The listener object to stop listening to the events.
Source: core.ts:114
convertFileSrc()
convertFileSrc(filePath, protocol = 'asset'): stringConvert a device file path to an URL that can be loaded by the webview.
Note that asset: and http://asset.localhost must be added to tauri.security.csp in tauri.conf.json.
Example CSP value: "csp": "default-src 'self' ipc: http://ipc.localhost; img-src 'self' asset: http://asset.localhost" to use the asset protocol on image sources.
Additionally, asset must be added to tauri.allowlist.protocol
in tauri.conf.json and its access scope must be defined on the assetScope array on the same protocol object.
Example
import { appDataDir, join } from '@tauri-apps/api/path';import { convertFileSrc } from '@tauri-apps/api/core';const appDataDirPath = await appDataDir();const filePath = await join(appDataDirPath, 'assets/video.mp4');const assetUrl = convertFileSrc(filePath);
const video = document.getElementById('my-video');const source = document.createElement('source');source.type = 'video/mp4';source.src = assetUrl;video.appendChild(source);video.load();Since
1.0.0
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
filePath | string | undefined | The file path. |
protocol | string | 'asset' | The protocol to use. Defaults to asset. You only need to set this when using a custom protocol. |
Returns
string
the URL that can be used as source on the webview.
Source: core.ts:193
invoke()
invoke<T>( cmd, args = {}, options?): Promise< T >Sends a message to the backend.
Example
import { invoke } from '@tauri-apps/api/core';await invoke('login', { user: 'tauri', password: 'poiwe3h4r5ip3yrhtew9ty' });Since
1.0.0
Type parameters
| Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
cmd | string | The command name. |
args | InvokeArgs | The optional arguments to pass to the command. |
options? | InvokeOptions | The request options. |
Returns
Promise< T >
A promise resolving or rejecting to the backend response.
Source: core.ts:155
isTauri()
isTauri(): unknownReturns
unknown
Source: core.ts:241
transformCallback()
transformCallback<T>(callback?, once? = false): numberTransforms a callback function to a string identifier that can be passed to the backend.
The backend uses the identifier to eval() the callback.
Since
1.0.0
Type parameters
| Parameter | Default |
|---|---|
T | unknown |
Parameters
| Parameter | Type | Default value |
|---|---|---|
callback? | (response) => void | undefined |
once? | boolean | false |
Returns
number
A unique identifier associated with the callback function.
Source: core.ts:20
© 2024 Tauri Contributors. CC-BY / MIT