diff options
author | Asher Gomez <ashersaupingomez@gmail.com> | 2024-01-22 10:20:59 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-22 00:20:59 +0100 |
commit | 983c745d4f385594638c42c107dca7d55afd0731 (patch) | |
tree | 2c184d26ea3344b82543117d25bb99e49ba876e9 /cli/tsc/dts/lib.deno.ns.d.ts | |
parent | 568337f3f43ef050cbbd95995f94e39663530ab4 (diff) |
chore: use `FsFile[Symbol.dispose]()` (#22007)
This change takes advantage of explicit resources management for
`FsFile` instances and tweaks documentation to encourage the use of it.
---------
Signed-off-by: Asher Gomez <ashersaupingomez@gmail.com>
Diffstat (limited to 'cli/tsc/dts/lib.deno.ns.d.ts')
-rw-r--r-- | cli/tsc/dts/lib.deno.ns.d.ts | 100 |
1 files changed, 53 insertions, 47 deletions
diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts index af427fda2..8a7abd0e1 100644 --- a/cli/tsc/dts/lib.deno.ns.d.ts +++ b/cli/tsc/dts/lib.deno.ns.d.ts @@ -1174,11 +1174,10 @@ declare namespace Deno { * * ```ts * Deno.bench("foo", async (t) => { - * const file = await Deno.open("data.txt"); + * using file = await Deno.open("data.txt"); * t.start(); * // some operation on `file`... * t.end(); - * file.close(); * }); * ``` */ @@ -1846,8 +1845,17 @@ declare namespace Deno { /** Open a file and resolve to an instance of {@linkcode Deno.FsFile}. The * file does not need to previously exist if using the `create` or `createNew` - * open options. It is the caller's responsibility to close the file when - * finished with it. + * open options. The caller may have the resulting file automatically closed + * by the runtime once it's out of scope by declaring the file variable with + * the `using` keyword. + * + * ```ts + * using file = await Deno.open("/foo/bar.txt", { read: true, write: true }); + * // Do work with file + * ``` + * + * Alternatively, the caller may manually close the resource when finished with + * it. * * ```ts * const file = await Deno.open("/foo/bar.txt", { read: true, write: true }); @@ -1868,8 +1876,17 @@ declare namespace Deno { /** Synchronously open a file and return an instance of * {@linkcode Deno.FsFile}. The file does not need to previously exist if - * using the `create` or `createNew` open options. It is the caller's - * responsibility to close the file when finished with it. + * using the `create` or `createNew` open options. The caller may have the + * resulting file automatically closed by the runtime once it's out of scope + * by declaring the file variable with the `using` keyword. + * + * ```ts + * using file = Deno.openSync("/foo/bar.txt", { read: true, write: true }); + * // Do work with file + * ``` + * + * Alternatively, the caller may manually close the resource when finished with + * it. * * ```ts * const file = Deno.openSync("/foo/bar.txt", { read: true, write: true }); @@ -2021,7 +2038,7 @@ declare namespace Deno { * * ```ts * // Given file.rid pointing to file with "Hello world", which is 11 bytes long: - * const file = await Deno.open( + * using file = await Deno.open( * "hello.txt", * { read: true, write: true, truncate: true, create: true }, * ); @@ -2033,14 +2050,13 @@ declare namespace Deno { * const buf = new Uint8Array(100); * await file.read(buf); * console.log(new TextDecoder().decode(buf)); // "world" - * file.close(); * ``` * * The seek modes work as follows: * * ```ts * // Given file.rid pointing to file with "Hello world", which is 11 bytes long: - * const file = await Deno.open( + * using file = await Deno.open( * "hello.txt", * { read: true, write: true, truncate: true, create: true }, * ); @@ -2052,7 +2068,6 @@ declare namespace Deno { * console.log(await Deno.seek(file.rid, 2, Deno.SeekMode.Current)); // "8" * // Seek backwards 2 bytes from the end of the file * console.log(await Deno.seek(file.rid, -2, Deno.SeekMode.End)); // "9" (i.e. 11-2) - * file.close(); * ``` * * @category I/O @@ -2068,7 +2083,7 @@ declare namespace Deno { * start) is returned. * * ```ts - * const file = Deno.openSync( + * using file = Deno.openSync( * "hello.txt", * { read: true, write: true, truncate: true, create: true }, * ); @@ -2080,14 +2095,13 @@ declare namespace Deno { * const buf = new Uint8Array(100); * file.readSync(buf); * console.log(new TextDecoder().decode(buf)); // "world" - * file.close(); * ``` * * The seek modes work as follows: * * ```ts * // Given file.rid pointing to file with "Hello world", which is 11 bytes long: - * const file = Deno.openSync( + * using file = Deno.openSync( * "hello.txt", * { read: true, write: true, truncate: true, create: true }, * ); @@ -2099,7 +2113,6 @@ declare namespace Deno { * console.log(Deno.seekSync(file.rid, 2, Deno.SeekMode.Current)); // "8" * // Seek backwards 2 bytes from the end of the file * console.log(Deno.seekSync(file.rid, -2, Deno.SeekMode.End)); // "9" (i.e. 11-2) - * file.close(); * ``` * * @category I/O @@ -2192,6 +2205,15 @@ declare namespace Deno { * Deno.close(file.rid); * ``` * + * It is recommended to define the variable with the `using` keyword so the + * runtime will automatically close the resource when it goes out of scope. + * Doing so negates the need to manually close the resource. + * + * ```ts + * using file = await Deno.open("my_file.txt"); + * // do work with "file" object + * ``` + * * @category I/O */ export function close(rid: number): void; @@ -2202,14 +2224,13 @@ declare namespace Deno { * recommended over using the discreet functions within the `Deno` namespace. * * ```ts - * const file = await Deno.open("/foo/bar.txt", { read: true }); + * using file = await Deno.open("/foo/bar.txt", { read: true }); * const fileInfo = await file.stat(); * if (fileInfo.isFile) { * const buf = new Uint8Array(100); * const numberOfBytesRead = await file.read(buf); // 11 bytes * const text = new TextDecoder().decode(buf); // "hello world" * } - * file.close(); * ``` * * @category File System @@ -2232,7 +2253,7 @@ declare namespace Deno { * based APIs. * * ```ts - * const file = await Deno.open("my_file.txt", { read: true }); + * using file = await Deno.open("my_file.txt", { read: true }); * const decoder = new TextDecoder(); * for await (const chunk of file.readable) { * console.log(decoder.decode(chunk)); @@ -2246,13 +2267,12 @@ declare namespace Deno { * * ```ts * const items = ["hello", "world"]; - * const file = await Deno.open("my_file.txt", { write: true }); + * using file = await Deno.open("my_file.txt", { write: true }); * const encoder = new TextEncoder(); * const writer = file.writable.getWriter(); * for (const item of items) { * await writer.write(encoder.encode(item)); * } - * file.close(); * ``` */ readonly writable: WritableStream<Uint8Array>; @@ -2270,9 +2290,8 @@ declare namespace Deno { * ```ts * const encoder = new TextEncoder(); * const data = encoder.encode("Hello world"); - * const file = await Deno.open("/foo/bar.txt", { write: true }); + * using file = await Deno.open("/foo/bar.txt", { write: true }); * const bytesWritten = await file.write(data); // 11 - * file.close(); * ``` * * @category I/O @@ -2288,9 +2307,8 @@ declare namespace Deno { * ```ts * const encoder = new TextEncoder(); * const data = encoder.encode("Hello world"); - * const file = Deno.openSync("/foo/bar.txt", { write: true }); + * using file = Deno.openSync("/foo/bar.txt", { write: true }); * const bytesWritten = file.writeSync(data); // 11 - * file.close(); * ``` */ writeSync(p: Uint8Array): number; @@ -2300,21 +2318,19 @@ declare namespace Deno { * ### Truncate the entire file * * ```ts - * const file = await Deno.open("my_file.txt", { write: true }); + * using file = await Deno.open("my_file.txt", { write: true }); * await file.truncate(); - * file.close(); * ``` * * ### Truncate part of the file * * ```ts * // if "my_file.txt" contains the text "hello world": - * const file = await Deno.open("my_file.txt", { write: true }); + * using file = await Deno.open("my_file.txt", { write: true }); * await file.truncate(7); * const buf = new Uint8Array(100); * await file.read(buf); * const text = new TextDecoder().decode(buf); // "hello w" - * file.close(); * ``` */ truncate(len?: number): Promise<void>; @@ -2325,21 +2341,19 @@ declare namespace Deno { * ### Truncate the entire file * * ```ts - * const file = Deno.openSync("my_file.txt", { write: true }); + * using file = Deno.openSync("my_file.txt", { write: true }); * file.truncateSync(); - * file.close(); * ``` * * ### Truncate part of the file * * ```ts * // if "my_file.txt" contains the text "hello world": - * const file = Deno.openSync("my_file.txt", { write: true }); + * using file = Deno.openSync("my_file.txt", { write: true }); * file.truncateSync(7); * const buf = new Uint8Array(100); * file.readSync(buf); * const text = new TextDecoder().decode(buf); // "hello w" - * file.close(); * ``` */ truncateSync(len?: number): void; @@ -2356,11 +2370,10 @@ declare namespace Deno { * * ```ts * // if "/foo/bar.txt" contains the text "hello world": - * const file = await Deno.open("/foo/bar.txt"); + * using file = await Deno.open("/foo/bar.txt"); * const buf = new Uint8Array(100); * const numberOfBytesRead = await file.read(buf); // 11 bytes * const text = new TextDecoder().decode(buf); // "hello world" - * file.close(); * ``` */ read(p: Uint8Array): Promise<number | null>; @@ -2377,11 +2390,10 @@ declare namespace Deno { * * ```ts * // if "/foo/bar.txt" contains the text "hello world": - * const file = Deno.openSync("/foo/bar.txt"); + * using file = Deno.openSync("/foo/bar.txt"); * const buf = new Uint8Array(100); * const numberOfBytesRead = file.readSync(buf); // 11 bytes * const text = new TextDecoder().decode(buf); // "hello world" - * file.close(); * ``` */ readSync(p: Uint8Array): number | null; @@ -2390,7 +2402,7 @@ declare namespace Deno { * * ```ts * // Given file pointing to file with "Hello world", which is 11 bytes long: - * const file = await Deno.open( + * using file = await Deno.open( * "hello.txt", * { read: true, write: true, truncate: true, create: true }, * ); @@ -2402,7 +2414,6 @@ declare namespace Deno { * const buf = new Uint8Array(100); * await file.read(buf); * console.log(new TextDecoder().decode(buf)); // "world" - * file.close(); * ``` * * The seek modes work as follows: @@ -2428,7 +2439,7 @@ declare namespace Deno { * The new position within the resource (bytes from the start) is returned. * * ```ts - * const file = Deno.openSync( + * using file = Deno.openSync( * "hello.txt", * { read: true, write: true, truncate: true, create: true }, * ); @@ -2440,14 +2451,13 @@ declare namespace Deno { * const buf = new Uint8Array(100); * file.readSync(buf); * console.log(new TextDecoder().decode(buf)); // "world" - * file.close(); * ``` * * The seek modes work as follows: * * ```ts * // Given file.rid pointing to file with "Hello world", which is 11 bytes long: - * const file = Deno.openSync( + * using file = Deno.openSync( * "hello.txt", * { read: true, write: true, truncate: true, create: true }, * ); @@ -2459,7 +2469,6 @@ declare namespace Deno { * console.log(file.seekSync(2, Deno.SeekMode.Current)); // "8" * // Seek backwards 2 bytes from the end of the file * console.log(file.seekSync(-2, Deno.SeekMode.End)); // "9" (i.e. 11-2) - * file.close(); * ``` */ seekSync(offset: number | bigint, whence: SeekMode): number; @@ -2468,10 +2477,9 @@ declare namespace Deno { * ```ts * import { assert } from "https://deno.land/std/assert/mod.ts"; * - * const file = await Deno.open("hello.txt"); + * using file = await Deno.open("hello.txt"); * const fileInfo = await file.stat(); * assert(fileInfo.isFile); - * file.close(); * ``` */ stat(): Promise<FileInfo>; @@ -2480,10 +2488,9 @@ declare namespace Deno { * ```ts * import { assert } from "https://deno.land/std/assert/mod.ts"; * - * const file = Deno.openSync("hello.txt") + * using file = Deno.openSync("hello.txt") * const fileInfo = file.statSync(); * assert(fileInfo.isFile); - * file.close(); * ``` */ statSync(): FileInfo; @@ -2491,9 +2498,8 @@ declare namespace Deno { * important to avoid leaking resources. * * ```ts - * const file = await Deno.open("my_file.txt"); + * using file = await Deno.open("my_file.txt"); * // do work with "file" object - * file.close(); * ``` */ close(): void; |