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

effkit

v1.2.1

Published

Algebraic effects in javascript with scoped handlers, multishot delimited continuations, stack safety and do notation

Downloads

10

Readme

Effects.js

Algebraic effects in javascript with scoped handlers, multishot delimited continuations and do notation

How to start?

You can try it out at codesandbox, or install it in npm:

$ npm install effkit

Documentation

You can read the docs here.

What are Algebraic Effects?

Algebraic effects are based on two primary concepts: effects and handlers. Effects are just a representation of an action that can be performed. Handlers will catch the performed effects and can choose to resume the program (continuation) with a result (like a promise), resume the continuation multiple times (like a forEach), or not resume at all and cancel the computation (like an exception). Handlers can also transform the result of the computation (into a promise, an array, etc).

To learn more about algebraic effects, see here.

Algebraic effects bring a multitude of advantages:

  • dependency injection
  • programming in direct-style (like async await - but for any data structure: promises, arrays, generators, etc)
  • maintaining code pure (referentially transparent) while working with effects
  • many control flow constructs can be expressed with only algebraic effects: async/await, coroutines/fibers, generators, exceptions, backtracking, react hooks & suspense, and more
  • combining monads (almost any monad can be represented as an effect, which allows them to be used/chained together)

It's easier to understand what it allows by seeing it in action:

  // write your program in direct style using the generator do notation
  const onUserClick = eff(function* () {
     // get the current request from express
     const auth = yield getAuth() 

     // await for async call
     const user = yield getUser(auth.user.id) 
     
     // throw recoverable exception
     const token = user.token || yield raise(new MissingTokenError())
     
     // for each subscriber in the users list of subscribers
     const subscriber = yield forEach(user.subscribers) 
     
     // await for async call
     const result = yield sendNotification(subscriber, 'clicked', { details: mouseEvent, user, token }) 

     return { user, subscriber, result }
  }) // returns [{ user, subscriber1, result1 }, { user, subscriber2, result2 }, ...], 
     // the return value depends on how you use the handlers 

Handle them later

express.post('actions/user-clicked', (req, res) => {
 const withDependencies = handler({
   getAuth: () => resume(req.auth),
   error: (e) => e instanceof MissingTokenError && cachedUser ? resume(cachedUser) : raise(e),
   sendNotification: (subscriber, type, data) => sendFirebaseNotification(...),
 })
 run(withDependencies(onUserClick))
})

All of the effects (request, getUser, sendNotification, etc) are highly testable, and can be replaced with testing/production/alternative versions.

This library

This library brings a algebraic effects implementation to Javascript using an Action monad, which means you can use the monadic API (map, chain, of), or use generator functions as a "do notation" to make the code look more natural. It is based on the languages Koka and Eff, and tries to bring all the algebraic effects features they have.

Performance

See benchmarks, it is expected to perform better than using native Promises (although they can't really be compared, because Algebraic Effects completely encapsulates Promises and is infinitely more extensible). Still, just like async/await code (or javascript code in general), it should not be used for cpu-heavy computations, only for non-blocking IO.

Stack-safety

It's 100% stack-safe!

Assistance is welcome

Feel free to create PRs or issues about bugs, suggestions, code review, questions, similar projects, improvements, etc. You can also get in contact with me, don't be shy to send a message!

Inspirations

koka

Eff

fx-ts

forgefx

Acknowledgments

Thanks so much to the people who helped me with this library! Thanks to Ohad Kammar for answering all my questions on algebraic effects, and Michael Arnaldi for showing me how to implement custom interpreters in order to achieve stack-safety

Roadmap:

  • ~~Get rid of scope and resume limitations~~
  • ~~Create monadic API~~
  • ~~Add docs~~
  • ~~Make it 100% stack safe~~
  • ~~Benchmarks~~
  • Descriptive errors on dev mode
  • Make a do notation babel plugin to compile the generator into chains
  • Make a typescript version
  • Expose API functions that work only with generators, and API functions that work with raw monads and continuations
  • Create normal handlers and control handlers (like in koka)