Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loading indicator visible and isActive true while endpoint fetch #9593

Merged
merged 2 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export class ConnectClient {
async(context: MiddlewareContext): Promise<Response> => {
this.loading(true);
try {
return fetch(context.request);
return await fetch(context.request);
} finally {
this.loading(false);
}
Expand Down
40 changes: 27 additions & 13 deletions flow-client/src/main/resources/META-INF/resources/frontend/Flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export class Flow {
// @ts-ignore
container : HTMLRouterContainer;

// flag used to inform Testbench whether a server route is in progress
private isActive = false;
private isActiveCount = 0;

private baseRegex = /^\//;
private appShellTitle: string;

Expand Down Expand Up @@ -307,15 +307,18 @@ export class Flow {
});
}

// flag used to inform Testbench whether a server route is in progress
get isActive(): boolean {
return this.isActiveCount > 0;
}

private showLoading() {
// Make Testbench know that server request is in progress
this.isActive = true;
$wnd.Vaadin.Flow.loading(true);
}

private hideLoading() {
// Make Testbench know that server request has finished
this.isActive = false;
$wnd.Vaadin.Flow.loading(false);
}

Expand Down Expand Up @@ -385,16 +388,27 @@ export class Flow {
let timeout2nd: any;
let timeout3rd: any;
$wnd.Vaadin.Flow.loading = (action: boolean) => {
clearTimeout(timeout2nd);
clearTimeout(timeout3rd);
loading.classList.remove('second');
loading.classList.remove('third');
// true if we should reveal indicator (first loading starting)
const show = action && this.isActiveCount === 0;
// true if we should hide indicator (last loading finished)
const hide = !action && this.isActiveCount === 1;
if (action) {
loading.removeAttribute('style');
timeout2nd = setTimeout(() => loading.classList.add('second'), 1500);
timeout3rd = setTimeout(() => loading.classList.add('third'), 5000);
} else {
loading.setAttribute('style', 'none');
this.isActiveCount++;
} else if (this.isActiveCount > 0) {
this.isActiveCount--;
}
if (show || hide) {
haijian-vaadin marked this conversation as resolved.
Show resolved Hide resolved
clearTimeout(timeout2nd);
clearTimeout(timeout3rd);
loading.classList.remove('second');
loading.classList.remove('third');
if (show) {
loading.removeAttribute('style');
timeout2nd = setTimeout(() => loading.classList.add('second'), 1500);
timeout3rd = setTimeout(() => loading.classList.add('third'), 5000);
} else {
loading.setAttribute('style', 'none');
}
}
};
}
Expand Down
14 changes: 14 additions & 0 deletions flow-client/src/test/frontend/FlowTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ suite("Flow", () => {
assert.isFalse(indicator.classList.contains('third'));
});

test("loading should stack isActive", async () => {
const flow = new Flow({imports: () => {}});
assert.isFalse(flow.isActive);
$wnd.Vaadin.Flow.loading(true);
assert.isTrue(flow.isActive);
$wnd.Vaadin.Flow.loading(true);
assert.isTrue(flow.isActive);
$wnd.Vaadin.Flow.loading(false);
assert.isTrue(flow.isActive);
$wnd.Vaadin.Flow.loading(false);
assert.isFalse(flow.isActive);

});

test("should initialize Flow server navigation when calling flowInit(true)", () => {
assert.isUndefined($wnd.Vaadin);

Expand Down