Skip to content

Commit 3a8ab7f

Browse files
committed
refactor: 优化性能
1 parent bd404f7 commit 3a8ab7f

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/BootstrapBlazor/Components/Toast/Toast.razor.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,64 @@ import EventHandler from "../../modules/event-handler.js"
33

44
export function init(id, invoke, callback) {
55
const el = document.getElementById(id)
6+
const progressElement = el.querySelector('.toast-progress')
67
const toast = {
78
element: el,
89
invoke,
910
callback,
1011
toast: new bootstrap.Toast(el),
1112
showProgress: () => {
1213
return toast.toast._config.autohide
13-
}
14+
},
15+
progressElement
1416
}
1517
Data.set(id, toast);
1618

1719
if (toast.showProgress()) {
18-
toast.progressElement = toast.element.querySelector('.toast-progress')
1920
const delay = toast.toast._config.delay / 1000
20-
toast.progressElement.style.transition = `width linear ${delay}s`
21+
progressElement.style.transition = `width linear ${delay}s`
2122
}
2223

23-
EventHandler.on(toast.element, 'shown.bs.toast', () => {
24+
EventHandler.on(el, 'shown.bs.toast', () => {
2425
if (toast.showProgress()) {
25-
toast.progressElement.style.width = '100%'
26+
progressElement.style.width = '100%'
2627
}
2728
})
28-
EventHandler.on(toast.element, 'hidden.bs.toast', () => {
29-
toast.invoke.invokeMethodAsync(toast.callback)
29+
EventHandler.on(el, 'hidden.bs.toast', () => {
30+
invoke.invokeMethodAsync(toast.callback)
3031
})
31-
toast.toast.show()
32+
EventHandler.on(progressElement, 'transitionend', e => {
33+
toast.toast.hide();
34+
});
35+
36+
toast.toast.show();
3237
}
3338

3439
export function update(id) {
3540
const t = Data.get(id);
36-
const { element, toast } = t;
41+
const { element, toast, progressElement } = t;
3742
const autoHide = element.getAttribute('data-bs-autohide') !== 'false';
38-
if(autoHide) {
43+
if (autoHide) {
3944
const delay = parseInt(element.getAttribute('data-bs-delay'));
40-
const progressElement = element.querySelector('.toast-progress');
4145

4246
toast._config.autohide = autoHide;
4347
toast._config.delay = delay;
4448

4549
progressElement.style.width = '100%';
4650
progressElement.style.transition = `width linear ${delay / 1000}s`;
47-
EventHandler.on(progressElement, 'transitionend', e => {
48-
toast.hide();
49-
});
5051
}
5152
}
5253

5354
export function dispose(id) {
5455
const toast = Data.get(id)
5556
Data.remove(id)
5657

57-
EventHandler.off(toast.element, 'shown.bs.toast')
58-
EventHandler.off(toast.element, 'hidden.bs.toast')
58+
const { element, progressElement } = toast;
59+
EventHandler.off(element, 'shown.bs.toast');
60+
EventHandler.off(element, 'hidden.bs.toast');
61+
EventHandler.off(progressElement, 'transitionend');
5962

6063
if (toast.toast) {
61-
toast.toast.dispose()
64+
toast.toast.dispose();
6265
}
6366
}

0 commit comments

Comments
 (0)