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

inu

v3.1.4

Published

composable user interface state and effects manager

Downloads

68

Readme

features

  • minimal size: inu + yo-yo + pull-stream weighs only ~8kb
  • app is a data structure: only need to learn 4 functions, automatically supports plugins
  • architecture is fractal: compose one app from many smaller apps
  • single source of truth: the state of your app is a single object tree
  • state is read-only: update state by dispatching an action, an object describing what happened
  • update with pure functions: updates are handled by a pure function, no magic
  • first-class side effects: initial state or updates can include an effect, an object describing what will happen
  • omakase: consistent flavoring with pull streams all the way down

demos

if you want to share anything using inu, add your thing here!

example

const { start, html, pull } = require('inu')
const delay = require('pull-delay')

const app = {

  init: () => ({
    model: 0,
    effect: 'SCHEDULE_TICK' // start perpetual motion
  }),

  update: (model, action) => {
    switch (action) {
      case 'TICK':
        return {
          model: (model + 1) % 60,
          effect: 'SCHEDULE_TICK'
        }
      default:
        return { model }
    }
  },

  view: (model, dispatch) => html`
    <div class='clock'>
      Seconds Elapsed: ${model}
    </div>
  `,

  run: (effect, sources) => {
    switch (effect) {
      case 'SCHEDULE_TICK':
        return pull(
          pull.values(['TICK']),
          delay(1000)
        )
    }
  }
}

const main = document.querySelector('.main')
const { views } = start(app)

pull(
  views(),
  pull.drain(function (view) {
    html.update(main, view)
  })
)

for a full example of composing multiple apps together, see source and demo.

concepts

imagine your app’s current state is described as a plain object. for example, the initial state of a todo app might look like this:

var initState = {
  model: {
    todos: [{
      text: 'Eat food',
      completed: true
    }, { 
      text: 'Exercise',
      completed: false
    }],
    visibilityFilter: 'SHOW_COMPLETED'
  },
  effect: 'FETCH_TODOS'
}

this state object describes the model (a list of todo items and an option for how to filter these items) and any optional effect (we immediately want to fetch for any new todo items).

to change something in the state, we need to dispatch an action. an action is a plain JavaScript object (notice how we don’t introduce any magic?) that describes what happened. here are a few example actions:

{ type: 'ADD_TODO', text: 'Go to swimming pool' }
{ type: 'TOGGLE_TODO', index: 1 }
{ type: 'SET_VISIBILITY_FILTER', filter: 'SHOW_ALL' }
{ type: 'LOAD_TODOS' }

enforcing that every change is described as an action lets us have a clear understanding of what’s going on in the app. if something changed, we know why it changed. actions are like breadcrumbs of what has happened.

finally, to tie state and actions together, we write an update function. again, nothing magic about it — it’s just a function that takes the model and action as arguments, and returns the next state of the app.

it would be hard to write such a function for a big app, so we write smaller functions managing parts of the state:

function visibilityFilter (model, action) {
  if (action.type === 'SET_VISIBILITY_FILTER') {
    return action.filter
  } else {
    return { model }
  }
}

function todos (model, action) {
  switch (action.type) {
    case 'ADD_TODO':
      return { model: model.concat([{ text: action.text, completed: false }]) }
    case 'TOGGLE_TODO':
      return {
        model: model.map((todo, index) =>
          action.index === index ?
            { text: todo.text, completed: !todo.completed } :
            todo
        )
      }
    case 'LOAD_TODOS':
      return { model, effect: 'FETCH_TODOS' }
    default:
      return { model }
  }
}

and we write another update function that manages the complete state of our app by calling those two update functions for the corresponding state keys:

function appUpdate (model, action) {
  const todosState = todos(model.todos, action)
  const visibilityFilterState = visibilityFilter(model.visibilityFilter, action)

  return {
    model: {
      todos: todosState.model,
      visibilityFilter: visibilityFilter.model
    },
    effect: todosState.effect
  }
}

if any effect is returned by an update function, we want to run it. this run functions is able to listen to any future changes and return a stream of any new actions.

here's how we handle our effect to fetch any todos, using pull-stream as pull:

function appRun (effect, sources) {
  if (effect === 'FETCH_TODOS') {
    return pull(
      fetchTodos(),
      pull.map(todo => {
        return {
          type: 'ADD_TODO',
          text: todo.text
        }
      })
    )
  }
}

now that we have our state, changes, and side effects managed in a predictable (and easy-to-test) way, we want to view our epic todo list.

here's a simplified view using yo-yo as html:

function appView (model, dispatch) {
  return html`
    <div class='todos'>
      ${model.todos.map((todo, index) => html`
        <div class='todo'>
          ${todo.text}
          <button onclick=${toggleTodo(index)}
        </div>
      `)}
    </div>
  `

  function toggleTodo (index) {
    return (ev) => dispatch({ 'TOGGLE_TODO', })
  }
}

put it all together and we have an inu app!

const app = {
  init: () => initState,
  update: appUpdate,
  view: appView,
  run: appRun
}

that's it for inu. note that we're only using plain functions and objects. inu (and inux) come with a few utilities to facilitate this pattern, but the main idea is that you describe how your state is updated over time in response to action objects, and 90% of the code you write is just plain JavaScript, with no use of inu itself, its APIs, or any magic.

(credit @gaearon of redux for initial source of this intro)

api

where state is an object with a required key model and an optional key effect,

an inu app is defined by an object with the following (optional) keys:

  • init: a function returning the initial state
  • update: a update(model, action) pure function, returns the new state
  • view: a view(model, dispatch) pure function, returns the user interface declaration
  • run: a run(effect, sources) function, returns an optional pull source stream of future actions

inu = require('inu')

the top-level inu module is a grab bag of all inu/* modules.

you can also require each module separately like require('inu/start').

sources = inu.start(app)

sources is an object with the following keys:

streams flow diagram

* in this context, state-ful means that the pull source stream will always start with the last value (if any) first.

inu.html === require('yo-yo') (for templating, virtual DOM "diffing")

inu.pull === require('pull-stream') (for async event "piping")

install

npm install --save inu

inspiration

license

The Apache License

Copyright © 2016 Michael Williams

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.