From b61347b2552cb7159ee3e82c5e9ca5a76e73c3f8 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Fri, 24 Jul 2020 02:33:52 +0100 Subject: fix(cli/js/ops/fs_events): Ignore polling errors caused by return() (#6785) --- cli/rt/40_fs_events.js | 16 ++++++++++++---- cli/tests/unit/fs_events_test.ts | 20 +++++++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) (limited to 'cli') diff --git a/cli/rt/40_fs_events.js b/cli/rt/40_fs_events.js index ad1fd678f..b9121c155 100644 --- a/cli/rt/40_fs_events.js +++ b/cli/rt/40_fs_events.js @@ -2,6 +2,7 @@ ((window) => { const { sendSync, sendAsync } = window.__bootstrap.dispatchJson; + const { errors } = window.__bootstrap.errors; const { close } = window.__bootstrap.resources; class FsWatcher { @@ -16,10 +17,17 @@ return this.#rid; } - next() { - return sendAsync("op_fs_events_poll", { - rid: this.rid, - }); + async next() { + try { + return await sendAsync("op_fs_events_poll", { + rid: this.rid, + }); + } catch (error) { + if (error instanceof errors.BadResource) { + return { value: undefined, done: true }; + } + throw error; + } } return(value) { diff --git a/cli/tests/unit/fs_events_test.ts b/cli/tests/unit/fs_events_test.ts index f2dd59d79..d73001187 100644 --- a/cli/tests/unit/fs_events_test.ts +++ b/cli/tests/unit/fs_events_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { unitTest, assert, assertThrows } from "./test_util.ts"; +import { unitTest, assert, assertEquals, assertThrows } from "./test_util.ts"; // TODO(ry) Add more tests to specify format. @@ -60,3 +60,21 @@ unitTest( assert(events[1].paths[0].includes(testDir)); }, ); + +unitTest( + { perms: { read: true, write: true } }, + async function watchFsReturn(): Promise { + const testDir = await Deno.makeTempDir(); + const iter = Deno.watchFs(testDir); + + // Asynchronously loop events. + const eventsPromise = getTwoEvents(iter); + + // Close the watcher. + await iter.return!(); + + // Expect zero events. + const events = await eventsPromise; + assertEquals(events, []); + }, +); -- cgit v1.2.3