Skip to content

Commit 5343a02

Browse files
bsmthJosh-Cena
authored andcommitted
chore: Change ints to 'number' for ProgressEvent properties (#39574)
* chore: Change ints to doubles for ProgressEvent properties * Apply suggestions from code review * Apply suggestions from code review * Update files/en-us/web/api/progressevent/loaded/index.md * feat: Clarify when progressevents use decimals * Apply suggestions from code review Co-authored-by: Joshua Chen <[email protected]> * feat: Clarify when progressevents use decimals --------- Co-authored-by: Joshua Chen <[email protected]>
1 parent 42c5198 commit 5343a02

File tree

4 files changed

+87
-26
lines changed

4 files changed

+87
-26
lines changed

files/en-us/web/api/progressevent/index.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ browser-compat: api.ProgressEvent
77

88
{{APIRef("XMLHttpRequest API")}}{{AvailableInWorkers}}
99

10-
The **`ProgressEvent`** interface represents events measuring progress of an underlying process, like an HTTP request (for an `XMLHttpRequest`, or the loading of the underlying resource of an {{HTMLElement("img")}}, {{HTMLElement("audio")}}, {{HTMLElement("video")}}, {{HTMLElement("style")}} or {{HTMLElement("link")}}).
10+
The **`ProgressEvent`** interface represents events that measure the progress of an underlying process, like an HTTP request (e.g., an `XMLHttpRequest`, or the loading of the underlying resource of an {{HTMLElement("img")}}, {{HTMLElement("audio")}}, {{HTMLElement("video")}}, {{HTMLElement("style")}} or {{HTMLElement("link")}}).
1111

1212
{{InheritanceDiagram}}
1313

@@ -21,18 +21,28 @@ The **`ProgressEvent`** interface represents events measuring progress of an und
2121
_Also inherits properties from its parent {{domxref("Event")}}_.
2222

2323
- {{domxref("ProgressEvent.lengthComputable")}} {{ReadOnlyInline}}
24-
- : A boolean flag indicating if the ratio between the size of the data already transmitted or processed (`loaded`), and the total size of the data (`total`), is calculable. In other words, it tells if the progress is measurable or not.
24+
- : A boolean flag indicating if the ratio between the size of the data already transmitted or processed (`loaded`), and the total size of the data (`total`), is calculable.
25+
In other words, it tells if the progress is measurable or not.
2526
- {{domxref("ProgressEvent.loaded")}} {{ReadOnlyInline}}
26-
- : A 64-bit unsigned integer indicating the size, in bytes, of the data already transmitted or processed. The ratio can be calculated by dividing `ProgressEvent.total` by the value of this property. When downloading a resource using HTTP, this only counts the body of the HTTP message, and doesn't include headers and other overhead. Note that for compressed requests of unknown total size, `loaded` might contain the size of the compressed, or decompressed, data, depending on the browser. As of 2024, it contains the size of the compressed data in Firefox, and the size of the uncompressed data in Chrome.
27+
- : A number indicating the size of the data already transmitted or processed.
28+
For a `ProgressEvent` dispatched by the browser in HTTP messages, the value refers to the size, in bytes, of the message body, excluding headers and other overhead.
29+
In compressed messages of unknown total size, `loaded` might refer to the size of the compressed or uncompressed data, depending on the browser.
30+
As of 2024, it contains the size of the compressed data in Firefox, and the uncompressed data in Chrome.
31+
In a `ProgressEvent` you create yourself, you can assign any numeric value to `loaded` that represents the amount of work completed relative to the `total` value.
2732
- {{domxref("ProgressEvent.total")}} {{ReadOnlyInline}}
28-
- : A 64-bit unsigned integer indicating the total size, in bytes, of the data being transmitted or processed. When downloading a resource using HTTP, this value is taken from the `Content-Length` response header. It only counts the body of the HTTP message, and doesn't include headers and other overhead.
33+
- : A number indicating the total size of the data being transmitted or processed.
34+
For `ProgressEvent`s dispatched by the browser in HTTP messages, the value refers to the size, in bytes, of a resource and is derived from the `Content-Length` header.
35+
In a `ProgressEvent` you create yourself, you may wish to normalize `total` to a value such as `100` or `1` if revealing the precise amount of bytes of a resource is a concern.
36+
If using `1` as a total, for example, then `loaded` would be a decimal value between `0` and `1`.
2937

3038
## Instance methods
3139

3240
_Inherits methods from its parent, {{domxref("Event")}}._
3341

3442
## Examples
3543

44+
### Showing the status of a request
45+
3646
The following example adds a `ProgressEvent` to a new {{domxref("XMLHttpRequest")}} and uses it to display the status of the request.
3747

3848
```js
@@ -51,6 +61,28 @@ client.onloadend = (pe) => {
5161
client.send();
5262
```
5363

64+
### Using fractions in a ProgressEvent
65+
66+
The total number of bytes of a resource may reveal too much information about a resource, so a number between 0 and 1 may be used in a {{domxref("ProgressEvent.ProgressEvent", "ProgressEvent()")}} instead:
67+
68+
```js
69+
function updateProgress(loaded, total) {
70+
const progressEvent = new ProgressEvent("progress", {
71+
lengthComputable: true,
72+
loaded,
73+
total,
74+
});
75+
76+
document.dispatchEvent(progressEvent);
77+
}
78+
79+
document.addEventListener("progress", (event) => {
80+
console.log(`Progress: ${event.loaded}/${event.total}`);
81+
});
82+
83+
updateProgress(0.123456, 1);
84+
```
85+
5486
## Specifications
5587

5688
{{Specifications}}

files/en-us/web/api/progressevent/loaded/index.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ browser-compat: api.ProgressEvent.loaded
88

99
{{APIRef("XMLHttpRequest API")}}{{AvailableInWorkers}}
1010

11-
The **`ProgressEvent.loaded`** read-only property is a 64-bit unsigned integer
12-
indicating the size, in bytes, of the data already transmitted or processed. The ratio can be calculated by dividing the value of this property by `ProgressEvent.total`.
13-
When downloading a resource using HTTP, this only counts the body of the HTTP message, and doesn't include headers and other overhead.
11+
The **`ProgressEvent.loaded`** read-only property is a number indicating the size of the data already transmitted or processed.
12+
The progress ratio can be calculated by dividing the value of this property by {{domxref("ProgressEvent.total")}}.
1413

15-
Note that for compressed requests of unknown total size, `loaded` might contain the size of the compressed, or decompressed, data, depending on the browser. As of 2024, it contains the size of the compressed data in Firefox, and the size of the uncompressed data in Chrome.
14+
For `ProgressEvent`s dispatched by the browser in HTTP messages, the value refers to the amount of bytes of a resource that are completed, and is derived from the `Content-Length` header.
15+
For compressed requests of unknown total size, `loaded` might contain the size of the compressed or decompressed data, depending on the browser.
16+
As of 2024, it contains the size of the compressed data in Firefox, and the size of the uncompressed data in Chrome.
17+
18+
In a `ProgressEvent` you create yourself, you can assign any numeric value to `loaded` that represents the amount of work completed relative to the `total` value.
1619

1720
## Value
1821

files/en-us/web/api/progressevent/progressevent/index.md

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,29 @@ new ProgressEvent(type, options)
2525
- `options` {{optional_inline}}
2626
- : An object that, _in addition of the properties defined in {{domxref("Event/Event", "Event()")}}_, can have the following properties:
2727
- `lengthComputable` {{optional_inline}}
28-
- : A boolean value indicating if the total work to be done, and the
29-
amount of work already done, by the underlying process is calculable. In other words,
30-
it tells if the progress is measurable or not. It defaults to `false`.
28+
- : A boolean value indicating if the total work to be done, and the amount of work already done, by the underlying process is calculable.
29+
In other words, it tells if the progress is measurable or not.
30+
It defaults to `false`.
3131
- `loaded` {{optional_inline}}
32-
- : A number representing the amount of work already
33-
performed by the underlying process. The ratio of work done can be calculated with the
34-
property and `ProgressEvent.total`. When downloading a resource using HTTP,
35-
this only represent the part of the content itself, not headers and other overhead. It
36-
defaults to `0`.
32+
- : A number representing the amount of work already performed by the underlying process.
33+
For a `ProgressEvent` dispatched by the browser in HTTP messages, the value refers to the size, in bytes, of the message body, excluding headers and other overhead.
34+
In a `ProgressEvent` you create yourself, you can assign any numeric value to `loaded` that represents the amount of work completed relative to the `total` value.
35+
It defaults to `0`.
3736
- `total` {{optional_inline}}
38-
- : A number representing the total amount of work that the
39-
underlying process is in the progress of performing. When downloading a resource using
40-
HTTP, this only represent the content itself, not headers and other overhead. It
41-
defaults to `0`.
37+
- : A number indicating the total size of the data being transmitted or processed.
38+
For `ProgressEvent`s dispatched by the browser in HTTP messages, the value refers to the size, in bytes, of a resource and is derived from the `Content-Length` response header.
39+
In a `ProgressEvent` you create yourself, you may wish to normalize `total` to a value such as `100` or `1` if revealing the precise amount of bytes of a resource is a concern.
40+
If using `1` as a total, for example, then `loaded` should be a decimal value between `0` and `1`.
41+
It defaults to `0`.
4242

4343
### Return value
4444

4545
A new {{domxref("ProgressEvent")}} object.
4646

4747
## Example
4848

49+
### File upload
50+
4951
The example demonstrates how a `ProgressEvent` is built using a constructor. This is particularly useful for tracking the progress of processes like file uploads, downloads, or any long-running tasks.
5052

5153
```js
@@ -66,6 +68,28 @@ document.addEventListener("progress", (event) => {
6668
updateProgress(50, 100);
6769
```
6870

71+
### Using fractions in a ProgressEvent
72+
73+
The total number of bytes of a resource may reveal too much information about a download, so a number between 0 and 1 may be used instead:
74+
75+
```js
76+
function updateProgress(loaded, total) {
77+
const progressEvent = new ProgressEvent("progress", {
78+
lengthComputable: true,
79+
loaded,
80+
total,
81+
});
82+
83+
document.dispatchEvent(progressEvent);
84+
}
85+
86+
document.addEventListener("progress", (event) => {
87+
console.log(`Progress: ${event.loaded}/${event.total}`);
88+
});
89+
90+
updateProgress(0.123456, 1);
91+
```
92+
6993
## Specifications
7094

7195
{{Specifications}}

files/en-us/web/api/progressevent/total/index.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ browser-compat: api.ProgressEvent.total
88

99
{{APIRef("XMLHttpRequest API")}}{{AvailableInWorkers}}
1010

11-
The **`ProgressEvent.total`** read-only property is a 64-bit unsigned integer
12-
indicating the total size, in bytes, of the data being transmitted or processed.
11+
The **`ProgressEvent.total`** read-only property is a number indicating the total size of the data being transmitted or processed.
1312

14-
When downloading a resource using HTTP, this value is taken from the `Content-Length` response header. It only counts the body of the HTTP message, and doesn't include headers and other overhead.
13+
For `ProgressEvent`s dispatched by the browser, the value refers to the size, in bytes, of a resource and is derived from the `Content-Length` response header.
1514

16-
If the event's {{domxref("ProgressEvent.lengthComputable", "lengthComputable")}}
17-
property is `false`, this value is meaningless and should be ignored.
15+
In a `ProgressEvent` you create yourself, this may also be the total bytes of a resource, although this can be any number.
16+
For example, you may wish to normalize `total` to a value such as `100` or `1` if revealing the precise amount of bytes of a resource is a concern.
17+
If using `1` as a total, then {{domxref("ProgressEvent.loaded")}} would be a decimal value between `0` and `1`.
18+
19+
If the event's {{domxref("ProgressEvent.lengthComputable", "lengthComputable")}} property is `false`, this value is meaningless and should be ignored.
1820

1921
## Value
2022

21-
An integer.
23+
A number.
2224

2325
## Specifications
2426

0 commit comments

Comments
 (0)