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", "name": "ig-stoppable",
"version": "2.0.1", "version": "2.0.2",
"description": "Utility library implementing tooling to make stoppable functions...", "description": "Utility library implementing tooling to make stoppable functions...",
"main": "stoppable.js", "main": "stoppable.js",
"scripts": { "scripts": {

View File

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