Skip to content

Commit e92ac62

Browse files
committed
[uploadChangeset] add onProgress callback
1 parent bb94f6f commit e92ac62

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- [uploadChangeset] add an `onProgress` callback, so that apps can show a progress bar while uploading
1011
- [uploadChangeset] compress changesets with gzip before uploading (if JS's [`CompressionStream`](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream) is available natively)
1112

1213
## 3.2.0 (2025-07-27)

examples/uploadChangeset.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,18 @@ await uploadChangeset(changesetTags, diff, {
115115
},
116116
});
117117
```
118+
119+
### onProgress
120+
121+
`onProgress` is a callback function which is called whenever the upload progress changes.
122+
It is called with an object parameter:
123+
124+
```js
125+
{
126+
phase: "upload",
127+
step: 20,
128+
total: 35,
129+
}
130+
```
131+
132+
`step` is a number from `0` to `total` which could be used to render a progress bar.

src/api/changesets/uploadChangeset.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export interface UploadChunkInfo {
1717
changesetTotal: number;
1818
}
1919

20+
export type UploadPhase = "upload" | "merge_conflicts";
21+
2022
/** @internal */
2123
export function compress(input: string) {
2224
// check if it's supported
@@ -37,6 +39,16 @@ export interface UploadOptions {
3739
*/
3840
onChunk?(info: UploadChunkInfo): Tags;
3941

42+
/**
43+
* Optional, if you want status updates during the upload process,
44+
* this callback is invoked whenever the progress updates.
45+
*/
46+
onProgress?(progress: {
47+
phase: UploadPhase;
48+
step: number;
49+
total: number;
50+
}): void;
51+
4052
/** by default, uploads are compressed with gzip. set to `false` to disable */
4153
disableCompression?: boolean;
4254
}
@@ -52,6 +64,7 @@ export async function uploadChangeset(
5264
): Promise<number> {
5365
const {
5466
onChunk,
67+
onProgress,
5568
disableCompression,
5669
//
5770
...fetchOptions
@@ -85,6 +98,8 @@ export async function uploadChangeset(
8598
// if the user didn't include a `created_by` tag, we'll add one.
8699
tagsForChunk["created_by"] ||= `osm-api-js ${version}`;
87100

101+
onProgress?.({ phase: "upload", step: index + 1, total: chunks.length });
102+
88103
const csId = +(await osmFetch<string>("/0.6/changeset/create", undefined, {
89104
...fetchOptions,
90105
method: "PUT",

0 commit comments

Comments
 (0)