added onstop(..) callback...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-12-29 02:44:20 +03:00
parent cf15e7c628
commit b47ebfbdc6
2 changed files with 22 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{
"name": "ig-stoppable",
"version": "2.0.1",
"version": "2.0.2",
"description": "Utility library implementing tooling to make stoppable functions...",
"main": "stoppable.js",
"scripts": {

View File

@ -68,7 +68,7 @@ var AsyncGenerator =
// NOTE: this repeats the same code at lest twice, not sure yet how to avoid
// this...
module =
function(func){
function(func, onstop){
return Object.assign(
// NOTE: the below implementations are almost the same, the main
// differences being the respective generator/async mechanics...
@ -85,8 +85,12 @@ function(func){
yield res }
} catch(err){
if(err === module.STOP){
onstop
&& onstop.call(this)
return
} else if(err instanceof module.STOP){
onstop
&& onstop.call(this, err.value)
yield err.value
return }
throw err } }
@ -96,15 +100,23 @@ function(func){
try{
for await(var res of func.call(this, ...arguments)){
if(res === module.STOP){
onstop
&& onstop.call(this)
return }
if(res instanceof module.STOP){
onstop
&& onstop.call(this, err.value)
yield res.value
return }
yield res }
} catch(err){
if(err === module.STOP){
onstop
&& onstop.call(this)
return
} else if(err instanceof module.STOP){
onstop
&& onstop.call(this, err.value)
yield err.value
return }
throw err } }
@ -121,8 +133,12 @@ function(func){
return res
} catch(err){
if(err === module.STOP){
onstop
&& onstop.call(this)
return
} else if(err instanceof module.STOP){
onstop
&& onstop.call(this, err.value)
return err.value }
throw err } }
// function...
@ -137,8 +153,12 @@ function(func){
return res
} catch(err){
if(err === module.STOP){
onstop
&& onstop.call(this)
return
} else if(err instanceof module.STOP){
onstop
&& onstop.call(this, err.value)
return err.value }
throw err } },
{ toString: function(){