From 2b53602d3c798b68db4ce986546d3434b986096a Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Fri, 6 Aug 2021 20:21:29 +0300 Subject: feat: support AbortSignal in writeFile (#11568) --- runtime/js/40_write_file.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'runtime') diff --git a/runtime/js/40_write_file.js b/runtime/js/40_write_file.js index def9f6fee..6ced4e066 100644 --- a/runtime/js/40_write_file.js +++ b/runtime/js/40_write_file.js @@ -13,6 +13,9 @@ data, options = {}, ) { + if (options?.signal?.aborted) { + throw new DOMException("The write operation was aborted.", "AbortError"); + } if (options.create !== undefined) { const create = !!options.create; if (!create) { @@ -68,12 +71,17 @@ await chmod(path, options.mode); } + const signal = options?.signal ?? null; let nwritten = 0; - while (nwritten < data.length) { + while (!signal?.aborted && nwritten < data.length) { nwritten += await file.write(TypedArrayPrototypeSubarray(data, nwritten)); } file.close(); + + if (signal?.aborted) { + throw new DOMException("The write operation was aborted.", "AbortError"); + } } function writeTextFileSync( -- cgit v1.2.3