Skip to content

Commit

Permalink
feat(api): expose Resource class (#8370)
Browse files Browse the repository at this point in the history
* feat(api): expose `Resource` class

continuation of #8276

* Apply suggestions from code review

* fmt

---------

Co-authored-by: Lucas Fernandes Nogueira <[email protected]>
Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
3 people authored Dec 18, 2023
1 parent face0b6 commit 428ea65
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changes/api-Resource.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tauri-apps/api': 'patch:feat'
---

Exposed `Resource` class which should be extended for Rust-backed resources created through `tauri::Manager::resources_table`.
2 changes: 1 addition & 1 deletion .changes/tauri-resources-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'tauri': 'patch:feat'
---

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.
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.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions tooling/api/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,50 @@ function convertFileSrc(filePath: string, protocol = 'asset'): string {
return window.__TAURI_INTERNALS__.convertFileSrc(filePath, protocol)
}

/**
* 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 {@linkcode Resource.close}
*
* @example
* ```typescript
* 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 });
* }
* }
* ```
*/
export class Resource {
readonly #rid: number

get rid(): number {
return this.#rid
}

constructor(rid: number) {
this.#rid = rid
}

/**
* 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.**
*/
async close(): Promise<void> {
return invoke('plugin:resources|close', {
rid: this.rid
})
}
}

export type { InvokeArgs, InvokeOptions }

export {
Expand Down
34 changes: 0 additions & 34 deletions tooling/api/src/internal/index.ts

This file was deleted.

3 changes: 1 addition & 2 deletions tooling/api/src/menu/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

import { Resource } from '../internal'
import { Channel, invoke } from '../core'
import { Channel, invoke, Resource } from '../core'
import { CheckMenuItemOptions } from './checkMenuItem'
import { IconMenuItemOptions } from './iconMenuItem'
import { MenuItemOptions } from './menuItem'
Expand Down
3 changes: 1 addition & 2 deletions tooling/api/src/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// SPDX-License-Identifier: MIT

import type { Menu, Submenu } from './menu'
import { Resource } from './internal'
import { Channel, invoke } from './core'
import { Channel, invoke, Resource } from './core'

/**
* Describes a tray event emitted when a tray icon is clicked
Expand Down

0 comments on commit 428ea65

Please sign in to comment.