Skip to content

Commit bd17553

Browse files
committed
Change progress to be between 0 and 1
See webmachinelearning/writing-assistance-apis#15.
1 parent dce6317 commit bd17553

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,28 @@ async function translateUnknownCustomerInput(textToTranslate, targetLanguage) {
140140

141141
### Download progress
142142

143-
In cases where translation or language detection 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:
143+
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:
144144

145145
```js
146146
const translator = await ai.translator.create({
147147
sourceLanguage,
148148
targetLanguage,
149149
monitor(m) {
150150
m.addEventListener("downloadprogress", e => {
151-
console.log(`Downloaded ${e.loaded} of ${e.total} bytes.`);
151+
console.log(`Downloaded ${e.loaded * 100}%`);
152152
});
153153
}
154154
});
155155
```
156156

157157
If the download fails, then `downloadprogress` events will stop being emitted, and the promise returned by `create()` will be rejected with a "`NetworkError`" `DOMException`.
158158

159+
Note that in the case that multiple entities are downloaded (e.g., an `en``ja` language pack and a `en``ko` language pack to support the `ja``ko` use case) 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.
160+
161+
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 [webmachinelearning/writing-assistance-apis issue #15](https://github.com/webmachinelearning/writing-assistance-apis/issues/15).)
162+
163+
At least two events, with `e.loaded === 0` and `e.loaded === 1`, will always be fired. This is true even if creating the translator or language detector doesn't require any downloading.
164+
159165
<details>
160166
<summary>What's up with this pattern?</summary>
161167

0 commit comments

Comments
 (0)