-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(deps): add
uuid
, anyhow
and oblivion
to Cargo.toml
- Loading branch information
Showing
4 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |