diff --git a/examples/external.ts b/examples/external.ts index db3f19b..6e7c7cd 100644 --- a/examples/external.ts +++ b/examples/external.ts @@ -17,3 +17,5 @@ const webview = new Webview( for await (const event of webview.iter()) { webview.setTitle(event); } + +// await webview.run((event) => webview.setTitle(event)); diff --git a/webview.ts b/webview.ts index 149e412..f8e36f7 100644 --- a/webview.ts +++ b/webview.ts @@ -116,12 +116,23 @@ export class Webview { * Iterates over the event loop until closed or terminated without * handling events */ - run(delta = 1000 / 60, block = false): Promise { + run( + callback?: (events: string) => unknown, + delta = 1000 / 60, + ): Promise { return new Promise((resolve) => { const interval = setInterval(() => { - const succ = this.loop(block); + const success = this.loop(); - if (!succ) { + if (callback !== undefined) { + const events = this.step(); + + for (const event of events) { + callback(event); + } + } + + if (!success) { resolve(); clearInterval(interval); } @@ -133,18 +144,18 @@ export class Webview { * Iterates over the event loop, yielding external invoke events as strings and * returning once closed or terminated */ - async *iter(delta = 1000 / 60, block = false): AsyncIterableIterator { + async *iter(delta = 1000 / 60): AsyncIterableIterator { let finished = false; - + const runner = debounce(async () => { - const succ = this.loop(block); - const evts = this.step(); + const success = this.loop(); + const events = this.step(); - if (!succ) { + if (!success) { finished = true; } - return evts; + return events; }, delta); while (!finished) {