You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Desperately needed node.js domain support and so.... I somewhat blindy (and naively to a certain extent) applied the fix suggested here nodejs/node-v0.x-archive/issues/8648#issuecomment-61082686
as per below (using a slighly date version of co btw). Seems to have done the trick for everything I have tested so far. This isn't my cup of tea per se.. some of this stuff is mind bending at times. ;) I am sure I have missed something... toPromise, thunkToPromise and co.wrap's internal onFulfilled and onRejected all probably need to apply or be domain bound too, no?
Is this something that would be applicable to be incorporated? :)
/** * slice() reference. */varslice=Array.prototype.slice;/** * Expose `co`. */module.exports=co['default']=co.co=co;/** * Wrap the given generator `fn` into a * function that returns a promise. * This is a separate function so that * every `co()` call doesn't create a new, * unnecessary closure. * * @param {GeneratorFunction} fn * @return {Function} * @api public */functiondomainBind(fn){if(process.domain&&typeoffn==='function'){fn=process.domain.bind(fn)}returnfn}co.wrap=function(fn){fn=domainBind(fn)varret=function(){returnco.call(this,fn.apply(this,arguments));};ret=domainBind(ret);returnret;};/** * Execute the generator function or a generator * and return a promise. * * @param {Function} fn * @return {Function} * @api public */functionco(gen){gen=domainBind(gen);varctx=this;if(typeofgen==='function')gen=gen.call(this);// we wrap everything in a promise to avoid promise chaining,// which leads to memory leak errors.// see https://github.com/tj/co/issues/180varret=newPromise(function(resolve,reject){onFulfilled();/** * @param {Mixed} res * @return {Promise} * @api private */functiononFulfilled(res){varret;try{ret=gen.next(res);}catch(e){returnreject(e);}next(ret);}/** * @param {Error} err * @return {Promise} * @api private */functiononRejected(err){varret;try{ret=gen.throw(err);}catch(e){returnreject(e);}next(ret);}/** * Get the next value in the generator, * return a promise. * * @param {Object} ret * @return {Promise} * @api private */functionnext(ret){if(ret.done)returnresolve(ret.value);varvalue=toPromise.call(ctx,ret.value);if(value&&isPromise(value))returnvalue.then(onFulfilled,onRejected);returnonRejected(newTypeError('You may only yield a function, promise, generator, array, or object, '+'but the following object was passed: "'+String(ret.value)+'"'));}});varoThen=ret.thenret.then=function(success,err){success=domainBind(success);err=domainBind(err);oThen.call(ret,success,err)}returnret;}/** * Convert a `yield`ed value into a promise. * * @param {Mixed} obj * @return {Promise} * @api private */functiontoPromise(obj){if(!obj)returnobj;if(isPromise(obj))returnobj;if(isGeneratorFunction(obj)||isGenerator(obj))returnco.call(this,obj);if('function'==typeofobj)returnthunkToPromise.call(this,obj);if(Array.isArray(obj))returnarrayToPromise.call(this,obj);if(isObject(obj))returnobjectToPromise.call(this,obj);returnobj;}/** * Convert a thunk to a promise. * * @param {Function} * @return {Promise} * @api private */functionthunkToPromise(fn){varctx=this;returnnewPromise(function(resolve,reject){fn.call(ctx,function(err,res){if(err)returnreject(err);if(arguments.length>2)res=slice.call(arguments,1);resolve(res);});});}/** * Convert an array of "yieldables" to a promise. * Uses `Promise.all()` internally. * * @param {Array} obj * @return {Promise} * @api private */functionarrayToPromise(obj){returnPromise.all(obj.map(toPromise,this));}/** * Convert an object of "yieldables" to a promise. * Uses `Promise.all()` internally. * * @param {Object} obj * @return {Promise} * @api private */functionobjectToPromise(obj){varresults=newobj.constructor();varkeys=Object.keys(obj);varpromises=[];for(vari=0;i<keys.length;i++){varkey=keys[i];varpromise=toPromise.call(this,obj[key]);if(promise&&isPromise(promise))defer(promise,key);elseresults[key]=obj[key];}returnPromise.all(promises).then(function(){returnresults;});functiondefer(promise,key){// predefine the key in the resultresults[key]=undefined;promises.push(promise.then(function(res){results[key]=res;}));}}/** * Check if `obj` is a promise. * * @param {Object} obj * @return {Boolean} * @api private */functionisPromise(obj){return'function'==typeofobj.then;}/** * Check if `obj` is a generator. * * @param {Mixed} obj * @return {Boolean} * @api private */functionisGenerator(obj){return'function'==typeofobj.next&&'function'==typeofobj.throw;}/** * Check if `obj` is a generator function. * * @param {Mixed} obj * @return {Boolean} * @api private */functionisGeneratorFunction(obj){varconstructor=obj.constructor;varproto=constructor.prototype;varname=constructor.displayName||constructor.name;varnameLooksRight='GeneratorFunction'==name;varmethodsLooksRight='function'==typeofproto.next&&'function'==typeofproto.throw;returnnameLooksRight||methodsLooksRight;}/** * Check for plain object. * * @param {Mixed} val * @return {Boolean} * @api private */functionisObject(val){returnObject==val.constructor;}
The text was updated successfully, but these errors were encountered:
Desperately needed node.js domain support and so.... I somewhat blindy (and naively to a certain extent) applied the fix suggested here nodejs/node-v0.x-archive/issues/8648#issuecomment-61082686
as per below (using a slighly date version of co btw). Seems to have done the trick for everything I have tested so far. This isn't my cup of tea per se.. some of this stuff is mind bending at times. ;) I am sure I have missed something... toPromise, thunkToPromise and co.wrap's internal onFulfilled and onRejected all probably need to apply or be domain bound too, no?
Is this something that would be applicable to be incorporated? :)
The text was updated successfully, but these errors were encountered: