Skip to content

Instantly share code, notes, and snippets.

@galvez
Last active April 10, 2019 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galvez/8595805ece7d817a52e75a866525542d to your computer and use it in GitHub Desktop.
Save galvez/8595805ece7d817a52e75a866525542d to your computer and use it in GitHub Desktop.
import Vue from 'vue'
function componentInject(key, setter) {
const installKey = `__component_inject_${key}`
if (Vue[installKey]) {
return
}
Vue[installKey] = true
const componentKey = `$${key}`
const privateKey = `_${key}`
Vue.use(() => {
if (!Vue.prototype.hasOwnProperty(componentKey)) {
Object.defineProperty(Vue.prototype, componentKey, {
get() {
if (!this[privateKey]) {
this[privateKey] = setter(this)
}
return this[privateKey]
}
})
}
})
}
export default function() {
componentInject('every', (component) => {
return (ms) => {
return new Proxy({}, {
get(_, prop) {
return (...args) => {
return setInterval(() => {
component[prop].call(component, ...args)
}, ms)
}
}
})
}
})
componentInject('after', (component) => {
return (ms) => {
return new Proxy({}, {
get(_, prop) {
return (...args) => {
return setTimeout(() => {
component[prop].call(component, ...args)
}, ms)
}
}
})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment