Skip to content

Instantly share code, notes, and snippets.

@kevincennis
Created October 19, 2016 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevincennis/d5a532db2fc39f8f3a76986cd1efd962 to your computer and use it in GitHub Desktop.
Save kevincennis/d5a532db2fc39f8f3a76986cd1efd962 to your computer and use it in GitHub Desktop.
Proxy coify
'use strict';
const co = require('co');
const toString = Object.prototype.toString;
const genTag = '[object GeneratorFunction]';
const handler = {
get( target, key, receiver ) {
const fn = target[ key ];
const str = toString.call( fn );
if ( str === genTag ) {
return co.wrap( fn );
}
return Reflect.get( target, key, receiver );
}
};
module.exports = function coify( obj ) {
const proxy = new Proxy( obj, handler );
if ( typeof obj === 'function' ) {
proxy.prototype = new Proxy( obj.prototype, handler );
}
return proxy;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment