Last active
April 10, 2019 14:19
-
-
Save galvez/8595805ece7d817a52e75a866525542d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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