mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-17 10:50:34 +03:00
feat: add options debounceWait
This commit is contained in:
@@ -3,7 +3,7 @@ import { rootConfigKey } from '../shared/constants'
|
||||
// store an id to keep track of DOM updates
|
||||
let batchId = null
|
||||
|
||||
export function triggerUpdate (rootVm, hookName) {
|
||||
export function triggerUpdate ({ debounceWait }, rootVm, hookName) {
|
||||
// if an update was triggered during initialization or when an update was triggered by the
|
||||
// metaInfo watcher, set initialized to null
|
||||
// then we keep falsy value but know we need to run a triggerUpdate after initialization
|
||||
@@ -13,7 +13,7 @@ export function triggerUpdate (rootVm, hookName) {
|
||||
|
||||
if (rootVm[rootConfigKey].initialized && !rootVm[rootConfigKey].pausing) {
|
||||
// batch potential DOM updates to prevent extraneous re-rendering
|
||||
batchUpdate(() => rootVm.$meta().refresh())
|
||||
batchUpdate(() => void rootVm.$meta().refresh(), debounceWait)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,10 +25,14 @@ export function triggerUpdate (rootVm, hookName) {
|
||||
* @return {Number} id - a new ID
|
||||
*/
|
||||
export function batchUpdate (callback, timeout) {
|
||||
timeout = timeout || 10
|
||||
timeout = timeout === undefined ? 10 : timeout
|
||||
|
||||
if (!timeout) {
|
||||
callback()
|
||||
return
|
||||
}
|
||||
|
||||
clearTimeout(batchId)
|
||||
|
||||
batchId = setTimeout(() => {
|
||||
callback()
|
||||
}, timeout)
|
||||
|
||||
@@ -23,6 +23,14 @@ export default function $meta (options) {
|
||||
options.refreshOnceOnNavigation = newOptions[refreshNavKey]
|
||||
addNavGuards($root)
|
||||
}
|
||||
|
||||
const debounceWaitKey = 'debounceWait'
|
||||
if (newOptions && newOptions[debounceWaitKey]) {
|
||||
const debounceWait = parseInt(newOptions[debounceWaitKey])
|
||||
if (!isNaN(debounceWait)) {
|
||||
options.debounceWait = debounceWait
|
||||
}
|
||||
}
|
||||
},
|
||||
'refresh': () => refresh($root, options),
|
||||
'inject': () => process.server ? inject($root, options) : showWarningNotSupportedInBrowserBundle('inject'),
|
||||
|
||||
@@ -49,6 +49,9 @@ export const contentKeyName = 'content'
|
||||
// The id used for the ssr app
|
||||
export const ssrAppId = 'ssr'
|
||||
|
||||
// How long meta update
|
||||
export const debounceWait = 10
|
||||
|
||||
export const defaultOptions = {
|
||||
keyName,
|
||||
attribute,
|
||||
@@ -56,6 +59,7 @@ export const defaultOptions = {
|
||||
tagIDKeyName,
|
||||
contentKeyName,
|
||||
metaTemplateKeyName,
|
||||
debounceWait,
|
||||
ssrAppId
|
||||
}
|
||||
|
||||
|
||||
+4
-5
@@ -69,7 +69,7 @@ export default function createMixin (Vue, options) {
|
||||
// credit for this suggestion goes to [Sébastien Chopin](https://github.com/Atinux)
|
||||
ensuredPush($options, 'created', function () {
|
||||
this.$watch('$metaInfo', function () {
|
||||
triggerUpdate(this[rootKey], 'watcher')
|
||||
triggerUpdate(options, this[rootKey], 'watcher')
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -112,7 +112,7 @@ export default function createMixin (Vue, options) {
|
||||
// current hook was called
|
||||
// (during initialization all changes are blocked)
|
||||
if (tags === false && $root[rootConfigKey].initialized === null) {
|
||||
this.$nextTick(() => triggerUpdate($root, 'init'))
|
||||
this.$nextTick(() => triggerUpdate(options, $root, 'init'))
|
||||
}
|
||||
|
||||
$root[rootConfigKey].initialized = true
|
||||
@@ -126,7 +126,6 @@ export default function createMixin (Vue, options) {
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// add the navigation guards if requested
|
||||
if (options.refreshOnceOnNavigation) {
|
||||
addNavGuards($root)
|
||||
@@ -142,7 +141,7 @@ export default function createMixin (Vue, options) {
|
||||
// no need to add this hooks on server side
|
||||
updateOnLifecycleHook.forEach((lifecycleHook) => {
|
||||
ensuredPush($options, lifecycleHook, function () {
|
||||
triggerUpdate(this[rootKey], lifecycleHook)
|
||||
triggerUpdate(options, this[rootKey], lifecycleHook)
|
||||
})
|
||||
})
|
||||
},
|
||||
@@ -165,7 +164,7 @@ export default function createMixin (Vue, options) {
|
||||
|
||||
clearInterval(interval)
|
||||
|
||||
triggerUpdate($this.$root, 'destroyed')
|
||||
triggerUpdate(options, $this.$root, 'destroyed')
|
||||
}, 50)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ export function setOptions (options) {
|
||||
tagIDKeyName: options['tagIDKeyName'] || defaultOptions.tagIDKeyName,
|
||||
contentKeyName: options['contentKeyName'] || defaultOptions.contentKeyName,
|
||||
metaTemplateKeyName: options['metaTemplateKeyName'] || defaultOptions.metaTemplateKeyName,
|
||||
debounceWait: options['debounceWait'] || defaultOptions.debounceWait,
|
||||
ssrAppId: options['ssrAppId'] || defaultOptions.ssrAppId,
|
||||
refreshOnceOnNavigation: !!options['refreshOnceOnNavigation']
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user