Skip to content

Instantly share code, notes, and snippets.

@viclm
Created May 7, 2018 08:19
Show Gist options
  • Save viclm/4cba38c79f63d31801767b2adb2bef7d to your computer and use it in GitHub Desktop.
Save viclm/4cba38c79f63d31801767b2adb2bef7d to your computer and use it in GitHub Desktop.
function proxy(context, target, assert) {
let queue = []
function send(methodName, ...methodArguments) {
const object = context[target]
if (object && eval(assert)) {
object[methodName](...methodArguments)
}
else {
queue.push({methodName, methodArguments})
}
}
send.register = function (...methodNames) {
for (let i = 0; i < methodNames.length; i++) {
this[methodNames[i]] = this.bind(null, methodNames[i])
}
}
function runQueue() {
const object = context[target]
for (let i = 0, invoke ; i < queue.length ; i++) {
invoke = queue[i]
object[invoke.methodName](...invoke.methodArguments)
}
queue = []
}
(function loop() {
if (context[target] && eval(assert)) {
runQueue()
}
else {
setTimeout(loop, 500)
}
})()
return send
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment