Skip to content

Commit

Permalink
Fix #5 by using an async rust op. bumped to v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Mar 21, 2020
1 parent 71c287f commit 0dc8fc2
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 56 deletions.
31 changes: 16 additions & 15 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deno_webview"
version = "0.2.2"
version = "0.3.0"
authors = ["Elias Sjögreen <[email protected]>"]
edition = "2018"

Expand All @@ -12,6 +12,7 @@ deno_core = "0.36.0"
webview-sys = "0.5.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
futures = "0.3.4"

[features]
default = ["webview-sys/edge"]
42 changes: 24 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,39 @@ Run the following with the `-A` flag enabled to get the example shown above:
import { WebView } from "https://deno.land/x/webview/mod.ts";

const webview1 = new WebView({
title: "Hello world",
url: `data:text/html,
title: "Multiple deno_webview example",
url: `data:text/html,
<html>
<body>
<h1>Hello from deno</h1>
<h1>1</h1>
</body>
</html>
`,
width: 300,
height: 300,
frameless: true
width: 800,
height: 600,
resizable: true,
debug: true,
frameless: false
});

const webview2 = new WebView({
title: "Hello world 2",
url: `data:text/html,
<html>
<body>
<h1>Hello from deno 2</h1>
</body>
</html>
`,
width: 300,
height: 300
title: "Multiple deno_webview example",
url: `data:text/html,
<html>
<body>
<h1>2</h1>
</body>
</html>
`,
width: 800,
height: 600,
resizable: true,
debug: true,
frameless: false
});

while (webview1.step() && webview2.step()) {}
await Promise.all([webview1.run(), webview2.run()]);

```

or just run the following in the terminal:
Expand All @@ -76,7 +82,7 @@ A `WebView` instance
height?: number; resizable?: boolean; debug?: boolean; frameless?: boolean;
}): WebView
- Creates a new `WebView` instance
- WebView.run(): Void
- WebView.run(): Promise<Void>
- Runs the event loop to completion
- WebView.step(): boolean
- Iterates the event loop and returns `false` if the the `WebView` has
Expand Down
2 changes: 1 addition & 1 deletion examples/local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebView } from "../mod.ts";

new WebView({
await new WebView({
title: "Local deno_webview example",
url: `data:text/html,
<html>
Expand Down
2 changes: 1 addition & 1 deletion examples/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ const webview2 = new WebView({
frameless: false
});

while (webview1.step() && webview2.step()) {}
await Promise.all([webview1.run(), webview2.run()]);
2 changes: 1 addition & 1 deletion examples/remote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebView } from "../mod.ts";

new WebView({
await new WebView({
title: "Remote deno_webview example",
url: `https://en.wikipedia.org/wiki/Main_Page`,
width: 800,
Expand Down
2 changes: 1 addition & 1 deletion examples/user_agent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebView } from "../mod.ts";

new WebView({
await new WebView({
title: "User agent deno_webview example",
url: `data:text/html,
<html>
Expand Down
32 changes: 16 additions & 16 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,29 @@ export class WebView {
}

/**
* Runs the event loop to completion
*/
public run() {
while (this.step()) {}
* Runs the event loop to completion
*/
public async run() {
await Plugin.WebViewRun({ id: this.id });
}

/**
* Iterates the event loop and returns `false` if the the `WebView` has been closed
*/
* Iterates the event loop and returns `false` if the the `WebView` has been closed
*/
public step(): boolean {
return Plugin.WebViewLoop({ id: this.id, blocking: 1 }).code === 0;
}

/**
* Exits the `WebView`
*/
* Exits the `WebView`
*/
public exit() {
Plugin.WebViewExit({ id: this.id });
}

/**
* Evaluates the provided js code in the `WebView`
*/
* Evaluates the provided js code in the `WebView`
*/
public eval(js: string) {
Plugin.WebViewEval({
id: this.id,
Expand All @@ -63,8 +63,8 @@ export class WebView {
}

/**
* Sets the color of the title bar
*/
* Sets the color of the title bar
*/
public setColor(color: { r: number; g: number; b: number; a: number }) {
Plugin.WebViewSetColor({
id: this.id,
Expand All @@ -73,8 +73,8 @@ export class WebView {
}

/**
* Sets the window title
*/
* Sets the window title
*/
public setTitle(title: string) {
Plugin.WebViewSetTitle({
id: this.id,
Expand All @@ -83,8 +83,8 @@ export class WebView {
}

/**
* Enables or disables fullscreen
*/
* Enables or disables fullscreen
*/
public setFullscreen(fullscreen: boolean) {
Plugin.WebViewSetFullscreen({
id: this.id,
Expand Down
35 changes: 33 additions & 2 deletions plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const MSHTML = Deno.env("MSHTML");

const pluginPath = DEV !== undefined
? DEV
: "https://github.com/eliassjogreen/deno_webview/releases/download/0.2.2";
: "https://github.com/eliassjogreen/deno_webview/releases/download/0.3.0";

const plugin = await prepare({
name: "deno_webview",
Expand Down Expand Up @@ -33,6 +33,23 @@ function jsonOpSync<P extends Object, R extends WebViewResponse<any>>(
return JSON.parse(decoder.decode(raw)) as R;
}

async function jsonOpAsync<P extends Object, R extends WebViewResponse<any>>(
op: Deno.PluginOp,
params: P
): Promise<R> {
return new Promise((resolve, reject) => {
op.setAsyncHandler((raw) => {
if (!raw) {
throw `Plugin op ${op} returned null`;
}

resolve(unwrapResponse(JSON.parse(decoder.decode(raw))));
});

op.dispatch(encoder.encode(JSON.stringify(params)));
});
}

function unwrapResponse<T, R extends WebViewResponse<T>>(response: R): T {
if (response.err) {
throw response.err;
Expand Down Expand Up @@ -110,6 +127,12 @@ export interface WebViewLoopResult {
code: number;
}

export interface WebViewRunParams {
id: number;
}

export interface WebViewRunResult {}

export function WebViewNew(params: WebViewNewParams): WebViewNewResult {
return unwrapResponse(jsonOpSync(plugin.ops.webview_new, params));
}
Expand Down Expand Up @@ -137,9 +160,17 @@ export function WebViewSetTitle(
export function WebViewSetFullscreen(
params: WebViewSetFullscreenParams
): WebViewSetFullscreenResult {
return unwrapResponse(jsonOpSync(plugin.ops.webview_set_fullscreen, params));
return unwrapResponse(
jsonOpSync(plugin.ops.webview_set_fullscreen, params)
);
}

export function WebViewLoop(params: WebViewLoopParams): WebViewLoopResult {
return unwrapResponse(jsonOpSync(plugin.ops.webview_loop, params));
}

export function WebViewRun(params: WebViewRunParams): Promise<
WebViewRunResult
> {
return jsonOpAsync(plugin.ops.webview_run, params);
}
Loading

0 comments on commit 0dc8fc2

Please sign in to comment.