• Stars
    star
    487
  • Rank 90,352 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.

Microcosm

CircleCI npm npm

Microcosm is a state management tool for React (and similar libraries). Keep track of user actions, cancel requests, and perform optimistic updates with ease.

Heads up

As of July 2019, Microcosm is no longer in active development. We will continue to maintain it for existing clients, and would be happy to accept contributions to continue support in the future.

It's been a great journey, thanks for taking it with us.

What you get

At a glance

import Microcosm, { get, set } from 'microcosm'
import axios from 'axios'

let repo = new Microcosm()

function getUser (id) {
  // This will return a promise. Microcosm automatically handles promises.
  // See http://code.viget.com/microcosm/api/actions.html
  return axios(`/users/${id}`)
}

// Domains define how a Microcosm should turn actions into new state
repo.addDomain('users', {
  getInitialState () {
    return {}
  },
  addUser (users, record) {
    // The set helper non-destructively assigns keys to an object
    return set(users, record.id, record)
  },
  register () {
    return {
      [getUser]: {
        done: this.addUser
      }
    }
  }
})

// Push an action, a request to perform some kind of work
let action = repo.push(getUser, 2)

action.onDone(function () {
  let user = get(repo.state, ['users', '2'])

  console.log(user) // { id: 2, name: "Bob" }
})

// You could also handle errors in a domain's register method
// by hooking into `getUser.error`
action.onError(function () {
  alert("Something went terribly wrong!")
})

Why?

Other Flux implementations treat actions as static events; the result of calling a dispatch method or resolving some sort of data structure like a Promise.

But what if a user gets tired of waiting for a file to upload, or switches pages before a GET request finishes? What if they dip into a subway tunnel and lose connectivity? They might want to retry a request, cancel it, or just see whatโ€™s happening.

The burden of this state often falls on data stores (Domains, in Microcosm) or a home-grown solution for tracking outstanding requests and binding them to related action data. Presentation layer requirements leak into the data layer, making it harder to write tests, reuse code, and accommodate unexpected changes.

How Microcosm is different

Microcosm actions are first-class citizens. An action can move from an open to error state if a request fails. Requests that are aborted may move into a cancelled state. As they change, actions resolve within a greater history of every other action.

This means that applications can make a lot of assumptions about user actions:

  • Actions resolve in a consistent, predictable order
  • Action types are automatically generated
  • Actions maintain the same public API, no matter what asynchronous pattern is utilized (or not)

This reduces a lot of boilerplate, however it also makes it easier for the presentation layer to handle use-case specific display requirements, like displaying an error, performing an optimistic update, or tracking file upload progress.

Get started

npm install --save microcosm

Check out our quickstart guide.

Documentation

Comprehensive documentation can be found in the docs section of this repo.

Overview

Microcosm is an evolution of Flux that makes it easy to manage complicated async workflows and unique data modeling requirements of complicated UIs.

Actions take center stage

Microcosm organizes itself around a history of user actions. As those actions move through a set lifecycle, Microcosm reconciles them in the order they were created.

Invoking push() appends to that history, and returns an Action object to represent it:

// axios is an AJAX library
// https://github.com/mzabriskie/axios
import axios from 'axios'

function getPlanet (id) {
  // axios returns a Promise, handled out of the box
  return axios(`/planets/${id}`)
}

let action = repo.push(getPlanet, 'venus')

action.onDone(function (planet) {
  console.log(planet.id) // venus
})

Domains: Stateless Stores

A Domain is a collection of side-effect free operations for manipulating data. As actions update, Microcosm uses domains to determine how state should change. Old state comes in, new state comes out:

const PlanetsDomain = {
  getInitialState () {
    return []
  },

  addPlanet (planets, record) {
    return planets.concat(record)
  },

  register() {
    return {
      [getPlanet]: this.addPlanet
    }
  }
}

repo.addDomain('planets', PlanetsDomain)

By implementing a register method, domains can subscribe to actions. Each action is assigned a unique string identifier. Action type constants are generated automatically.

Pending, failed, and cancelled requests

Microcosm makes it easy to handle pending, loading, cancelled, completed, and failed requests:

const PlanetsDomain = {
  // ...handlers

  register() {
    return {
      [getPlanet] : {
        open   : this.setPending,
        update : this.setProgress,
        done   : this.addPlanet,
        error  : this.setError,
        cancel : this.setCancelled
      }
    }
  }
}

open, loading, done, error and cancelled are action states. In our action creator, we can unlock a deeper level of control by returning a function:

import request from 'superagent'

function getPlanet (id) {

  return function (action) {
    action.open(id)

    let request = request('/planets/' + id)

    request.end(function (error, response) {
      if (error) {
        action.reject(error)
      } else {
        action.resolve(response.body)
      }
    })

    // Cancellation!
    action.onCancel(request.abort)
  }
}

First, the action becomes open. This state is useful when waiting for something to happen, such as loading. When the request finishes, if it fails, we reject the action, otherwise we resolve it.

Microcosm actions are cancellable. Invoking action.cancel() triggers a cancellation event:

let action = repo.push(getPlanet, 'Pluto')

// Wait, Pluto isn't a planet!
action.cancel()

When action.cancel() is called, the action will move into a cancelled state. If a domain doesn't handle a given state no data operation will occur.

Visit the API documentation for actions to read more.

A historical account of everything that has happened

Whenever an action creator is pushed into a Microcosm, it creates an action to represent it. This gets placed into a tree of all actions that have occurred.

For performance, completed actions are archived and purged from memory, however passing the maxHistory option into Microcosm allows for a compelling debugging story, For example, the time-travelling Microcosm debugger:

let forever = new Microcosm({ maxHistory: Infinity })
Microcosm Debugger

Taken from the Chatbot example.

Optimistic updates

Microcosm will never clean up an action that precedes incomplete work When an action moves from open to done, or cancelled, the historical account of actions rolls back to the last state, rolling forward with the new action states. This makes optimistic updates simpler because action states are self cleaning:

import { send } from 'actions/chat'

const Messages = {
  getInitialState () {
    return []
  },

  setPending(messages, item) {
    return messages.concat({ ...item, pending: true })
  },

  setError(messages, item) {
    return messages.concat({ ...item, error: true })
  },

  addMessage(messages, item) {
    return messages.concat(item)
  }

  register () {
    return {
      [send]: {
        open: this.setPending,
        error: this.setError,
        done: this.addMessage
      }
    }
  }
}

In this example, as chat messages are sent, we optimistically update state with the pending message. At this point, the action is in an open state. The request has not finished.

On completion, when the action moves into error or done, Microcosm recalculates state starting from the point prior to the open state update. The message stops being in a loading state because, as far as Microcosm is now concerned, it never occured.

Forks: Global state, local concerns

Global state management reduces the complexity of change propagation tremendously. However it can make application features such as pagination, sorting, and filtering cumbersome.

How do we maintain the current page we are on while keeping in sync with the total pool of known records?

To accommodate this use case, there is Microcosm::fork:

const UsersDomain = {
  getInitialState() {
    return []
  },
  addUsers(users, next) {
    return users.concat(next)
  },
  register() {
    return {
      [getUsers]: this.addUsers
    }
  }
})

const PaginatedUsersDomain {
  getInitialState() {
    return []
  },
  addUsers(users, next) {
    let page = next.map(user => user.id)

    // Reduce the user list down to only what was included
    // in the current request
    return users.filter(user => page.contains(user.id))
  },
  register() {
    return {
      [getUsers]: this.addUsers
    }
  }
})

let roster = new Microcosm()
let pagination = parent.fork()

roster.addDomain('users', UsersDomain)
pagination.addDomain('users', PaginatedUsersDomain)

// Forks share the same history, so you could also do
// `pagination.push(getUsers, ...)`
roster.push(getUsers, { page: 1 }) // 10 users
roster.push(getUsers, { page: 2 }) // 10 users

// when it finishes...
console.log(roster.state.users.length) // 20
console.log(pagination.state.users.length) // 10

fork returns a new Microcosm, however it shares the same action history. Additionally, it inherits state updates from its parent. In this example, we've added special version of the roster repo that only keeps track of the current page.

As getUsers() is called, the roster will add the new users to the total pool of records. Forks dispatch sequentially, so the child pagination repo is able to filter the data set down to only what it needs.

Networks of Microcosms with Presenters

Fork is an important component of the Presenter addon. Presenter is a special React component that can build a view model around a given Microcosm state, sending it to child "passive view" components.

All Microcosms sent into a Presenter are forked, granting them a sandbox for data operations specific to a particular part of an application:

class PaginatedUsers extends Presenter {
  setup (repo, { page }) {
    repo.add('users', PaginatedUsersDomain)

    repo.push(getUsers, page)
  }

  getModel () {
    return {
      page: state => state.users
    }
  }

  render () {
    const { page } = this.model

    return <UsersTable users={page} />
  }
}

const repo = new Microcosm()
repo.addDomain('users', UsersDomain)

ReactDOM.render(<PaginatedUsers repo={repo} page="1" />, el)

Inspiration


Code At Viget

Visit code.viget.com to see more projects from Viget.

More Repositories

1

blendid

A delicious blend of gulp tasks combined into a configurable asset pipeline and static site builder
JavaScript
4,986
star
2

gulp-rails-pipeline

Ditch the Rails Asset Pipeline and roll your own with Gulp
Ruby
646
star
3

Twitter-Bootstrap-for-Omnigraffle

Omnigraffle stencil, template, and color picker modeled after Twitter Bootstrap
585
star
4

react-ink

A React component for adding material design style ink
JavaScript
325
star
5

colonel-kurtz

A Block Editor
JavaScript
318
star
6

sass-json-vars

Import variables as JSON into Sass
Ruby
294
star
7

grunt-complexity

A JavaScript complexity analysis grunt task.
JavaScript
221
star
8

sprig

Relational seeding for Rails apps
Ruby
208
star
9

jmapping

jQuery plugin for creating Google Maps from semantic markup
JavaScript
207
star
10

olive_branch

Handle camel/snake/dash case conversion
Ruby
180
star
11

acts_as_markup

Represent ActiveRecord Markdown, Textile, or RDoc columns as Markdown, Textile, or RDoc objects using various external libraries to convert to HTML.
Ruby
123
star
12

active_admin_associations

This extends ActiveAdmin to allow for better editing of associations.
Ruby
120
star
13

whitespace-tmbundle

TextMate bundle to remove trailing whitespace & tabs
115
star
14

capistrano_rsync_with_remote_cache

A deployment strategy for Capistrano 2.0 which combines rsync with a remote cache, allowing fast deployments from SCM servers behind firewalls.
Ruby
102
star
15

serialize_with_options

Simple XML and JSON APIs for your Rails app
Ruby
92
star
16

cachebar

A simple API caching layer built on top of HTTParty and Redis
Ruby
88
star
17

redirector

A Rails engine that adds a piece of middleware to the top of your middleware stack that looks for redirect rules stored in your database and redirects you accordingly.
Ruby
83
star
18

react-focus-trap

Traps focus for accessible dropdowns and modal content
JavaScript
81
star
19

stat_board

Simple dashboard of records created this week, this month, and all time, mountable as a Rails Engine.
Ruby
79
star
20

ensure-animation

Ensure animation runs until class loaded
JavaScript
76
star
21

nav_lynx

Rails helper to generate navigation links with a selected class.
Ruby
75
star
22

simplest_auth

Simple implementation of authentication for Rails
Ruby
73
star
23

tailwindcss-plugins

A collection of plugins for Tailwind CSS
JavaScript
58
star
24

interactive-wcag

A filterable and shareable version of the WCAG 2.0 spec.
46
star
25

storyboard

For making ADVENTURES
TypeScript
43
star
26

jquery.transport

Transport elements to other containers based upon media queries.
JavaScript
42
star
27

ruby-string-showdown

Double vs Single Quoted Ruby Strings... Which will emerge victorious??
Ruby
39
star
28

trackomatic

A high-performance tracking enhancement for Google Analytics.
JavaScript
34
star
29

an-isomorphic-chart

Charts are for everyone. A pending blog post.
JavaScript
33
star
30

otp

One-time pad programming exercise
Ruby
31
star
31

ruby_spark

Ruby Gem to make API calls to the Spark Cloud
Ruby
31
star
32

ars-arsenal

A react gallery picker
TypeScript
30
star
33

json-weight

Measure the weight of a JSON endpoint
JavaScript
30
star
34

augmented-card

Example project for ARKit Image Tracking blog post
Swift
29
star
35

simplest_status

Simple status functionality for Rails models.
Ruby
28
star
36

craft-color-swatches

PHP
27
star
37

ca11y

A lightweight accessible dependency-free datepicker
JavaScript
26
star
38

watch-dog

Monit + Sinatra + Monk + SQLite + Mustache = multi-site dowtime alert app
Ruby
25
star
39

jambells

HQ/Boulder Pointless Weekend Project
Elixir
25
star
40

craft-localeredirector

Craft plugin for detecting user language preferences and redirecting to the appropriate locale.
PHP
24
star
41

craft-videoembed

Craft plugin to generate an embed URL from a YouTube or Vimeo URL.
PHP
24
star
42

ffeud

Elixir
23
star
43

platomformio

Integration Atom IDE with PlatformIO
CoffeeScript
23
star
44

jest-with-nightmare

JavaScript
22
star
45

craft-profile

A command-line utility for capturing Craft CMS Profiling output and reporting averages.
JavaScript
22
star
46

sprig-reap

Sprig-Reap is a gem that allows you to output your application's data state to seed files.
Ruby
18
star
47

Screenless

A $55 alternative user interface for rapidly prototyping ideas
C++
16
star
48

foliage

A light, cursor-like tree data structure
JavaScript
15
star
49

canvas-instagram-filters

Demo for an upcoming blog post
JavaScript
15
star
50

simple-benchmark

A simple gem to benchmark spots in your Ruby / Rails code
Ruby
14
star
51

Material-Design-for-Omnigraffle

Omnigraffle stencil modeled after the Material Design sticker sheets
14
star
52

gangway

A client-side API abstraction layer
JavaScript
13
star
53

pointless-feedback

Rails Engine to handle basic user feedback
Ruby
13
star
54

craft-classnames

Craft plugin for conditionally joining css class names together in Twig templates. Really helps with wrangling Tailwind CSS.
PHP
11
star
55

is-it-viget

A demo app for viget.com tutorial
Swift
11
star
56

beginning-react-native

The online home of the SXSW 2016 Beginning iOS Development With React Native Workshop
11
star
57

special-delivery

A webhook event manager for Mailgun
Ruby
10
star
58

d3-builder

Builds D3 as a browserify dependency
JavaScript
10
star
59

checkoning

Visualizating team PR interactions
JavaScript
10
star
60

react-dragon

A very simple drag and drop component. Did I mention it was simple?
JavaScript
10
star
61

diode

An extremely small, single event emitter
JavaScript
9
star
62

prestotype

(WIP) Simple UX prototype boilerplate
JavaScript
9
star
63

craft-site-starter

PHP
8
star
64

craft-viget-base

PHP
7
star
65

slack_alert

A simple button that lights up, plays jingles, and interacts with Slack.
Ruby
7
star
66

rails_polymorphic_select

This is a simple Rails extension that allows you to create polymorphic select inputs for relationships that are polymorphic. It takes advantage of Rails 4.2 Global ID
Ruby
7
star
67

chronolog

Change Tracking for ActiveAdmin
Ruby
7
star
68

sxsw

SXSW Connected Devices Workshop Materials
Arduino
6
star
69

grunt-git-hooks-demo

A sample application to get started using Git hooks with Grunt.
JavaScript
6
star
70

trailbuddy-view-transition

Astro
6
star
71

reword

A tiny translator.
JavaScript
6
star
72

active_versioning

Plug-and-Play Versioning for Rails
Ruby
6
star
73

craft-sidebartoggle

Add the ability to hide the meta sidebar on element entry screens
PHP
5
star
74

gradebook

Example application for the Rails Developer position
5
star
75

VL-Date-Selects

EE plugin to dynamically generated selects for months, days, and years.
PHP
5
star
76

gastropod

Simple library for generating slugs
Ruby
5
star
77

talking-heads

The creepy talking head app.
JavaScript
5
star
78

bode

The bode webserver, written in C
C
5
star
79

washi

A simple, backbone inspired view helper.
JavaScript
5
star
80

microcosm-preact

Microcosm bindings for Preact:
4
star
81

romanize

Roman numeral conversion programming exercise
Ruby
4
star
82

Search-Words

Ruby
4
star
83

inline-svg-sprite

Inlines external svgs sprites and updates `xlink:href` attributes in `<use>` tags to reference the inlined svg.
JavaScript
4
star
84

Style-and-Swatch-Palettes

4
star
85

microcosm-devtools

Developer tools for Microcosm
3
star
86

lights-out

New programming challenge based a game from the 80s
Ruby
3
star
87

VL_CP_Logger

ExpressionEngine extension that logs more actions to the control panel log.
PHP
3
star
88

dnsimple

Python module for interacting with DNSimple service
Python
3
star
89

Device-Asset-Templates

3
star
90

duke-colab-course

Arduino
3
star
91

json-feed-validator

HTML
3
star
92

responsive-page-text

Mixins for LESS and Sass to make all page text โ€”font sizes and line heightsโ€” responsive
CSS
3
star
93

webpack-intro

Intro to Webpacks in Steps
JavaScript
3
star
94

elixir-deploy-quickstart

Quickstart docs and files to get an Elixir app running on production
Elixir
3
star
95

lights-out-vr

A WebVR exploration of Lights Out.
JavaScript
3
star
96

snakey-table-ios

๐Ÿ“ฑ Custom UITableViewCells in iOS
Swift
3
star
97

microcosm-debugger

No longer in service. Use https://github.com/vigetlabs/microcosm-devtools
JavaScript
2
star
98

stimulus-controllers

Astro
2
star
99

gather-content-api

PHP Client for GatherContent API
PHP
2
star
100

wordpress-site-starter

JavaScript
2
star