luacoro-js
Lua-like pseudo-coroutine for JavaScript/TypeScript using generator. Demo
TOC
- Installation
- Examples
- Error handling
- Golang-like defer
- Functions
- Class Coroutine
- Class ComposedCoroutine
Installation
npm install --save luacoro
// or
Examples
Example code is also displayed in the demo.
Error handling
Can handle errors just like normal functions.
it'handles error',
Golang-like defer
luacoro.defer
works like Golang's defer.
Useful to clean up scene scoped resources.
Defer functions must be normal functions.
Not yield
able within them.
See examples/browser/src/guide.ts for example.
Functions
create
Function create start?: Coroutinizable<T>: Coroutine<T>
Create a new coroutine to iterate start
first.
start
normally must be an iterator generated by a generator
implemented to yield
(or return
) values of the following 3 types:
-
o
: An instance of arbitary class or plainobject
|string
|Array
resume()
returnso
.- If
o
has await
field,resume()
returnsnull
througho.wait - 1
frames after that. The iterator is not resumed while this, which means that this coroutine waitsn
frames including the current frame.
-
n
: Anumber
resume()
returnsnull
.- After that,
resume()
returnsnull
throughn - 1
frames. The iterator is not resumed while this, which means that this coroutine waitsn
frames including the current frame.
-
i
: AnIterator
of the same type asstart
- When
i
isreturn
ed, the current iterator is terminated andi
is immediately started to iterate as the replacement. - When
i
isyield
ed, the current iterator is paused and pushed onto the stack, andi
is immediately started to iterate. Afteri
is terminated, the caller iterator is popped from the stack and continued to be iterated. At this time, the return value ofi
can be got.
- When
concurrent
Function concurrent coroutines: Coroutinizable<T>: ComposedCoroutine<T>
Create a new coroutine to iterate all coroutines
concurrently.
This coroutine will never die.
Additional coroutines can be added by add<T>()
.
Dead coroutines will be removed automatically.
all
Function all coroutines: Coroutinizable<T>: ComposedCoroutine<T>
Create a new coroutine to iterate all coroutines
concurrently until the all of them are dead.
Dead coroutines will not be removed to keep array indexes of the yield
ed value.
Adding coroutines by add<T>()
is discouraged.
race
Function race coroutines: Coroutinizable<T>: ComposedCoroutine<T>
Create a new coroutine to iterate all coroutines
concurrently until one of them is dead.
Array indexes of the yield
ed value will be keeped.
Adding coroutines by add<T>()
is discouraged.
forever
Function forever generator: SimpleGenerator<T>: Coroutine<T>
Create a new coroutine that repeats generating iterator and iterating it forever.
defer
Function defer fn:void
Register fn
to be invoked when exiting the caller iterator.
Works like Golang's defer.
Class Coroutine
resume
Method resumeresumeValue?: T: T
Resume the current iterator and receive the yielded value at the next frame. This method will return nulls forever after the coroutine stops.
stop
Method stop: void
Stop this coroutine.
isAlive
Accessor get isAlive: boolean
Whether this coroutine is alive.
Class ComposedCoroutine
Coroutine that wraps multiple iterators and yields results in an array.
add
Method add coroutine: Coroutinizable<T>
Add a coroutine
to iterate together.