npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

luacoro

v2.2.4

Published

Lua-like pseudo-coroutine that wraps iterators.

Downloads

21

Readme

luacoro-js

Build Status npm Dependency Status MIT License

Lua-like pseudo-coroutine for JavaScript/TypeScript using generator. Demo

TOC

Installation

npm install --save luacoro
import * as luacoro from 'luacoro'
// or
const luacoro = require('luacoro')

Examples

See examples/browser/src/.

Example code is also displayed in the demo.

Error handling

Can handle errors just like normal functions.

See src/index.spec.ts

it('handles error', () => {
  let result = ''

  function* second (): luacoro.Iterator<{}> {
    throw new Error('an error')
  }

  function* first (): luacoro.Iterator<{}> {
    try {
      yield second()
    } catch (e) {
      result += 'caught '
    }
    yield second()
  }

  const c = luacoro.create(first())
  try {
    c.resume()
  } catch (e) {
    result += e.message
  }
  expect(result).toEqual('caught an 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 yieldable within them.

See examples/browser/src/guide.ts for example.

function addClickEffect () {
  coro.add(function* (): luacoro.Iterator<{}> {
    const e = document.createElement('div')
    e.classList.add('guide-click-effect')
    document.getElementById('guide').appendChild(e)
    luacoro.defer(() => {
      e.remove()
    })
    e.style.left = `${lastCursorPos.x}px`
    e.style.top = `${lastCursorPos.y}px`

    for (let i = 1; i <= clickEffectFrames; i++) {
      const s = easingOut(i / clickEffectFrames)
      e.style.transform = `translate(-50%, -50%) scale(${s}, ${s})`
      e.style.opacity = `${1 - s}`
      yield
    }
  })
}

Functions

Function create

create<T> (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 plain object | string | Array

    • resume() returns o.
    • If o has a wait field, resume() returns null through o.wait - 1 frames after that. The iterator is not resumed while this, which means that this coroutine waits n frames including the current frame.
  • n: A number

    • resume() returns null.
    • After that, resume() returns null through n - 1 frames. The iterator is not resumed while this, which means that this coroutine waits n frames including the current frame.
  • i: An Iterator of the same type as start

    • When i is returned, the current iterator is terminated and i is immediately started to iterate as the replacement.
    • When i is yielded, the current iterator is paused and pushed onto the stack, and i is immediately started to iterate. After i is terminated, the caller iterator is popped from the stack and continued to be iterated. At this time, the return value of i can be got.

Function concurrent

concurrent<T> (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.

Function all

all<T> (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 yielded value. Adding coroutines by add<T>() is discouraged.

Function race

race<T> (coroutines: Coroutinizable<T>[]): ComposedCoroutine<T>

Create a new coroutine to iterate all coroutines concurrently until one of them is dead.

Array indexes of the yielded value will be keeped. Adding coroutines by add<T>() is discouraged.

Function forever

forever<T> (generator: SimpleGenerator<T>): Coroutine<T>

Create a new coroutine that repeats generating iterator and iterating it forever.

Function defer

defer (fn: () => void)

Register fn to be invoked when exiting the caller iterator. Works like Golang's defer.

Class Coroutine

Method resume

resume(resumeValue?: 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.

Method stop

stop(): void

Stop this coroutine.

Accessor isAlive

get isAlive(): boolean

Whether this coroutine is alive.

Class ComposedCoroutine

Coroutine that wraps multiple iterators and yields results in an array.

Method add

add (coroutine: Coroutinizable<T>)

Add a coroutine to iterate together.