Skip to content

Commit 0e49a9c

Browse files
committed
refactor: minimize function calls / use of bind
1 parent ae98f65 commit 0e49a9c

File tree

7 files changed

+94
-56
lines changed

7 files changed

+94
-56
lines changed

examples/vue-router/app.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const ChildComponent = {
1313
props: ['page'],
1414
template: `<div>
1515
<h3>You're looking at the <strong>{{ page }}</strong> page</h3>
16-
<p>Has metaInfo been updated? {{ metaUpdated }}</p>
16+
<p>Has metaInfo been updated due to navigation? {{ metaUpdated }}</p>
1717
</div>`,
1818
metaInfo () {
1919
return {
@@ -64,6 +64,13 @@ const router = new Router({
6464

6565
const App = {
6666
router,
67+
metaInfo () {
68+
return {
69+
meta: [
70+
{ charset: 'utf=8' }
71+
]
72+
}
73+
},
6774
template: `
6875
<div id="app">
6976
<h1>vue-router</h1>
@@ -80,3 +87,43 @@ const App = {
8087
const app = new Vue(App)
8188

8289
app.$mount('#app')
90+
/*
91+
const waitFor = time => new Promise(r => setTimeout(r, time || 1000))
92+
const o = {
93+
meta: [{ a: 1 }]
94+
}
95+
const ob = Vue.observable(o)
96+
97+
const root = new Vue({
98+
beforeCreate() {
99+
this.meta = ob.meta
100+
101+
this.$options.computed = this.$options.computed || {}
102+
this.$options.computed['$ob'] = () => {
103+
return { meta: this.meta }
104+
}
105+
},
106+
created() {
107+
console.log('HERE')
108+
this.$watch('$ob', (a, b) => {
109+
console.log('WATCHER', this.$ob.meta[0].a, a.meta[0].a, b.meta[0].a, diff(a, b))
110+
}, { deep: true })
111+
},
112+
render(h) {
113+
return h('div', null, 'test')
114+
}
115+
})
116+
117+
async function main () {
118+
root.$mount('#app')
119+
console.log(root)
120+
await waitFor(500)
121+
122+
root.meta[0].a = 2
123+
await waitFor(100)
124+
125+
ob.meta[0].a = 3
126+
await waitFor(100)
127+
}
128+
main()
129+
/**/

src/browser.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ function install (Vue, options = {}) {
1717

1818
options = setOptions(options)
1919

20-
Vue.prototype.$meta = $meta(options)
20+
Vue.prototype.$meta = function () {
21+
return $meta.call(this, options)
22+
}
2123

2224
Vue.mixin(createMixin(Vue, options))
2325
}

src/client/$meta.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,27 @@ import { getOptions } from '../shared/options'
33
import { pause, resume } from '../shared/pausing'
44
import refresh from './refresh'
55

6-
export default function _$meta (options = {}) {
7-
const _refresh = refresh(options)
8-
const inject = () => {}
9-
6+
export default function $meta (options = {}) {
107
/**
118
* Returns an injector for server-side rendering.
129
* @this {Object} - the Vue instance (a root component)
1310
* @return {Object} - injector
1411
*/
15-
return function $meta () {
16-
if (!this.$root._vueMeta) {
17-
return {
18-
getOptions: showWarningNotSupported,
19-
refresh: showWarningNotSupported,
20-
inject: showWarningNotSupported,
21-
pause: showWarningNotSupported,
22-
resume: showWarningNotSupported
23-
}
24-
}
25-
12+
if (!this.$root._vueMeta) {
2613
return {
27-
getOptions: () => getOptions(options),
28-
refresh: _refresh.bind(this),
29-
inject,
30-
pause: pause.bind(this),
31-
resume: resume.bind(this)
14+
getOptions: showWarningNotSupported,
15+
refresh: showWarningNotSupported,
16+
inject: showWarningNotSupported,
17+
pause: showWarningNotSupported,
18+
resume: showWarningNotSupported
3219
}
3320
}
21+
22+
return {
23+
getOptions: () => getOptions(options),
24+
refresh: () => refresh.call(this, options),
25+
inject: () => {},
26+
pause: () => pause.call(this),
27+
resume: () => resume.call(this)
28+
}
3429
}

src/client/refresh.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import getMetaInfo from '../shared/getMetaInfo'
44
import { isFunction } from '../utils/is-type'
55
import updateClientMetaInfo from './updateClientMetaInfo'
66

7-
export default function _refresh (options = {}) {
7+
export default function refresh (options = {}) {
88
/**
99
* When called, will update the current meta info with new meta info.
1010
* Useful when updating meta info as the result of an asynchronous
@@ -15,20 +15,18 @@ export default function _refresh (options = {}) {
1515
*
1616
* @return {Object} - new meta info
1717
*/
18-
return function refresh () {
19-
// collect & aggregate all metaInfo $options
20-
const rawInfo = getComponentMetaInfo(options, this.$root)
18+
// collect & aggregate all metaInfo $options
19+
const rawInfo = getComponentMetaInfo(options, this.$root)
2120

22-
const metaInfo = getMetaInfo(options, rawInfo, clientSequences, this.$root)
21+
const metaInfo = getMetaInfo(options, rawInfo, clientSequences, this.$root)
2322

24-
const appId = this.$root._vueMeta.appId
25-
const tags = updateClientMetaInfo(appId, options, metaInfo)
23+
const appId = this.$root._vueMeta.appId
24+
const tags = updateClientMetaInfo(appId, options, metaInfo)
2625

27-
// emit "event" with new info
28-
if (tags && isFunction(metaInfo.changed)) {
29-
metaInfo.changed(metaInfo, tags.addedTags, tags.removedTags)
30-
}
31-
32-
return { vm: this, metaInfo, tags }
26+
// emit "event" with new info
27+
if (tags && isFunction(metaInfo.changed)) {
28+
metaInfo.changed(metaInfo, tags.addedTags, tags.removedTags)
3329
}
30+
31+
return { vm: this, metaInfo, tags }
3432
}

src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ function install (Vue, options = {}) {
1717

1818
options = setOptions(options)
1919

20-
Vue.prototype.$meta = $meta(options)
20+
Vue.prototype.$meta = function () {
21+
return $meta.call(this, options)
22+
}
2123

2224
Vue.mixin(createMixin(Vue, options))
2325
}

src/server/$meta.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@ import { pause, resume } from '../shared/pausing'
33
import refresh from '../client/refresh'
44
import inject from './inject'
55

6-
export default function _$meta (options = {}) {
7-
const _refresh = refresh(options)
8-
const _inject = inject(options)
9-
6+
export default function $meta (options = {}) {
107
/**
118
* Returns an injector for server-side rendering.
129
* @this {Object} - the Vue instance (a root component)
1310
* @return {Object} - injector
1411
*/
15-
return function $meta () {
16-
return {
17-
getOptions: () => getOptions(options),
18-
refresh: _refresh.bind(this),
19-
inject: _inject.bind(this),
20-
pause: pause.bind(this),
21-
resume: resume.bind(this)
22-
}
12+
return {
13+
getOptions: () => getOptions(options),
14+
refresh: () => refresh.call(this, options),
15+
inject: () => inject.call(this, options),
16+
pause: () => pause.call(this),
17+
resume: () => resume.call(this)
2318
}
2419
}

src/server/inject.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ export default function _inject (options = {}) {
1111
* @this {Object} - Vue instance - ideally the root component
1212
* @return {Object} - server meta info with `toString` methods
1313
*/
14-
return function inject () {
15-
// collect & aggregate all metaInfo $options
16-
const rawInfo = getComponentMetaInfo(options, this.$root)
1714

18-
const metaInfo = getMetaInfo(options, rawInfo, serverSequences, this.$root)
15+
// collect & aggregate all metaInfo $options
16+
const rawInfo = getComponentMetaInfo(options, this.$root)
1917

20-
// generate server injectors
21-
generateServerInjector(options, metaInfo)
18+
const metaInfo = getMetaInfo(options, rawInfo, serverSequences, this.$root)
2219

23-
return metaInfo
24-
}
20+
// generate server injectors
21+
generateServerInjector(options, metaInfo)
22+
23+
return metaInfo
2524
}

0 commit comments

Comments
 (0)