Skip to content

Commit

Permalink
Queryable version (#48)
Browse files Browse the repository at this point in the history
* Add getVersion method

* Bump to 0.0.9 (binary 0.1.8)
  • Loading branch information
zephraph authored Sep 23, 2024
1 parent 897c615 commit 3bd1304
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.0.9 (binary 0.1.8) -- 2024-09-23

- Adds a `getVersion` method to `Webview` that returns the binary version.
- Adds a check on startup that compares the expected version to the current version.
- Adds slightly more graceful error handling to deserialization errors in the deno code.

## 0.0.8 (binary 0.1.7) -- 2024-09-23

NOTE: The binary version was bumped this release, but it doesn't actually have changes.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deno-webview"
version = "0.1.7"
version = "0.1.8"
edition = "2021"

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@justbe/webview",
"exports": "./src/lib.ts",
"version": "0.0.8",
"version": "0.0.9",
"tasks": {
"dev": "deno run --watch main.ts",
"gen": "deno task gen:rust && deno task gen:deno",
Expand Down
6 changes: 5 additions & 1 deletion schemas/WebViewMessage.json

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

18 changes: 18 additions & 0 deletions schemas/WebViewRequest.json

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

49 changes: 45 additions & 4 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type { WebViewOptions } from "./schemas.ts";

// Should match the cargo package version
/** The version of the webview binary that's expected */
export const BIN_VERSION = "0.1.7";
export const BIN_VERSION = "0.1.8";

type JSON =
| string
Expand Down Expand Up @@ -234,7 +234,12 @@ export class WebView implements Disposable {
return new Promise((resolve) => {
// Setup listener before sending the message to avoid race conditions
this.#internalEvent.once(id, (event) => {
resolve(WebViewResponse.parse(event));
const result = WebViewResponse.safeParse(event);
if (result.success) {
resolve(result.data);
} else {
resolve({ $type: "err", id, message: result.error.message });
}
});
this.#stdin.write(
new TextEncoder().encode(
Expand All @@ -256,23 +261,51 @@ export class WebView implements Disposable {
if (NulCharIndex === -1) {
continue;
}
const result = WebViewMessage.parse(
const result = WebViewMessage.safeParse(
JSON.parse(this.#buffer.slice(0, NulCharIndex)),
);
this.#buffer = this.#buffer.slice(NulCharIndex + 1);
return result;
if (result.success) {
return result.data;
} else {
console.error("Error parsing message", result.error);
return result;
}
}
}

async #processMessageLoop() {
while (true) {
const result = await this.#recv();
if (!result) return;
if ("error" in result) {
// TODO: This should be handled more gracefully
for (const issue of result.error.issues) {
switch (issue.code) {
case "invalid_type":
console.error(
`Invalid type: expected ${issue.expected} but got ${issue.received}`,
);
break;
default:
console.error(`Unknown error: ${issue.message}`);
}
}
continue;
}
const { $type, data } = result;

if ($type === "notification") {
const notification = data;
this.#externalEvent.emit(notification.$type);
if (notification.$type === "started") {
const version = notification.version;
if (version !== BIN_VERSION) {
console.warn(
`Expected webview to be version ${BIN_VERSION} but got ${version}. Some features may not work as expected.`,
);
}
}
if (notification.$type === "closed") {
return;
}
Expand Down Expand Up @@ -312,6 +345,14 @@ export class WebView implements Disposable {
this.#externalEvent.once(event, callback);
}

/**
* Gets the version of the webview binary.
*/
async getVersion(): Promise<string> {
const result = await this.#send({ $type: "getVersion" });
return returnResult(result, "string");
}

/**
* Sets the title of the webview window.
*/
Expand Down
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use serde::{Deserialize, Serialize};
use serde_json;
use tao::window::Fullscreen;

const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(JsonSchema, Deserialize, Debug)]
struct WebViewOptions {
/// Sets the title of the window.
Expand Down Expand Up @@ -75,7 +77,7 @@ enum Message {
#[serde(rename_all = "camelCase")]
#[serde(tag = "$type")]
enum Notification {
Started,
Started { version: String },
Closed,
}

Expand All @@ -84,6 +86,7 @@ enum Notification {
#[serde(rename_all = "camelCase")]
#[serde(tag = "$type")]
enum Request {
GetVersion { id: String },
Eval { id: String, js: String },
SetTitle { id: String, title: String },
GetTitle { id: String },
Expand Down Expand Up @@ -217,7 +220,9 @@ fn main() -> wry::Result<()> {
*control_flow = ControlFlow::Wait;

match event {
Event::NewEvents(StartCause::Init) => notify(Notification::Started),
Event::NewEvents(StartCause::Init) => notify(Notification::Started {
version: VERSION.into(),
}),
Event::UserEvent(event) => {
eprintln!("User event: {:?}", event);
}
Expand Down Expand Up @@ -271,6 +276,12 @@ fn main() -> wry::Result<()> {
id,
result: window.is_visible().into(),
}),
Request::GetVersion { id } => {
res(Response::Result {
id,
result: VERSION.to_string().into(),
});
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const WebViewOptions: z.ZodType<WebViewOptions> = z.intersection(
);

export type WebViewRequest =
| {
$type: "getVersion";
id: string;
}
| {
$type: "eval";
id: string;
Expand Down Expand Up @@ -69,6 +73,7 @@ export type WebViewRequest =
export const WebViewRequest: z.ZodType<WebViewRequest> = z.discriminatedUnion(
"$type",
[
z.object({ $type: z.literal("getVersion"), id: z.string() }),
z.object({ $type: z.literal("eval"), id: z.string(), js: z.string() }),
z.object({
$type: z.literal("setTitle"),
Expand Down Expand Up @@ -136,6 +141,7 @@ export type WebViewMessage =
data:
| {
$type: "started";
version: string;
}
| {
$type: "closed";
Expand Down Expand Up @@ -177,7 +183,7 @@ export const WebViewMessage: z.ZodType<WebViewMessage> = z.discriminatedUnion(
z.object({
$type: z.literal("notification"),
data: z.discriminatedUnion("$type", [
z.object({ $type: z.literal("started") }),
z.object({ $type: z.literal("started"), version: z.string() }),
z.object({ $type: z.literal("closed") }),
]),
}),
Expand Down

0 comments on commit 3bd1304

Please sign in to comment.