Skip to content

Commit

Permalink
feat(#59): event callback for webview.run
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Dec 4, 2020
1 parent 17483db commit 907e072
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions examples/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
29 changes: 20 additions & 9 deletions webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
run(
callback?: (events: string) => unknown,
delta = 1000 / 60,
): Promise<void> {
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);
}
Expand All @@ -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<string> {
async *iter(delta = 1000 / 60): AsyncIterableIterator<string> {
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) {
Expand Down

0 comments on commit 907e072

Please sign in to comment.