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

effed

v0.1.0

Published

Effects library for JavaScript

Downloads

3

Readme

effed

JavaScript library that elevates side-effects to the front line.

Inspired by project like redux and redux-saga effed proposes that idea that the code/business logic can be expressed as a generator yielding effects (which are plain objects) and the run function that interprets those effects.

While inspired by redux the proposed model of computation is generic enough to be used anywhere where generators are supported, in the browser or in node.js environment.

Benefits

  • Defer side-effect
  • Synchronous testing
  • Multiple interpretation of effects
  • Effect composition, creating high-level DSL effects

Quick example


// inside fetch.js
import fetch from 'node-fetch'

// define middleware that will run fetch effects
export const fetchMiddleware = (run) => (next) => (effect) => {
    if (effect.type === 'fetch') {
        return fetch(effect.url, effect.options)
            .then(response => response.json())
    } else {
        return next(effect)
    }
}

// define effect creator function
export const fetch = (url, options = {}) => ({type: 'fetch', url, options})


// inside index.js
import {createRunner} from 'effed'
import {fetchMiddleware, fetch) from './fetch'

// create the run function that is capable of running generators yielding fetch effects
const run = createRunner(fetchMiddleware)

// run a generator function
run(function * () {
    const content1 = yield fetch('http://url1')
    const content2 = yield fetch('http://url2')
    return [content1, content1]
}).then(console.log, console.error)
// => [{...}, {...}] contents of url2 and url2 are returned in a Promise

Concepts

Effect - a plain JavaScript object defining the side-effect requested. The "primitive" effects define some IO operation, e.g. fetch a url, read a file, query database. The "primitive" effects can be combined into higher level effects by either using combinators like parallel, race, sequence, or defining a generator that combines other effects and returns the result.

run - is a function that is able to drive passed in generator until it completes, using a chain of middleware. Generally on a project, there will be one module that export such function, already created with all middleware that a project uses.

run :: Effect -> Promise<any>

middleware - implements the logic of running an effect and returns a promise with the result of the effect. Middlewares are passed to createRunner function, where they are chained internally into a pipeline and used to run each effect. To write a middleware one simply needs to define a function with the following signature:

middleware :: run -> next -> effect -> Promise<effect result>

// inside ./run.js
const myMiddleware = (config) => (run) => (next) => (effect) => {
    if (effectIsForMe(effect)) {
        // can use run function here to run a sub-effect if needed
        return Promise.resolve(someResult)
    } else {
        // continue the chain of processing
        return next(effect)
    }
}
const importedMiddleware from 'importedMiddleware'
export default createRunner(myMiddleware({}), importedMiddleware({}))

Composition of effects

// How do we combine multiple effects? // Option 1: Create larger effects, with its own creators and runners // Option 2: Combine several low level effects using generators

Testing

The major benefit of expressing the logic as a generator emitting effects is the ease of testing.

The scripts themselves are synchronous generators, and the tests just need to verify that they emit correct sequence of effects when fed predefined results of previous effects.

FAQ

Questions

  • how is stack handled? Free monad trampolines computation without using stack, will generators do that?

Write your program using exactly the language you need. Compose your language from smaller orthogonal languages, in a canonical way. Plug in interpreters that support the behavior you want.