|
@@ -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 {
|