Skip to content

Commit

Permalink
chore(deps): add uuid, anyhow and oblivion to Cargo.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed May 17, 2024
1 parent 3b19647 commit ffd4bdb
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tauri-plugin-oblivion"
version = "0.0.0"
version = "0.1.0"
authors = [ "You" ]
description = ""
edition = "2021"
Expand All @@ -12,8 +12,10 @@ links = "tauri-plugin-oblivion"
tauri = { version = "2.0.0-beta.19" }
serde = "1.0"
thiserror = "1.0"
oblivion = { path = "./oblivion" }
oblivion = { path = "./oblivion", features = ["serde"] }
tokio = { version = "1.37.0", features = ["sync"] }
uuid = { version = "1.8.0", features = ["v4"] }
anyhow = "1.0.83"

[build-dependencies]
tauri-plugin = { version = "2.0.0-beta.15", features = ["build"] }
87 changes: 84 additions & 3 deletions guest-js/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,86 @@
import { invoke } from '@tauri-apps/api/core'
import { invoke } from "@tauri-apps/api/core";

export async function connect() {
await invoke('plugin:oblivion|connect')
interface ClientInstance {
uuid: string;
entrance: string;
message: string;
success: boolean;
}

interface ResponseData {
header: string;
content: Uint8Array;
entrance: string;
status_code: number;
flag: number;
}

class Response implements ResponseData {
header: string;
content: Uint8Array;
entrance: string;
status_code: number;
flag: number;

constructor(response: ResponseData) {
this.header = response.header;
this.content = new Uint8Array(response.content);
this.entrance = response.entrance;
this.status_code = response.status_code;
this.flag = response.flag;
}

get text() {
return new TextDecoder().decode(this.content);
}

get json() {
return JSON.parse(this.text);
}
}

class Client {
closed: boolean = false;

constructor(public uuid: string | null, public entrance: string) {
this.uuid = uuid;
this.entrance = entrance;
}

async connect(entrance: string | null = null): Promise<void> {
if (!entrance && !this.entrance) {
throw new Error("An oblivion entrance is required");
}
const result: ClientInstance = await invoke("plugin:oblivion|connect", {
entrance: entrance || this.entrance,
});
this.uuid = result.uuid;
}

async close(): Promise<void> {
await invoke("plugin:oblivion|close", { uuid: this.uuid });
this.closed = true;
}

async send_json(data: any): Promise<boolean> {
const result: boolean = await invoke("plugin:oblivion|send_json", {
uuid: this.uuid,
data,
});
return result;
}

async recv(): Promise<Response> {
const result: ResponseData = await invoke("plugin:oblivion|recv", {
uuid: this.uuid,
});
return new Response(result);
}
}

export async function connect(entrance: string): Promise<Client> {
let client: ClientInstance = await invoke("plugin:oblivion|connect", {
entrance,
});
return new Client(client.uuid, entrance);
}
2 changes: 1 addition & 1 deletion oblivion

0 comments on commit ffd4bdb

Please sign in to comment.