From 67c99e06f08dd6c509ff061f0d9a960acca1994f Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 29 Dec 2022 03:01:13 +0300 Subject: [PATCH] now onstop(..) gets most of the "call context"... Signed-off-by: Alex A. Naanou --- package.json | 2 +- stoppable.js | 33 ++++++++++++++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 89c8101..4ed56f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-stoppable", - "version": "2.0.3", + "version": "2.0.4", "description": "Utility library implementing tooling to make stoppable functions...", "main": "stoppable.js", "scripts": { diff --git a/stoppable.js b/stoppable.js index 6306859..9c016dc 100644 --- a/stoppable.js +++ b/stoppable.js @@ -67,7 +67,6 @@ var AsyncGenerator = // // NOTE: this repeats the same code at lest twice, not sure yet how to avoid // this... -// XXX need to give onstop(..) access to the actual call context... module = function(func, onstop){ return Object.assign( @@ -80,22 +79,22 @@ function(func, onstop){ for(var res of func.call(this, ...arguments)){ if(res === module.STOP){ onstop - && onstop.call(this) + && onstop.call(this, res, ...arguments) return } if(res instanceof module.STOP){ onstop - && onstop.call(this, res.value) + && onstop.call(this, res.value, ...arguments) yield res.value return } yield res } } catch(err){ if(err === module.STOP){ onstop - && onstop.call(this) + && onstop.call(this, err, ...arguments) return } else if(err instanceof module.STOP){ onstop - && onstop.call(this, err.value) + && onstop.call(this, err.value, ...arguments) yield err.value return } throw err } } @@ -106,22 +105,22 @@ function(func, onstop){ for await(var res of func.call(this, ...arguments)){ if(res === module.STOP){ onstop - && onstop.call(this) + && onstop.call(this, res, ...arguments) return } if(res instanceof module.STOP){ onstop - && onstop.call(this, res.value) + && onstop.call(this, res.value, ...arguments) yield res.value return } yield res } } catch(err){ if(err === module.STOP){ onstop - && onstop.call(this) + && onstop.call(this, err, ...arguments) return } else if(err instanceof module.STOP){ onstop - && onstop.call(this, err.value) + && onstop.call(this, err.value, ...arguments) yield err.value return } throw err } } @@ -133,21 +132,21 @@ function(func, onstop){ // NOTE: this is here for uniformity... if(res === module.STOP){ onstop - && onstop.call(this) + && onstop.call(this, res, ...arguments) return } if(res instanceof module.STOP){ onstop - && onstop.call(this, res.value) + && onstop.call(this, res.value, ...arguments) return res.value } return res } catch(err){ if(err === module.STOP){ onstop - && onstop.call(this) + && onstop.call(this, err, ...arguments) return } else if(err instanceof module.STOP){ onstop - && onstop.call(this, err.value) + && onstop.call(this, err.value, ...arguments) return err.value } throw err } } // function... @@ -157,21 +156,21 @@ function(func, onstop){ // NOTE: this is here for uniformity... if(res === module.STOP){ onstop - && onstop.call(this) + && onstop.call(this, res, ...arguments) return } if(res instanceof module.STOP){ onstop - && onstop.call(this, res.value) + && onstop.call(this, res.value, ...arguments) return res.value } return res } catch(err){ if(err === module.STOP){ onstop - && onstop.call(this) + && onstop.call(this, err, ...arguments) return } else if(err instanceof module.STOP){ onstop - && onstop.call(this, err.value) + && onstop.call(this, err.value, ...arguments) return err.value } throw err } }, { toString: function(){