Skip to content

Commit 428ea65

Browse files
feat(api): expose Resource class (#8370)
* feat(api): expose `Resource` class continuation of #8276 * Apply suggestions from code review * fmt --------- Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app> Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent face0b6 commit 428ea65

7 files changed

Lines changed: 53 additions & 40 deletions

File tree

.changes/api-Resource.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tauri-apps/api': 'patch:feat'
3+
---
4+
5+
Exposed `Resource` class which should be extended for Rust-backed resources created through `tauri::Manager::resources_table`.

.changes/tauri-resources-table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'tauri': 'patch:feat'
33
---
44

5-
Exposed `Manager::resources_table` to access the resources table used by tauri, which could be used by plugins or app authors to store their resources and retrieve it later using an id.
5+
Exposed `Manager::resources_table` to access the resources table used by tauri, which could be used by plugins or app authors to store their resources and retrieve it later using an id and can be used to create Rust-backed resources in JS.

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.

tooling/api/src/core.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,50 @@ function convertFileSrc(filePath: string, protocol = 'asset'): string {
160160
return window.__TAURI_INTERNALS__.convertFileSrc(filePath, protocol)
161161
}
162162

163+
/**
164+
* A rust-backed resource stored through `tauri::Manager::resources_table` API.
165+
*
166+
* The resource lives in the main process and does not exist
167+
* in the Javascript world, and thus will not be cleaned up automatiacally
168+
* except on application exit. If you want to clean it up early, call {@linkcode Resource.close}
169+
*
170+
* @example
171+
* ```typescript
172+
* import { Resource, invoke } from '@tauri-apps/api/core';
173+
* export class DatabaseHandle extends Resource {
174+
* static async open(path: string): Promise<DatabaseHandle> {
175+
* const rid: number = await invoke('open_db', { path });
176+
* return new DatabaseHandle(rid);
177+
* }
178+
*
179+
* async execute(sql: string): Promise<void> {
180+
* await invoke('execute_sql', { rid: this.rid, sql });
181+
* }
182+
* }
183+
* ```
184+
*/
185+
export class Resource {
186+
readonly #rid: number
187+
188+
get rid(): number {
189+
return this.#rid
190+
}
191+
192+
constructor(rid: number) {
193+
this.#rid = rid
194+
}
195+
196+
/**
197+
* Destroys and cleans up this resource from memory.
198+
* **You should not call any method on this object anymore and should drop any reference to it.**
199+
*/
200+
async close(): Promise<void> {
201+
return invoke('plugin:resources|close', {
202+
rid: this.rid
203+
})
204+
}
205+
}
206+
163207
export type { InvokeArgs, InvokeOptions }
164208

165209
export {

tooling/api/src/internal/index.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

tooling/api/src/menu/base.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
import { Resource } from '../internal'
6-
import { Channel, invoke } from '../core'
5+
import { Channel, invoke, Resource } from '../core'
76
import { CheckMenuItemOptions } from './checkMenuItem'
87
import { IconMenuItemOptions } from './iconMenuItem'
98
import { MenuItemOptions } from './menuItem'

tooling/api/src/tray.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
import type { Menu, Submenu } from './menu'
6-
import { Resource } from './internal'
7-
import { Channel, invoke } from './core'
6+
import { Channel, invoke, Resource } from './core'
87

98
/**
109
* Describes a tray event emitted when a tray icon is clicked

0 commit comments

Comments
 (0)