mirror of
https://github.com/flynx/stoppable.js.git
synced 2025-10-29 19:00:08 +00:00
added async function support + docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
21a343b5d2
commit
7d9b494305
94
README.md
94
README.md
@ -1,6 +1,23 @@
|
|||||||
# stoppable.js
|
# stoppable.js
|
||||||
Utility library implementing tooling to make stoppable functions...
|
Utility library implementing tooling to make stoppable functions...
|
||||||
|
|
||||||
|
|
||||||
|
- [stoppable.js](#stoppablejs)
|
||||||
|
- [Install / import](#install--import)
|
||||||
|
- [Components](#components)
|
||||||
|
- [`stoppable(..)`](#stoppable)
|
||||||
|
- [`stoppable.STOP / stoppable.STOP(..)`](#stoppablestop--stoppablestop)
|
||||||
|
- [Examples](#examples)
|
||||||
|
- [Function](#function)
|
||||||
|
- [Async function](#async-function)
|
||||||
|
- [Generator](#generator)
|
||||||
|
- [Async generator](#async-generator)
|
||||||
|
- [License](#license)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Install / import
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ npm install --save ig-stoppable
|
$ npm install --save ig-stoppable
|
||||||
```
|
```
|
||||||
@ -10,6 +27,52 @@ var stoppable = require('ig-stoppable')
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Components
|
||||||
|
|
||||||
|
### `stoppable(..)`
|
||||||
|
|
||||||
|
Create a wrapper for the input function/generator.
|
||||||
|
|
||||||
|
```bnf
|
||||||
|
stoppable(<function>)
|
||||||
|
-> <function>
|
||||||
|
|
||||||
|
stoppable(<async-function>)
|
||||||
|
-> <async-function>
|
||||||
|
|
||||||
|
stoppable(<generator>)
|
||||||
|
-> <generator>
|
||||||
|
|
||||||
|
stoppable(<async-generator>)
|
||||||
|
-> <async-generator>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### `stoppable.STOP / stoppable.STOP(..)`
|
||||||
|
|
||||||
|
A special object/constructor that can be either returned/thrown _as-is_ or
|
||||||
|
used to create an _instance_ to be returned thrown.
|
||||||
|
|
||||||
|
```bnf
|
||||||
|
stoppable.STOP
|
||||||
|
|
||||||
|
stoppable.STOP()
|
||||||
|
stoppable.STOP(<value>)
|
||||||
|
-> <stop-object>
|
||||||
|
```
|
||||||
|
|
||||||
|
This will get intercepted by `stoppable(..)` and appropriately handled merging
|
||||||
|
it into the return/yield value and stopping the function/iterator.
|
||||||
|
|
||||||
|
`<stop-object>` can contain a value that will get handled by `stoppable(..)` (default: `undefined`).
|
||||||
|
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Function
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var func = stoppable(function(){
|
var func = stoppable(function(){
|
||||||
// ...
|
// ...
|
||||||
@ -22,6 +85,23 @@ var func = stoppable(function(){
|
|||||||
var value = func() // -> 'something'
|
var value = func() // -> 'something'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Async function
|
||||||
|
```javascript
|
||||||
|
var func = stoppable(async function(){
|
||||||
|
// ...
|
||||||
|
|
||||||
|
throw stoppable.STOP('something')
|
||||||
|
|
||||||
|
// ...
|
||||||
|
})
|
||||||
|
|
||||||
|
var value = await func() // -> 'something'
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Generator
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var gen = stoppable(function*(){
|
var gen = stoppable(function*(){
|
||||||
// ...
|
// ...
|
||||||
@ -34,6 +114,9 @@ var gen = stoppable(function*(){
|
|||||||
var value = [...gen()] // -> ['somthing']
|
var value = [...gen()] // -> ['somthing']
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Async generator
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var agen = stoppable(async function*(){
|
var agen = stoppable(async function*(){
|
||||||
// ...
|
// ...
|
||||||
@ -45,3 +128,14 @@ var agen = stoppable(async function*(){
|
|||||||
|
|
||||||
var value = awat agen() // -> ['something']
|
var value = awat agen() // -> ['something']
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[BSD 3-Clause License](./LICENSE)
|
||||||
|
|
||||||
|
Copyright (c) 2022-, Alex A. Naanou,
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
|
||||||
|
<!-- vim:set ts=4 sw=4 spell : -->
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-stoppable",
|
"name": "ig-stoppable",
|
||||||
"version": "2.0.0",
|
"version": "2.0.1",
|
||||||
"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": {
|
||||||
|
|||||||
29
stoppable.js
29
stoppable.js
@ -18,6 +18,9 @@
|
|||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// helpers...
|
// helpers...
|
||||||
|
|
||||||
|
var AsyncFunction =
|
||||||
|
(async function(){}).constructor
|
||||||
|
|
||||||
var Generator =
|
var Generator =
|
||||||
(function*(){}).constructor
|
(function*(){}).constructor
|
||||||
|
|
||||||
@ -66,10 +69,9 @@ var AsyncGenerator =
|
|||||||
module =
|
module =
|
||||||
function(func){
|
function(func){
|
||||||
return Object.assign(
|
return Object.assign(
|
||||||
|
// NOTE: the below implementations are almost the same, the main
|
||||||
|
// differences being the respective generator/async mechanics...
|
||||||
func instanceof Generator ?
|
func instanceof Generator ?
|
||||||
// NOTE: the only difference between Generator/AsyncGenerator
|
|
||||||
// versions of this is the async keyword -- keep them
|
|
||||||
// in sync...
|
|
||||||
function*(){
|
function*(){
|
||||||
try{
|
try{
|
||||||
for(var res of func.call(this, ...arguments)){
|
for(var res of func.call(this, ...arguments)){
|
||||||
@ -87,12 +89,9 @@ function(func){
|
|||||||
return }
|
return }
|
||||||
throw err } }
|
throw err } }
|
||||||
: func instanceof AsyncGenerator ?
|
: func instanceof AsyncGenerator ?
|
||||||
// NOTE: the only difference between Generator/AsyncGenerator
|
|
||||||
// versions of this is the async keyword -- keep them
|
|
||||||
// in sync...
|
|
||||||
async function*(){
|
async function*(){
|
||||||
try{
|
try{
|
||||||
for(var res of func.call(this, ...arguments)){
|
for await(var res of func.call(this, ...arguments)){
|
||||||
if(res === module.STOP){
|
if(res === module.STOP){
|
||||||
return }
|
return }
|
||||||
if(res instanceof module.STOP){
|
if(res instanceof module.STOP){
|
||||||
@ -106,6 +105,22 @@ function(func){
|
|||||||
yield err.value
|
yield err.value
|
||||||
return }
|
return }
|
||||||
throw err } }
|
throw err } }
|
||||||
|
: func instanceof AsyncFunction ?
|
||||||
|
async function(){
|
||||||
|
try{
|
||||||
|
var res = await func.call(this, ...arguments)
|
||||||
|
// NOTE: this is here for uniformity...
|
||||||
|
if(res === module.STOP){
|
||||||
|
return }
|
||||||
|
if(res instanceof module.STOP){
|
||||||
|
return res.value }
|
||||||
|
return res
|
||||||
|
} catch(err){
|
||||||
|
if(err === module.STOP){
|
||||||
|
return
|
||||||
|
} else if(err instanceof module.STOP){
|
||||||
|
return err.value }
|
||||||
|
throw err } }
|
||||||
: function(){
|
: function(){
|
||||||
try{
|
try{
|
||||||
var res = func.call(this, ...arguments)
|
var res = func.call(this, ...arguments)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user