Skip to content

Commit 2da8180

Browse files
authored
Add a reason property to help the users track why the progress event is being fired (#4647)
1 parent f290b91 commit 2da8180

File tree

7 files changed

+33
-18
lines changed

7 files changed

+33
-18
lines changed

packages/model-viewer/src/features/ar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ configuration or device capabilities');
415415
}
416416

417417
async prepareUSDZ(): Promise<string> {
418-
const updateSourceProgress = this[$progressTracker].beginActivity();
418+
const updateSourceProgress = this[$progressTracker].beginActivity('usdz-conversion');
419419

420420
await this[$triggerLoad]();
421421

packages/model-viewer/src/features/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export const EnvironmentMixin = <T extends Constructor<ModelViewerElementBase>>(
125125
return;
126126
}
127127

128-
const updateEnvProgress = this[$progressTracker].beginActivity();
128+
const updateEnvProgress = this[$progressTracker].beginActivity('environment-update');
129129

130130
try {
131131
const {environmentMap, skybox} =

packages/model-viewer/src/features/scene-graph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export const SceneGraphMixin = <T extends Constructor<ModelViewerElementBase>>(
170170
super.updated(changedProperties);
171171

172172
if (changedProperties.has('variantName')) {
173-
const updateVariantProgress = this[$progressTracker].beginActivity();
173+
const updateVariantProgress = this[$progressTracker].beginActivity('variant-update');
174174
updateVariantProgress(0.1);
175175
const model = this[$model];
176176
const {variantName} = this;

packages/model-viewer/src/model-viewer-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ export default class ModelViewerElementBase extends ReactiveElement {
613613
// throw exceptions and/or behave in unexpected ways:
614614
scene.stopAnimation();
615615

616-
const updateSourceProgress = this[$progressTracker].beginActivity();
616+
const updateSourceProgress = this[$progressTracker].beginActivity('model-load');
617617
const source = this.src;
618618
try {
619619
const srcUpdated = scene.setSource(

packages/model-viewer/src/test/utilities/progress-tracker-spec.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {waitForEvent} from '../../utilities.js';
1919
import {Activity, ProgressDetails, ProgressTracker} from '../../utilities/progress-tracker.js';
2020

2121
suite('ProgressTracker', () => {
22+
const progressReason = 'progress-test';
23+
2224
let progressTracker: ProgressTracker;
2325
setup(() => {
2426
progressTracker = new ProgressTracker();
@@ -28,11 +30,23 @@ suite('ProgressTracker', () => {
2830
expect(progressTracker.ongoingActivityCount).to.be.equal(0);
2931
});
3032

33+
test('event includes the reason for the progress', async () => {
34+
const activity = progressTracker.beginActivity(progressReason);
35+
36+
const progressEventDispatches =
37+
waitForEvent<CustomEvent<ProgressDetails>>(
38+
progressTracker, 'progress');
39+
activity(0.5);
40+
const event = await progressEventDispatches;
41+
42+
expect(event.detail.reason).to.be.equal(progressReason);
43+
});
44+
3145
suite('an activity', () => {
3246
let firstActivity: Activity;
3347

3448
setup(() => {
35-
firstActivity = progressTracker.beginActivity();
49+
firstActivity = progressTracker.beginActivity(progressReason);
3650
});
3751

3852
test('increases the ongoing activities count', () => {
@@ -73,12 +87,12 @@ suite('ProgressTracker', () => {
7387
});
7488

7589
test('is added to the current stack of activities', () => {
76-
progressTracker.beginActivity();
90+
progressTracker.beginActivity(progressReason);
7791
expect(progressTracker.ongoingActivityCount).to.be.equal(2);
7892
});
7993

8094
test('defers marking all activities completed', () => {
81-
progressTracker.beginActivity();
95+
progressTracker.beginActivity(progressReason);
8296
firstActivity(1.0);
8397
expect(progressTracker.ongoingActivityCount).to.be.equal(2);
8498
});
@@ -107,7 +121,7 @@ suite('ProgressTracker', () => {
107121
let secondActivity: Activity;
108122

109123
setup(() => {
110-
secondActivity = progressTracker.beginActivity();
124+
secondActivity = progressTracker.beginActivity(progressReason);
111125
});
112126

113127
test('increases the ongoing activity count', () => {
@@ -151,4 +165,4 @@ suite('ProgressTracker', () => {
151165
});
152166
});
153167
});
154-
});
168+
});

packages/model-viewer/src/utilities/progress-tracker.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type Activity = (progress: number) => number;
3838
*/
3939
export interface ProgressDetails {
4040
totalProgress: number;
41+
reason: string;
4142
}
4243

4344
/**
@@ -50,7 +51,7 @@ export interface ProgressDetails {
5051
*
5152
* The value of totalProgress is a number that progresses from 0 to 1. The
5253
* ProgressTracker allows for the lazy accumulation of tracked actions, so the
53-
* total progress represents a abstract, non-absolute progress towards the
54+
* total progress represents an abstract, non-absolute progress towards the
5455
* completion of all currently tracked events.
5556
*
5657
* When all currently tracked activities are finished, the ProgressTracker
@@ -86,15 +87,15 @@ export class ProgressTracker extends EventTarget {
8687
* progress is reported than the previously reported progress, it will be
8788
* ignored.
8889
*/
89-
beginActivity(): Activity {
90+
beginActivity(reason: string): Activity {
9091
const activity: OngoingActivity = {progress: 0, completed: false};
9192

9293
this.ongoingActivities.add(activity);
9394

9495
if (this.ongoingActivityCount === 1) {
9596
// Announce the first progress event (which should always be 0 / 1
9697
// total progress):
97-
this.announceTotalProgress(activity, 0);
98+
this.announceTotalProgress(activity, 0, reason);
9899
}
99100

100101
return (progress: number): number => {
@@ -103,15 +104,15 @@ export class ProgressTracker extends EventTarget {
103104
nextProgress = Math.max(clamp(progress, 0, 1), activity.progress);
104105

105106
if (nextProgress !== activity.progress) {
106-
this.announceTotalProgress(activity, nextProgress);
107+
this.announceTotalProgress(activity, nextProgress, reason);
107108
}
108109

109110
return activity.progress;
110111
};
111112
}
112113

113114
private announceTotalProgress(
114-
updatedActivity: OngoingActivity, nextProgress: number) {
115+
updatedActivity: OngoingActivity, nextProgress: number, reason: string) {
115116
let progressLeft = 0;
116117
let completedActivities = 0;
117118

@@ -122,7 +123,7 @@ export class ProgressTracker extends EventTarget {
122123
const {progress} = activity;
123124
progressLeft += 1.0 - progress;
124125

125-
if (activity.completed === true) {
126+
if (activity.completed) {
126127
completedActivities++;
127128
}
128129
}
@@ -140,7 +141,7 @@ export class ProgressTracker extends EventTarget {
140141
this.totalProgress;
141142

142143
this.dispatchEvent(new CustomEvent<ProgressDetails>(
143-
'progress', {detail: {totalProgress}}));
144+
'progress', {detail: {totalProgress, reason }}));
144145

145146
if (completedActivities === this.ongoingActivityCount) {
146147
this.totalProgress = 0.0;

packages/modelviewer.dev/data/docs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
{
249249
"name": "progress",
250250
"htmlName": "progress",
251-
"description": "This event fires while a model, environment, and/or skybox is loading, during both download and processing. The progress of all of these concurrent tasks will be given by a value between 0 and 1: event.detail.totalProgress."
251+
"description": "This event fires while a model, environment, skybox, variant, and/or a usdz conversion is loading, during both download and processing. The progress of all of these concurrent tasks will be given by a value between 0 and 1: event.detail.totalProgress. It also provides the reason why the event is fired ('model-load', 'environment-update', 'variant-update', 'usdz-conversion'): event.detail.reason."
252252
},
253253
{
254254
"name": "render-scale",
@@ -1147,4 +1147,4 @@
11471147
}
11481148
]
11491149
}
1150-
]
1150+
]

0 commit comments

Comments
 (0)