Skip to content

Commit d1e118a

Browse files
fehmerMiodec
andauthored
refactor: page loadingOptions rework (@Miodec, @fehmer) (monkeytypegame#6949)
Co-authored-by: Miodec <[email protected]>
1 parent 31a7a2d commit d1e118a

File tree

4 files changed

+51
-29
lines changed

4 files changed

+51
-29
lines changed

frontend/src/ts/auth.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@ export async function onAuthStateChanged(
170170
await navigate(undefined, {
171171
force: true,
172172
loadingOptions: {
173-
shouldLoad: () => {
174-
return user !== null;
173+
loadingMode: () => {
174+
if (user !== null) {
175+
return "sync";
176+
} else {
177+
return "none";
178+
}
175179
},
176-
waitFor: async () => {
180+
loadingPromise: async () => {
177181
await userPromise;
178182
},
179183
style: "bar",

frontend/src/ts/controllers/page-controller.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function updateTitle(nextPage: { id: string; display?: string }): void {
5050
}
5151
}
5252

53-
async function showLoading({
53+
async function showSyncLoading({
5454
loadingOptions,
5555
totalDuration,
5656
easingMethod,
@@ -97,7 +97,7 @@ async function showLoading({
9797
void PageLoading.updateBar(100, 125);
9898
PageLoading.updateText("Done");
9999
} else {
100-
await options.waitFor();
100+
await options.loadingPromise();
101101
}
102102
}
103103

@@ -123,7 +123,7 @@ async function getLoadingPromiseWithBarKeyframes(
123123
fillOffset: number
124124
): Promise<void> {
125125
let aborted = false;
126-
let loadingPromise = loadingOptions.waitFor();
126+
let loadingPromise = loadingOptions.loadingPromise();
127127

128128
// Animate bar keyframes, but allow aborting if loading.promise finishes first
129129
const keyframePromise = (async () => {
@@ -214,28 +214,25 @@ export async function change(
214214
previousPage.element.addClass("hidden");
215215
await previousPage?.afterHide();
216216

217+
// we need to evaluate and store next page loading mode in case options.loadingOptions.loadingMode is sync
218+
const nextPageLoadingMode = nextPage.loadingOptions?.loadingMode();
219+
217220
//show loading page if needed
218221
try {
219-
let loadingOptions: LoadingOptions[] = [];
220-
if (options.loadingOptions) {
221-
loadingOptions.push(options.loadingOptions);
222+
let syncLoadingOptions: LoadingOptions[] = [];
223+
if (options.loadingOptions?.loadingMode() === "sync") {
224+
syncLoadingOptions.push(options.loadingOptions);
222225
}
223-
if (nextPage.loadingOptions) {
224-
loadingOptions.push(nextPage.loadingOptions);
226+
if (nextPage.loadingOptions?.loadingMode() === "sync") {
227+
syncLoadingOptions.push(nextPage.loadingOptions);
225228
}
226229

227-
if (loadingOptions.length > 0) {
228-
const shouldShowLoading =
229-
options.loadingOptions?.shouldLoad() ||
230-
nextPage.loadingOptions?.shouldLoad();
231-
232-
if (shouldShowLoading === true) {
233-
await showLoading({
234-
loadingOptions,
235-
totalDuration,
236-
easingMethod,
237-
});
238-
}
230+
if (syncLoadingOptions.length > 0) {
231+
await showSyncLoading({
232+
loadingOptions: syncLoadingOptions,
233+
totalDuration,
234+
easingMethod,
235+
});
239236
}
240237
} catch (error) {
241238
pages.loading.element.addClass("active");
@@ -263,6 +260,17 @@ export async function change(
263260
// @ts-expect-error for the future (i think)
264261
data: options.data,
265262
});
263+
264+
if (
265+
typeof nextPageLoadingMode === "object" &&
266+
nextPageLoadingMode.mode === "async"
267+
) {
268+
nextPageLoadingMode.beforeLoading();
269+
void nextPage?.loadingOptions?.loadingPromise().then(() => {
270+
nextPageLoadingMode.afterLoading();
271+
});
272+
}
273+
266274
nextPage.element.removeClass("hidden").css("opacity", 0);
267275
await Misc.promiseAnimation(
268276
nextPage.element,

frontend/src/ts/pages/account.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,10 +1323,14 @@ export const page = new Page({
13231323
element: $(".page.pageAccount"),
13241324
path: "/account",
13251325
loadingOptions: {
1326-
shouldLoad: () => {
1327-
return DB.getSnapshot()?.results === undefined;
1326+
loadingMode: () => {
1327+
if (DB.getSnapshot()?.results === undefined) {
1328+
return "sync";
1329+
} else {
1330+
return "none";
1331+
}
13281332
},
1329-
waitFor: async () => {
1333+
loadingPromise: async () => {
13301334
if (DB.getSnapshot() === null) {
13311335
throw new Error(
13321336
"Looks like your account data didn't download correctly. Please refresh the page.<br>If this error persists, please contact support."

frontend/src/ts/pages/page.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ type Options<T> = {
2424

2525
export type LoadingOptions = {
2626
/**
27-
* Should the loading screen be shown?
27+
* Get the loading mode for this page.
28+
* "none" - No loading screen will be shown.
29+
* "sync" - A loading spinner or bar (depending on style) will be shown until the page is ready.
30+
* { mode: "async", beforeLoading, afterLoading } - The loadingPromise will be executed in the background and afterLoading called after it resolves.
2831
*/
29-
shouldLoad: () => boolean;
32+
loadingMode: () =>
33+
| "none"
34+
| "sync"
35+
| { mode: "async"; beforeLoading: () => void; afterLoading: () => void };
3036
/**
3137
* When this promise resolves, the loading screen will be hidden.
3238
*/
33-
waitFor: () => Promise<void>;
39+
loadingPromise: () => Promise<void>;
3440
} & (
3541
| {
3642
style: "spinner";

0 commit comments

Comments
 (0)