2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-25 12:20:33 +03:00

refactor: minimize function calls / use of bind

This commit is contained in:
pimlie
2019-09-12 17:19:55 +02:00
parent ae98f65511
commit 0e49a9c43e
7 changed files with 94 additions and 56 deletions
+48 -1
View File
@@ -13,7 +13,7 @@ const ChildComponent = {
props: ['page'],
template: `<div>
<h3>You're looking at the <strong>{{ page }}</strong> page</h3>
<p>Has metaInfo been updated? {{ metaUpdated }}</p>
<p>Has metaInfo been updated due to navigation? {{ metaUpdated }}</p>
</div>`,
metaInfo () {
return {
@@ -64,6 +64,13 @@ const router = new Router({
const App = {
router,
metaInfo () {
return {
meta: [
{ charset: 'utf=8' }
]
}
},
template: `
<div id="app">
<h1>vue-router</h1>
@@ -80,3 +87,43 @@ const App = {
const app = new Vue(App)
app.$mount('#app')
/*
const waitFor = time => new Promise(r => setTimeout(r, time || 1000))
const o = {
meta: [{ a: 1 }]
}
const ob = Vue.observable(o)
const root = new Vue({
beforeCreate() {
this.meta = ob.meta
this.$options.computed = this.$options.computed || {}
this.$options.computed['$ob'] = () => {
return { meta: this.meta }
}
},
created() {
console.log('HERE')
this.$watch('$ob', (a, b) => {
console.log('WATCHER', this.$ob.meta[0].a, a.meta[0].a, b.meta[0].a, diff(a, b))
}, { deep: true })
},
render(h) {
return h('div', null, 'test')
}
})
async function main () {
root.$mount('#app')
console.log(root)
await waitFor(500)
root.meta[0].a = 2
await waitFor(100)
ob.meta[0].a = 3
await waitFor(100)
}
main()
/**/
+3 -1
View File
@@ -17,7 +17,9 @@ function install (Vue, options = {}) {
options = setOptions(options)
Vue.prototype.$meta = $meta(options)
Vue.prototype.$meta = function () {
return $meta.call(this, options)
}
Vue.mixin(createMixin(Vue, options))
}
+5 -10
View File
@@ -3,16 +3,12 @@ import { getOptions } from '../shared/options'
import { pause, resume } from '../shared/pausing'
import refresh from './refresh'
export default function _$meta (options = {}) {
const _refresh = refresh(options)
const inject = () => {}
export default function $meta (options = {}) {
/**
* Returns an injector for server-side rendering.
* @this {Object} - the Vue instance (a root component)
* @return {Object} - injector
*/
return function $meta () {
if (!this.$root._vueMeta) {
return {
getOptions: showWarningNotSupported,
@@ -25,10 +21,9 @@ export default function _$meta (options = {}) {
return {
getOptions: () => getOptions(options),
refresh: _refresh.bind(this),
inject,
pause: pause.bind(this),
resume: resume.bind(this)
}
refresh: () => refresh.call(this, options),
inject: () => {},
pause: () => pause.call(this),
resume: () => resume.call(this)
}
}
+1 -3
View File
@@ -4,7 +4,7 @@ import getMetaInfo from '../shared/getMetaInfo'
import { isFunction } from '../utils/is-type'
import updateClientMetaInfo from './updateClientMetaInfo'
export default function _refresh (options = {}) {
export default function refresh (options = {}) {
/**
* When called, will update the current meta info with new meta info.
* Useful when updating meta info as the result of an asynchronous
@@ -15,7 +15,6 @@ export default function _refresh (options = {}) {
*
* @return {Object} - new meta info
*/
return function refresh () {
// collect & aggregate all metaInfo $options
const rawInfo = getComponentMetaInfo(options, this.$root)
@@ -31,4 +30,3 @@ export default function _refresh (options = {}) {
return { vm: this, metaInfo, tags }
}
}
+3 -1
View File
@@ -17,7 +17,9 @@ function install (Vue, options = {}) {
options = setOptions(options)
Vue.prototype.$meta = $meta(options)
Vue.prototype.$meta = function () {
return $meta.call(this, options)
}
Vue.mixin(createMixin(Vue, options))
}
+5 -10
View File
@@ -3,22 +3,17 @@ import { pause, resume } from '../shared/pausing'
import refresh from '../client/refresh'
import inject from './inject'
export default function _$meta (options = {}) {
const _refresh = refresh(options)
const _inject = inject(options)
export default function $meta (options = {}) {
/**
* Returns an injector for server-side rendering.
* @this {Object} - the Vue instance (a root component)
* @return {Object} - injector
*/
return function $meta () {
return {
getOptions: () => getOptions(options),
refresh: _refresh.bind(this),
inject: _inject.bind(this),
pause: pause.bind(this),
resume: resume.bind(this)
}
refresh: () => refresh.call(this, options),
inject: () => inject.call(this, options),
pause: () => pause.call(this),
resume: () => resume.call(this)
}
}
+1 -2
View File
@@ -11,7 +11,7 @@ export default function _inject (options = {}) {
* @this {Object} - Vue instance - ideally the root component
* @return {Object} - server meta info with `toString` methods
*/
return function inject () {
// collect & aggregate all metaInfo $options
const rawInfo = getComponentMetaInfo(options, this.$root)
@@ -22,4 +22,3 @@ export default function _inject (options = {}) {
return metaInfo
}
}