From 4f04e0429db64f5f3e24abcb6dfd54b7edb6ae27 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Wed, 22 Jan 2025 13:51:45 +0900 Subject: [PATCH] Change progress to be between 0 and 1 Closes #15. Depends on https://github.com/whatwg/xhr/pull/394. --- README.md | 12 ++++-- index.bs | 116 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 114 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 2b3fcb7..542c193 100644 --- a/README.md +++ b/README.md @@ -213,27 +213,31 @@ if (supportsOurUseCase !== "no") { ### Download progress -In cases where using the API is only possible after a download, you can monitor the download progress (e.g. in order to show your users a progress bar) using code such as the following: +For cases where using the API is only possible after a download, you can monitor the download progress (e.g. in order to show your users a progress bar) using code such as the following: ```js const writer = await ai.writer.create({ ...otherOptions, monitor(m) { m.addEventListener("downloadprogress", e => { - console.log(`Downloaded ${e.loaded} of ${e.total} bytes.`); + console.log(`Downloaded ${e.loaded * 100}%`); }); } ); ``` -If the download fails, then `downloadprogress` events will stop being emitted, and the promise returned by `create()` will be rejected with a `"NetworkError"` `DOMException`. +If the download fails, then `downloadprogress` events will stop being fired, and the promise returned by `create()` will be rejected with a `"NetworkError"` `DOMException`. Note that in the case that multiple entities are downloaded (e.g., a base model plus a [LoRA fine-tuning](https://arxiv.org/abs/2106.09685) for writing, or for the particular style requested) web developers do not get the ability to monitor the individual downloads. All of them are bundled into the overall `downloadprogress` events, and the `create()` promise is not fulfilled until all downloads and loads are successful. +The event is a [`ProgressEvent`](https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent) whose `loaded` property is between 0 and 1, and whose `total` property is always 1. (The exact number of total or downloaded bytes are not exposed; see the discussion in [issue #15](https://github.com/webmachinelearning/writing-assistance-apis/issues/15).) + +At least two events, with `e.loaded === 0` and `e.loaded === 1`, will always be fired. This is true even if creating the model doesn't require any downloading. +
What's up with this pattern? -This pattern is a little involved. Several alternatives have been considered. However, asking around the web standards community it seemed like this one was best, as it allows using standard event handlers and `ProgressEvent`s, and also ensures that once the promise is settled, the translator or language detector object is completely ready to use. +This pattern is a little involved. Several alternatives have been considered. However, asking around the web standards community it seemed like this one was best, as it allows using standard event handlers and `ProgressEvent`s, and also ensures that once the promise is settled, the returned object is completely ready to use. It is also nicely future-extensible by adding more events and properties to the `m` object. diff --git a/index.bs b/index.bs index 64ede36..aa7964a 100644 --- a/index.bs +++ b/index.bs @@ -29,6 +29,9 @@ urlPrefix: https://tc39.es/ecma402/; spec: ECMA-402 text: LookupMatchingLocaleByBestFit; url: sec-lookupmatchinglocalebybestfit text: IsStructurallyValidLanguageTag; url: sec-isstructurallyvalidlanguagetag text: CanonicalizeUnicodeLocaleId; url: sec-canonicalizeunicodelocaleid +urlPrefix: https://tc39.es/ecma262/; spec: ECMA-262 + type: abstract-op + text: floor; url: eqn-floor