From d19defccc477995793d6ed4625e7f4c0b25e9562 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Feb 2022 17:35:01 +0530 Subject: [PATCH] fix(example): launch webview from main thread (#102) --- Cargo.toml | 1 - Makefile | 2 +- examples/local.ts | 3 ++- examples/ssr/main.ts | 13 +++++++++++++ examples/ssr/worker.ts | 12 ------------ examples/ssr/{ssr.jsx => worker.tsx} | 11 +---------- src/webview.ts | 4 ++-- 7 files changed, 19 insertions(+), 27 deletions(-) create mode 100644 examples/ssr/main.ts delete mode 100644 examples/ssr/worker.ts rename examples/ssr/{ssr.jsx => worker.tsx} (68%) diff --git a/Cargo.toml b/Cargo.toml index 18d1619..40f8d74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,3 @@ crate-type = ["cdylib"] [dependencies] webview-official-sys = "0.1.2" once_cell = "1.9.0" - diff --git a/Makefile b/Makefile index da53a85..d9b6c37 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ example: --unstable \ --no-check \ --config examples/ssr/tsconfig.json \ - examples/ssr/ssr.jsx + examples/ssr/main.ts fmt: cargo fmt diff --git a/examples/local.ts b/examples/local.ts index 405577e..aa55d3e 100644 --- a/examples/local.ts +++ b/examples/local.ts @@ -9,5 +9,6 @@ const html = ` `; const webview = new Webview(); + webview.navigate(`data:text/html,${encodeURIComponent(html)}`); -webview.run(); +webview.run(); \ No newline at end of file diff --git a/examples/ssr/main.ts b/examples/ssr/main.ts new file mode 100644 index 0000000..6ea7f33 --- /dev/null +++ b/examples/ssr/main.ts @@ -0,0 +1,13 @@ +import { dirname, join } from "https://deno.land/std@0.114.0/path/mod.ts"; +import { Webview } from "../../mod.ts"; + +new Worker( + join(dirname(import.meta.url), "worker.tsx"), + { type: "module", deno: true }, +) + +const webview = new Webview(); +webview.navigate("http://localhost:8000/"); + +console.log("[runner] worker started"); +webview.run(); \ No newline at end of file diff --git a/examples/ssr/worker.ts b/examples/ssr/worker.ts deleted file mode 100644 index 313c4a4..0000000 --- a/examples/ssr/worker.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Webview } from "../../mod.ts"; -const webview = new Webview(); - -webview.navigate("http://localhost:8000/"); -onmessage = (event) => { - if (event.data === "close") { - webview.terminate(); - self.close(); - } -}; - -webview.run(); diff --git a/examples/ssr/ssr.jsx b/examples/ssr/worker.tsx similarity index 68% rename from examples/ssr/ssr.jsx rename to examples/ssr/worker.tsx index d2e9adb..533b4e0 100644 --- a/examples/ssr/ssr.jsx +++ b/examples/ssr/worker.tsx @@ -1,4 +1,3 @@ -import { dirname, join } from "https://deno.land/std@0.114.0/path/mod.ts"; import { serve } from "https://deno.land/std@0.114.0/http/server.ts"; import { h, ssr, tw } from "https://crux.land/nanossr@0.0.1"; @@ -18,12 +17,4 @@ const server = serve((req) => { }, { port: 8000 }); console.log("[runner] Listening on http://localhost:8000"); -console.log("[runner] worker started"); - -setTimeout(() => - new Worker( - join(dirname(import.meta.url), "worker.ts"), - { type: "module", deno: true }, - ), 1000); - -await server; +await server; \ No newline at end of file diff --git a/src/webview.ts b/src/webview.ts index b685751..245b03e 100644 --- a/src/webview.ts +++ b/src/webview.ts @@ -45,10 +45,10 @@ export class Webview { this.#url = url; } - async run() { + run() { if (this.#url == null) throw new TypeError("URL not initialized"); sys.symbols.deno_webview_navigate(this.#handle, encode(this.#url)); - await sys.symbols.deno_webview_run(this.#handle); + sys.symbols.deno_webview_run(this.#handle); } set title(title: string) {