Giter Club home page Giter Club logo

crustate's Introduction

Crustate is a message-based modular state-management library for JavaScript applications

npm bundle size Dependencies Build Status Codecov License npm

This library is based on the principles of message passing found in languages like Elm and Elixir/Erlang. The purpose is to be able to build modular state with controlled side-effects through messaging.

State

type State<T, I, M: Message> = {
  name: string,
  init: (init: I) => DataUpdate<T> | MessageUpdate<T>,
  update: (state: T, msg: M) => Update<T>,
  subscriptions: (state: T) => Array<Subscription>,
};

Sates use messages to communicate with other state-instances and the runtime.

Message

type Message = { tag: string };

A message is just plain data, a JavaScript object, with two mandatory properties named tag. The tag is supposed to work as a discriminator, informing the receivers of what type of message it is, what possible data it contains, and what it means.

Note that these messages are to be completely serializable by JSON.stringify to facilitate resumable sever-rendering, logging, history playback, inspection, and other features.

const ADD = "add";

let msg = {
  tag:   ADD,
  value: 2,
};

Update

type StateUpdate = <T, M: Message>(state: T, msg: M) => Update<T>;

Conceptually update is responsible for receiving messages, interpreting them, updating the state, and send new messages in case other components need to be informed or additional data requested.

This is very similar to Redux's Reducer concept with the main difference being that the update-function can send new messages.

import { NONE, updateData } from "crustate";

function update(state, message) {
  switch(message.tag) {
  case ADD:
    return updateData(state + message.value);
  }

  return NONE;
}

Messages sent from the update function are propagated upwards in the state-hierarchy and can be subscribed to in supervising states.

Subscriber

For a state to actually receive messages it first needs to subscribe to messages; which tags it is interested in, if they have any specific requirements, and if it is supposed to be the primary handler for messages of that type.

crustate's People

Contributors

m4rw3r avatar

Watchers

James Cloos avatar Oscar Hillestad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.