Skip to content

Commit 2296686

Browse files
authored
chore: unify interval/timeout handling (#6015)
1 parent 16f777b commit 2296686

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

docs/plugins/docs-mixin.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ const TOC_CACHE = {}
88

99
// @vue/component
1010
export default {
11-
data() {
12-
return {
13-
scrollTimeout: null
14-
}
15-
},
1611
head() {
1712
return {
1813
title: this.headTitle,
@@ -70,6 +65,9 @@ export default {
7065
}
7166
},
7267
created() {
68+
// Create private non-reactive props
69+
this.$_filterTimer = null
70+
7371
// In a `$nextTick()` to ensure `toc.vue` is created first
7472
this.$nextTick(() => {
7573
const key = `${this.$route.name}_${this.$route.params.slug || ''}`
@@ -91,10 +89,8 @@ export default {
9189
},
9290
methods: {
9391
clearScrollTimeout() {
94-
if (this.scrollTimeout) {
95-
clearTimeout(this.scrollTimeout)
96-
this.scrollTimeout = null
97-
}
92+
clearTimeout(this.$_scrollTimeout)
93+
this.$_scrollTimeout = null
9894
},
9995
focusScroll() {
10096
const hash = this.$route.hash
@@ -119,9 +115,9 @@ export default {
119115
if (el) {
120116
// Get the document scrolling element
121117
const scroller = document.scrollingElement || document.documentElement || document.body
118+
this.clearScrollTimeout()
122119
// Allow time for v-play to finish rendering
123-
this.scrollTimeout = setTimeout(() => {
124-
this.clearScrollTimeout()
120+
this.$_scrollTimeout = setTimeout(() => {
125121
// Scroll heading into view (minus offset to account for nav top height)
126122
scrollTo(scroller, offsetTop(el) - 70, 100)
127123
}, 100)

src/components/alert/alert.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export const BAlert = /*#__PURE__*/ Vue.extend({
6262
data() {
6363
return {
6464
countDown: 0,
65-
countDownTimeout: null,
6665
// If initially shown, we need to set these for SSR
6766
localShow: parseShow(this.show)
6867
}
@@ -83,7 +82,7 @@ export const BAlert = /*#__PURE__*/ Vue.extend({
8382
}
8483
if (newVal > 0) {
8584
this.localShow = true
86-
this.countDownTimeout = setTimeout(() => {
85+
this.$_countDownTimeout = setTimeout(() => {
8786
this.countDown--
8887
}, 1000)
8988
} else {
@@ -108,6 +107,9 @@ export const BAlert = /*#__PURE__*/ Vue.extend({
108107
}
109108
},
110109
created() {
110+
// Create private non-reactive props
111+
this.$_filterTimer = null
112+
111113
this.countDown = parseCountDown(this.show)
112114
this.localShow = parseShow(this.show)
113115
},
@@ -125,10 +127,8 @@ export const BAlert = /*#__PURE__*/ Vue.extend({
125127
this.localShow = false
126128
},
127129
clearCountDownInterval() {
128-
if (this.countDownTimeout) {
129-
clearTimeout(this.countDownTimeout)
130-
this.countDownTimeout = null
131-
}
130+
clearTimeout(this.$_countDownTimeout)
131+
this.$_countDownTimeout = null
132132
}
133133
},
134134
render(h) {

src/components/toast/toast.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ export const BToast = /*#__PURE__*/ Vue.extend({
124124
isTransitioning: false,
125125
isHiding: false,
126126
order: 0,
127-
timer: null,
128127
dismissStarted: 0,
129128
resumeDismiss: 0
130129
}
@@ -189,6 +188,10 @@ export const BToast = /*#__PURE__*/ Vue.extend({
189188
}
190189
}
191190
},
191+
created() {
192+
// Create private non-reactive props
193+
this.$_dismissTimer = null
194+
},
192195
mounted() {
193196
this.isMounted = true
194197
this.$nextTick(() => {
@@ -289,14 +292,14 @@ export const BToast = /*#__PURE__*/ Vue.extend({
289292
startDismissTimer() {
290293
this.clearDismissTimer()
291294
if (!this.noAutoHide) {
292-
this.timer = setTimeout(this.hide, this.resumeDismiss || this.computedDuration)
295+
this.$_dismissTimer = setTimeout(this.hide, this.resumeDismiss || this.computedDuration)
293296
this.dismissStarted = Date.now()
294297
this.resumeDismiss = 0
295298
}
296299
},
297300
clearDismissTimer() {
298-
clearTimeout(this.timer)
299-
this.timer = null
301+
clearTimeout(this.$_dismissTimer)
302+
this.$_dismissTimer = null
300303
},
301304
setHoverHandler(on) {
302305
const el = this.$refs['b-toast']
@@ -305,7 +308,7 @@ export const BToast = /*#__PURE__*/ Vue.extend({
305308
},
306309
onPause() {
307310
// Determine time remaining, and then pause timer
308-
if (this.noAutoHide || this.noHoverPause || !this.timer || this.resumeDismiss) {
311+
if (this.noAutoHide || this.noHoverPause || !this.$_dismissTimer || this.resumeDismiss) {
309312
return
310313
}
311314
const passed = Date.now() - this.dismissStarted

src/components/toast/toast.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ describe('b-toast', () => {
214214
await waitRAF()
215215

216216
expect(wrapper.element.tagName).toBe('DIV')
217-
expect(wrapper.vm.timer).not.toEqual(null)
217+
expect(wrapper.vm.$_dismissTimer).not.toEqual(null)
218218

219219
jest.runOnlyPendingTimers()
220220

@@ -228,7 +228,7 @@ describe('b-toast', () => {
228228
await waitRAF()
229229

230230
expect(wrapper.element.nodeType).toBe(Node.COMMENT_NODE)
231-
expect(wrapper.vm.timer).toBe(null)
231+
expect(wrapper.vm.$_dismissTimer).toBe(null)
232232

233233
wrapper.destroy()
234234
})
@@ -259,17 +259,17 @@ describe('b-toast', () => {
259259
await waitRAF()
260260

261261
expect(wrapper.element.tagName).toBe('DIV')
262-
expect(wrapper.vm.timer).not.toEqual(null)
262+
expect(wrapper.vm.$_dismissTimer).not.toEqual(null)
263263
await waitNT(wrapper.vm)
264264
await waitRAF()
265265

266266
await wrapper.trigger('mouseenter')
267267
await waitRAF()
268-
expect(wrapper.vm.timer).toEqual(null)
268+
expect(wrapper.vm.$_dismissTimer).toEqual(null)
269269

270270
await wrapper.trigger('mouseleave')
271271
await waitRAF()
272-
expect(wrapper.vm.timer).not.toEqual(null)
272+
expect(wrapper.vm.$_dismissTimer).not.toEqual(null)
273273

274274
wrapper.destroy()
275275
})

0 commit comments

Comments
 (0)