Created
May 7, 2018 08:19
-
-
Save viclm/4cba38c79f63d31801767b2adb2bef7d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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