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

amnis

v3.0.0

Published

Minimal yet complete state and effect management solution for JavaScript applications

Downloads

9

Readme

✨ Features:

  • Tiny in size.
  • Complete. Handles both state and effects
  • Redux pattern
  • Familiar interface

💡 Motivation

Redux is great. But out of the box it only deals with synchronous actions. We know of a great way to model asynchronous actions - that is with Observables. That is exactly what redux-observable does. But it turns out that if you are armed with observables you can model the whole redux pattern in pretty much one line of code:

// pseudo code
const state$ = action$
  .scan(rootReducer, initialState)

This is not a new idea and many different proposals have been made that replace redux with something like rxjs. However they usually require you to completely change the way you write your redux applications. For example you would have to write actions and reducers as observables, while they are better of staying as pure simple functions. Those approaches are also usually tied to a specific observable library, like rxjs which introduces bloat into your application. With amnis you get to write your redux applications like you're used to. Except without redux. Create your store, actions, and reducers like you always have. But then use any observable library where it makes sense - for effect management. Better yet, you get all of that in 1.7KB gzipped.

🔧 Installation

Assuming you use npm as your package manager:

npm install --save amnis

Then you can use it from Node environment or if you are building for the browser you can use a module bundler like Webpack, Browserify, or Rollup. If you want to experiment and play around with Amnis without a module bundler or you don't use one - that's OK. Amnis npm package includes precompiled production and development UMD builds. You can just drop a UMD build as a <script> tag on a page. The UMD builds make amnis available as a window.amnis.

🔨 Usage

First, let's look at an example that doesn't use effects.

Counter

You start by creating a reducer function just like you would in redux:

export function counter(state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    default:
      return state
  }
}

Then you create a store createStore accepts the rootReducer and an optional initialState parameter. There is no concept of middleware in Amnis since there is no need for it.

import {createStore} from 'amnis'
import {counter} from './counter'

const store = createStore(counter)

It returns an object with 3 properties: state$ - an observable of states action$ - an observable of actions dispatched dispatch - a function to call to dispatch new actions Now we can dispatch actions, subscribe to actions being dispatched, and get notified of every state change.

store.state$.subscribe(state => console.log(state))
store.action$.subscribe(action => console.log(action))

store.dispatch({type: 'INCREMENT'})
store.dispatch({type: 'INCREMENT'})
store.dispatch({type: 'DECREMENT'})

Now let's see how we manage effects.

Effects example

Things to note here: First - reducer is not relevant for this example. Second - we use stream-lite library to work with observables but you don't have to.

 import {createStore, runEffects} from 'amnis'
 import {filter, tap, switchMap, mapTo} from 'stream-lite/operators'
 import {never} from 'stream-lite/statics'
  
 const logAll = action$ => action$.pipe(
	 tap(console.log),
	 switchMap(never),
 )
 
 const pong = action$ => action$.pipe(
	 filter(a => a.type === 'PING'),
	 mapTo(({type: 'PONG'})),
 )
 
 const rootReducer = (state, action) => 'irrelevant'
 
 const store = createStore(rootReducer)

runEffects({
  store,  
  effects: [logAll, pong]
})
 
 store.dispatch({type: 'PING'})

🚀 Choose your own observable library

Internally amnis uses stream-lite. Mostly because it's core is only about 1KB, but also because of it's RxJS-like interface and support for pipeable operators. But you can use any observable library that you want! The observables that Amnis returns implement a [Symbol.observable] method as per Observable proposal specification. All you need to do is supply runEffects with a function it can use to convert a standard observable to an observable of your favorite library:

import {from} from 'rxjs/observable/from'

runEffects({  
  store,  
  effects: [logAll, pong],  
  adapter: from
})

📓 Examples

Stay tuned. Examples are coming.

🙏 License

MIT