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

affx-affects

v0.3.0

Published

AffX Asynchronous Effects

Downloads

7

Readme

AffX

AffX is a micro state manager heavily inspired by Elm, PureScript, and Redux.

Embracing the Micro Frontends (and Elm) phylosophy, it allows to easily handle state at the Component level, rather than at the application level.

AffX' syntax and concepts being very close from the Redux' ones, it's very easy to grasp. AffX' update function allows to return "commands" additionally to the state to perform side effects (such as fetch, Date, etc...), it's therefore strongly adviced to take a look at TEA if you don't feel confortable with the concept.

The library is written in TypeScript for more fun and benefits :)

For a complete example see here

Installation

npm install affx

or

yarn add affx

A simple example using React and react-affx

The Actions and State types

// The Action type is defined by AffX
type CounterActions = Action<"INCREMENT"> | Action<"DECREMENT">;

interface CounterState {
  counter: number;
}

const initialState: CounterState = { counter: 0 };

The Component

// The WithAffxProps type is available in react-affx
const AffxLessCounter: React.StatelessComponent<
  OwnProps & WithAffxProps<CounterState, CounterActions>
> = ({ dispatch, state }) => (
  <div>
    <button onClick={dispatch.always({ type: "INCREMENT" })}>+</button>
    <button onClick={dispatch.always({ type: "DECREMENT" })}>-</button>
    <strong>{state.counter}</strong>
  </div>
);

// We can now use the withAffx HOC defined by react-affx
// The update function is defined below
const Counter = withAffx(initialState, update)(Counter);

The Update function

// The Update type defined by AffX will enforce the type of our update function
const update: Update<CounterState, CounterActions> = action => state => {
  switch (action.type) {
    case "INCREMENT": {
      return { state: { ...state, counter: state.counter + 1 } };
    }

    case "DECREMENT": {
      return { state: { ...state, counter: state.counter - 1 } };
    }

    default: {
      // AffX will NOT render our React component if no changes have been performed on the state
      return { state };
    }
  }
};

The asynchronous effects (affects)

Where AffX shines is how it handles the side effects. Let's say we want to add a button in our Counter Component that sends the counter state to a Rest API. Depending on the UI framework we use (here React), the solution may be different, but we would have to call an injected service, or an imported function, to handle the asynchronicity ourself, in a somewhat heterogeneous manner, and mostly inside our Component.

Not with AffX:

  // We need to add a new Action to our CounterActions type
  type CounterActions = ... | Action<"SEND_TO_SERVER">;

  // We just add this button in our render method
  <button onClick={this.dispatch.always({ type: "SEND_TO_SERVER" })}>

  // And finally we may update our "update" function (pun intended)
  // ...
  case "SEND_TO_SERVER": {
    // ajax is defined in the affx-affects library and relies on fetch to perform the requests
    const sendToServer = ajax(
      "https://www.my-super-rest-api/counters/", // URL
      "json", // Method used to decode the Response
      { body: state.counter.toString()}, // Any Fetch options
    );

    return {
      state,
      commands: [sendToServer(/* an actionCreator has to be given to sendToServer */)]
    };
  }
  // ...

And voilà!

Additional words

Of course, we may update our state while we send "commands", we therefore may add some "loading" attribute to our state while sending the counter state to the API.

Also, the actionCreator we pass to sentToServer will be called once the Request is over (whether it failed or not), then its result will be pass to the dispatcher method of our Component. The whole logic disappears from the Component, however, our update function remains pure!