Skip to content

Commit 8fd7ade

Browse files
committed
refactor the how the render functions called
1 parent 5337d35 commit 8fd7ade

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

src/main.ts

+50-51
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,49 @@ import "./index.css";
1111
const fb = new FastbootManager();
1212
fb.init();
1313

14-
const linearProgressCtnEl = document.getElementById("linear-progress-ctn")!;
15-
const linearProgressEl = document.getElementById("linear-progress")!;
16-
const titleEl = document.getElementById("title")!;
17-
const iconCtnEl = document.getElementById("icon-ctn")!;
18-
19-
fb.addEventListener("step", ((event: CustomEvent<FastbootManagerStateType>) => {
20-
const state = event.detail;
21-
updateIcon(iconCtnEl, state);
22-
updateTitle(titleEl, state);
23-
}) as EventListener);
24-
25-
fb.addEventListener("progress", ((
26-
event: CustomEvent<FastbootManagerStateType>,
27-
) => {
28-
const state = event.detail;
29-
console.log("progress", state);
30-
updateLinearProgress(linearProgressEl, state);
31-
}) as EventListener);
32-
33-
fb.addEventListener("message", ((
34-
event: CustomEvent<FastbootManagerStateType>,
35-
) => {
36-
const state = event.detail;
37-
updateTitle(titleEl, state);
38-
}) as EventListener);
39-
40-
fb.addEventListener("error", ((
41-
event: CustomEvent<FastbootManagerStateType>,
42-
) => {
43-
const state = event.detail;
44-
updateIcon(iconCtnEl, state);
45-
}) as EventListener);
46-
47-
function updateLinearProgress(
48-
element: HTMLElement,
49-
state: FastbootManagerStateType,
50-
) {
14+
function setupProgressIndicatorView(initialState: FastbootManagerStateType) {
15+
renderProgressIndicator(initialState);
16+
fb.on("progress", renderProgressIndicator);
17+
}
18+
19+
function setupStatusView(initialState: FastbootManagerStateType) {
20+
renderStatusView(initialState);
21+
fb.on("message", renderStatusView);
22+
fb.on("step", renderStatusView);
23+
fb.on("error", renderStatusView);
24+
}
25+
26+
function setupIconView(initialState: FastbootManagerStateType) {
27+
renderIconView(initialState);
28+
fb.on("step", renderIconView);
29+
fb.on("error", renderIconView);
30+
}
31+
32+
function setupRetryButtonView(initialState: FastbootManagerStateType) {
33+
renderRetryButtonView(initialState);
34+
fb.on("error", renderRetryButtonView);
35+
}
36+
37+
function setupDeviceStatusView(initialState: FastbootManagerStateType) {
38+
renderDeviceStateView(initialState);
39+
fb.on("connected", renderDeviceStateView);
40+
fb.on("serial", renderDeviceStateView);
41+
}
42+
43+
function renderProgressIndicator(state: FastbootManagerStateType) {
44+
const el = document.getElementById("linear-progress")!;
45+
const ctnEl = document.getElementById("linear-progress-ctn")!;
46+
5147
const { progress, step } = state;
52-
element.style.transform = `translateX(${progress - 100}%)`;
53-
element.className = `absolute top-0 bottom-0 left-0 w-full transition-all ${fbSteps[step].bgColor}`;
54-
linearProgressCtnEl.style.opacity = progress === -1 ? "0" : "1";
48+
el.style.transform = `translateX(${progress - 100}%)`;
49+
el.className = `absolute top-0 bottom-0 left-0 w-full transition-all ${fbSteps[step].bgColor}`;
50+
ctnEl.style.opacity = progress === -1 ? "0" : "1";
5551
}
5652

57-
function updateTitle(el: HTMLElement, state: FastbootManagerStateType) {
53+
function renderStatusView(state: FastbootManagerStateType) {
54+
const el = document.getElementById("title")!;
55+
const subtitleEl = document.getElementById("subtitle")!;
56+
5857
const { message, error, progress, step } = state;
5958
let title;
6059
if (message && !error) {
@@ -66,23 +65,23 @@ function updateTitle(el: HTMLElement, state: FastbootManagerStateType) {
6665
title = fbSteps[step].status;
6766
}
6867
el.innerHTML = title;
69-
const subtitleEl = document.getElementById("subtitle")!;
7068
subtitleEl.innerHTML = fbSteps[step].description ?? "";
7169
}
7270

73-
function updateIcon(el: HTMLElement, state: FastbootManagerStateType) {
71+
function renderIconView(state: FastbootManagerStateType) {
72+
const el = document.getElementById("icon-ctn")!;
73+
const img = el.getElementsByTagName("img")[0];
7474
const { step, error, onContinue } = state;
7575
el.className = `p-8 rounded-full ${fbSteps[step].bgColor}`;
7676
if (onContinue) {
7777
el.style.cursor = "pointer";
7878
el.addEventListener("click", onContinue);
7979
}
80-
const img = el.getElementsByTagName("img")[0];
8180
img.src = fbSteps[step].icon;
8281
img.className = `${!error && step !== FastbootStep.DONE ? "animate-pulse" : ""}`;
8382
}
8483

85-
function updateRetryButton(state: FastbootManagerStateType) {
84+
function renderRetryButtonView(state: FastbootManagerStateType) {
8685
const { error } = state;
8786
if (error !== FastbootError.NONE) {
8887
const el = document.getElementById("subtitle")!;
@@ -108,7 +107,7 @@ function updateRetryButton(state: FastbootManagerStateType) {
108107
}
109108
}
110109

111-
function updateDeviceState(state: FastbootManagerStateType) {
110+
function renderDeviceStateView(state: FastbootManagerStateType) {
112111
const { serial, connected } = state;
113112
if (!connected) {
114113
const deviceStateEl = document.getElementById("device-state");
@@ -214,8 +213,8 @@ const fbSteps: Record<
214213
},
215214
};
216215

217-
updateIcon(iconCtnEl, fb.state);
218-
updateTitle(titleEl, fb.state);
219-
updateLinearProgress(linearProgressEl, fb.state);
220-
updateRetryButton(fb.state);
221-
updateDeviceState(fb.state);
216+
setupProgressIndicatorView(fb.state);
217+
setupIconView(fb.state);
218+
setupStatusView(fb.state);
219+
setupDeviceStatusView(fb.state);
220+
setupRetryButtonView(fb.state);

src/utils/fastboot.ts

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ export class FastbootManager extends EventTarget {
3333
this.manifest = null;
3434
}
3535

36+
on(type: string, listener: (data: FastbootManagerStateType) => void) {
37+
this.addEventListener(type, ((
38+
event: CustomEvent<FastbootManagerStateType>,
39+
) => listener(event.detail)) as EventListener);
40+
}
41+
3642
private setStep(step: FastbootStep) {
3743
this.state.step = step;
3844
this.dispatchEvent(new CustomEvent("step", { detail: this.state }));

0 commit comments

Comments
 (0)