Skip to content

Instantly share code, notes, and snippets.

@bterlson
Created January 24, 2014 22:55
Show Gist options
  • Save bterlson/8608516 to your computer and use it in GitHub Desktop.
Save bterlson/8608516 to your computer and use it in GitHub Desktop.
function assertOrderOfOps(act, exp) {
exp.forEach(function(e, i) {
if(act[i][0] !== exp[i][0] || act[i][1] !== exp[i][1]) {
throw new Error('Expected operation ' + i + ' to be ' + exp[i] + ' but was ' + act[i]);
}
});
if(act.length > exp.length) {
throw new Error('More operations than expected: ' +
act.slice(exp.length).map(function(e) { return e[0] })
);
}
}
function createLoggerProxy(obj) {
var ops = [];
var proxy = new Proxy(obj, new Proxy({}, {
get: function(o, v, r) {
return function() {
ops.push([v, arguments[1]]);
return Reflect[v].apply(this, arguments);
}
}
}))
return {
log: ops,
proxy: proxy
}
}
@bterlson
Copy link
Author

Example test case (can run in firefox with tvcutsem's Reflect polyfill script included):

var arr = [0];
var res = createLoggerProxy(arr);
var p = res.proxy;
var log = res.log;
p.pop();
assertOrderOfOps(res.log, [
    ['get', 'pop'],
    ['get', 'length'],
    ['get', '0'],
    ['delete', '0'],
    ['set', 'length']
]);

Small snippet to include the polyfill in your current context:

var s = document.createElement('script'); s.src = 'https://rawgithub.com/tvcutsem/harmony-reflect/master/reflect.js'; document.body.appendChild(s);

@allenwb
Copy link

allenwb commented Jan 25, 2014

Brian,
Based upon spec bug reports I've received, I think the Chrome guys may already be using something like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment