diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-02-07 20:22:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-07 20:22:46 +0100 |
commit | b4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch) | |
tree | 3d008912affe8550692183bd2697a386db5e3c79 /runtime/js/40_fs_events.js | |
parent | 65500f36e870b4ada3996b06aa287e30177d21a3 (diff) |
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as
ES modules.
`__bootstrap`has been mostly replaced with static imports in form in
`internal:[path to file from repo root]`.
To specify if files are ESM, an `esm` method has been added to
`Extension`, similar to the `js` method.
A new ModuleLoader called `InternalModuleLoader` has been added to
enable the loading of internal specifiers, which is used in all
situations except when a snapshot is only loaded, and not a new one is
created from it.
---------
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'runtime/js/40_fs_events.js')
-rw-r--r-- | runtime/js/40_fs_events.js | 109 |
1 files changed, 51 insertions, 58 deletions
diff --git a/runtime/js/40_fs_events.js b/runtime/js/40_fs_events.js index b410e2299..4c2f5fc9a 100644 --- a/runtime/js/40_fs_events.js +++ b/runtime/js/40_fs_events.js @@ -1,70 +1,63 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -"use strict"; -((window) => { - const core = window.Deno.core; - const ops = core.ops; - const { BadResourcePrototype, InterruptedPrototype } = core; - const { - ArrayIsArray, - ObjectPrototypeIsPrototypeOf, - PromiseResolve, - SymbolAsyncIterator, - } = window.__bootstrap.primordials; - class FsWatcher { - #rid = 0; - - constructor(paths, options) { - const { recursive } = options; - this.#rid = ops.op_fs_events_open({ recursive, paths }); - } +const core = globalThis.Deno.core; +const { BadResourcePrototype, InterruptedPrototype, ops } = core; +const primordials = globalThis.__bootstrap.primordials; +const { + ArrayIsArray, + ObjectPrototypeIsPrototypeOf, + PromiseResolve, + SymbolAsyncIterator, +} = primordials; +class FsWatcher { + #rid = 0; + + constructor(paths, options) { + const { recursive } = options; + this.#rid = ops.op_fs_events_open({ recursive, paths }); + } - get rid() { - return this.#rid; - } + get rid() { + return this.#rid; + } - async next() { - try { - const value = await core.opAsync("op_fs_events_poll", this.rid); - return value - ? { value, done: false } - : { value: undefined, done: true }; - } catch (error) { - if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error)) { - return { value: undefined, done: true }; - } else if ( - ObjectPrototypeIsPrototypeOf(InterruptedPrototype, error) - ) { - return { value: undefined, done: true }; - } - throw error; + async next() { + try { + const value = await core.opAsync("op_fs_events_poll", this.rid); + return value ? { value, done: false } : { value: undefined, done: true }; + } catch (error) { + if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error)) { + return { value: undefined, done: true }; + } else if ( + ObjectPrototypeIsPrototypeOf(InterruptedPrototype, error) + ) { + return { value: undefined, done: true }; } + throw error; } + } - // TODO(kt3k): This is deprecated. Will be removed in v2.0. - // See https://github.com/denoland/deno/issues/10577 for details - return(value) { - core.close(this.rid); - return PromiseResolve({ value, done: true }); - } - - close() { - core.close(this.rid); - } + // TODO(kt3k): This is deprecated. Will be removed in v2.0. + // See https://github.com/denoland/deno/issues/10577 for details + return(value) { + core.close(this.rid); + return PromiseResolve({ value, done: true }); + } - [SymbolAsyncIterator]() { - return this; - } + close() { + core.close(this.rid); } - function watchFs( - paths, - options = { recursive: true }, - ) { - return new FsWatcher(ArrayIsArray(paths) ? paths : [paths], options); + [SymbolAsyncIterator]() { + return this; } +} + +function watchFs( + paths, + options = { recursive: true }, +) { + return new FsWatcher(ArrayIsArray(paths) ? paths : [paths], options); +} - window.__bootstrap.fsEvents = { - watchFs, - }; -})(this); +export { watchFs }; |