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

eff.js

v2.0.0

Published

One-Shot Algebric Effects on JavaScript Generators

Downloads

2

Readme

eff.js

One-Shot Algebric Effects on JavaScript Generators

It is JavaScript port of eff.lua. Many thanks to @Nymphium!

Build Status Coverage NPM version

Install

NPM:

$ npm install eff.js

Yarn:

$ yarn add eff.js

Example

import {inst, handler, combineHandlers, execute} from 'eff.js';

// Creates effect instances.
const write = inst('write');
const httpGet = inst('httpGet');

const main = async function* () {
  // Invokes effects.
  yield write('hello world');
  yield write((yield httpGet('https://example.com')).replace(/\n/, '⏎').slice(0, 20) + '...');
};
// NOTE: An async generator function `main` is pure in fact.
// e.g. `await main().next()` evaluates `{ value: {eff: inst%0(write), values: ['hello world']}, done: false}`
// without any effects.

// Defines handlers for each effect.
const handleWrite = handler(
  // A target effect instance:
  write,
  // A return callback function:
  // It is called when `main` is finished.
  async function* (v) {
    return v;
  },
  // An effect handler:
  // It is called on each `yield write(text)` with the one-shot continuation after this
  // and `text` value.
  async function* (k, text) {
    console.log(text);
    return yield* k();
  },
);
const handleWriteWithTimestamp = handler(
  write,
  async function* (v) {
    return v;
  },
  async function* (k, text) {
    console.log(new Date(), text);
    return yield* k();
  }
);
const handleHttpGet = handler(
  httpGet,
  async function* (v) {
    return v;
  },
  async function* (k, url) {
    const text = await (await fetch(url)).text();
    return yield* k(text);
  },
);

// Runs them.
(async () => {
  console.log('write + httpGet:');
  await execute(combineHandlers(handleWrite, handleHttpGet)(main));
  console.log();

  console.log('write with timestamp + httpGet:');
  await execute(combineHandlers(handleWriteWithTimestamp, handleHttpGet)(main));
  console.log();
})();

// Outputs:
// write + httpGet:
// hello world
// <!doctype html>⏎<htm...
//
// write with timestamp + httpGet:
// 2019-03-12T16:53:34.344Z 'hello world'
// 2019-03-12T16:53:35.133Z '<!doctype html>⏎<htm...'

API

See API.md.

Technical Note

What's difference between this and eff.lua?

eff.lua is built on Lua's asymmetric coroutines. It is more powerful than JavaScript generators: Lua's one can do yield from anywhere, but JavaScript's one can do yield only from generator functions (function *). Thus, eff.js is less convenient than eff.lua. However this porting is interesting and important technically.

How to emulate stackful coroutine on stackless coroutine.

According to Moura et al [1], both of coroutines call asymmetric. Lua's coroutine calls stackful that can yield over call stacks, and JavaScript generator calls stackless on the other hand. In general, stackful coroutine is more powerful than stackless one. So, it is important how to emulate stackful coroutine on stackless coroutine.

Of course, it is impossible that usual stackless corutine emulates stackful coroutine. Therefore, considering restricted JavaScript is needed. This JavaScript has only generator functions, but generator operations are exception, then relations between asymmetric coroutine operation and JavaScript shows the following table:

| | Asymmetric coroutine | JavaScript | |---------------------|----------------------|--------------| | Creates a coroutine | co = create f | co = f() | | Calls a function | f() | yield* f() | | Yields a coroutine | yield v | yield v | | Resumes a coroutine | resume co v | co.next(v) |

I implemented eff.js in accordance with this table. The interesting point is vh of handler function is generator function on eff.js. It is important to work resend invocation correctly.

Reference

  1. Moura, Ana Lúcia De, and Roberto Ierusalimschy. "Revisiting coroutines." ACM Transactions on Programming Languages and Systems (TOPLAS) 31.2 (2009): 6.

License

MIT

(C) 2019 TSUYUSATO "MakeNowJust" Kitsune