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

mobx-state-class

v0.1.4

Published

mobx tucked away

Downloads

6

Readme

mobx-state-class

mobx-state-class is a state management lib built on top of the awesome MobX lib, heavily inspired by mobx-state-tree project.

Introduction

mobx-state-class (abbr. MSC) is just a syntax sugar layer to the original MobX APIs. Thus familiarity with MobX is a prerequisite.

MSC is built around MobX's class decorators. By extending MSC's ReactiveNode base class, it automatically applies @observable to all class properties, @computed to all getters, and @action to all methods.

This effectively frees you from the worry of forgetting to apply the correct decorator from time to time, which is a typical gotcha when working with MobX. Such approach lets you focus more on your own business logics, and your code also looks cleaner without all those annotations.

By design MSC embraces a convention-over-configuration approach. It offers no way to configure at all. Anytime you feel you need some fine grain controls, just turn back to original MobX APIs. Remember, MSC is just syntax sugar.

Requirement

MSC is implemented with ES7 Proxy, and is compatible with MobX v5+ only, you need to ensure MobX exists in your project as peer dependency.

Usage

Basic Usage

MSC assumes you write your state tree node with ES6 class syntax. It provides a ReactiveNode base class for you to extend. You just need to extend ReactiveNode, forget about MobX and write your code like normal js.

class Todo extends ReactiveNode {
  constructor(initialState) {
    const { todos } = initialState;
    super();
    this.todos = todos;
  }

  get completedTodos() {
    return this.todos.filter(todo => todo.completed);
  }

  addTodo(title) {
    this.todos.push({ title, completed: false });
  }

  async fetch(params) {
    this.todos = await api.fetchTodos(params);
  }
}

Above code is (almost) equivalent to below.

class Todo {
  @observable todos;

  constructor(initialState) {
    const { todos } = initialState;
    this.todos = todos;
  }

  @computed
  get completedTodos() {
    return this.todos.filter(todo => todo.completed);
  }

  @action.bound
  addTodo(title) {
    this.todos.push({ title, completed: false });
  }

  fetch: mobx.flow(function* (params) {
    this.todos = yield api.fetchTodos(params);
  })
}

As seen in example, class methods are automatically converted to bound actions, and async function is also taken cared (this trick actually involves hacking Promise.prototype.then, it's not clean, but worth it).

All conversions happen in runtime, no transpiler required.

Tree Node Properties

MSC borrows the concept of "tree" from mobx-state-tree project. It provides a set of lightweight APIs to help you work with tree relationship.

ReactiveNode class implements 4 hidden properties (actually they are getters):

  • .parent
  • .path
  • .root
  • .isRoot

Tree node object's class definition typically locates in seperate files. These properties help you access other related node from within one node without the need to import that file.

If you organize your state tree node in a single-state-tree fashion, then you can easily get the global app state by accessing .root.

Example:

// app-state.js
import Todo from "todo";

class AppState extends ReactiveNode {
  constructor() {
    super();
    this.todos = [new Todos()];
  }

  printIsRoot() {
    console.log(this.isRoot); // "true"
  }

  printPath() {
    console.log(this.path); // ""
  }

  printParent() {
    console.log(this.parent); // will log "null"
  }

  printAppState() {
    console.log(this.root); // will log appState itself
  }
}
// todo.js
class Todo extends ReactiveNode {
  printIsRoot() {
    console.log(this.isRoot); // "false"
  }

  printPath() {
    console.log(this.path); // "todos/0"
  }

  printParent() {
    console.log(this.parent); // will log appState.todos
  }

  printAppState() {
    console.log(this.root); // will log appState
  }
}

Array and Map

Like MobX, MSC provides support to array and map. Anytime an array or a map gets attached to a ReactiveNode as class member, it'll be injected with all 4 tree node properties (.parent, .path, .root, .isRoot). It'll also receive an interceptor that handles updating tree relationships and other weight lifting like runtime conversion. Apart from these, they're basically plain MobX Observable Array/Map.

arrayOf() and mapOf() are exposed to help you create typed array/map. Example:

class MyTodoApp extends ReactiveNode {
  constructor() {
    super();
    this.namedTodoLists = mapOf(Todo);
  }
}

const myTodoApp = new MyTodoApp()
// This config the map interceptor so that when you invoke:
myTodoApp.namedTodoLists.set("Shopping List", newValue) 

// it'll be  to to 
myTodoApp.namedTodoLists.set("Shopping List", new Todo(newValue))

React Binding

There exists an official mobx-react binding lib. MSC provide a connect() function on top of that and follow the single state tree philosophy of Redux. See usage example:

import { Provider } from 'mobx-react'
import { connect, IMobxStateClassConfigs } from 'mobx-state-class'
import { App } from './MyApp'
import { AppState } from './AppState'

const appState = new AppState()

const ConnectedApp = connect(state => {
  return { 
    todos: state.todos,
    count: state.todos.length
  }
})(App);

// "state" is the required prop key here.
const AppWithStateProvider = () => (
  <Provider state={appState}>
    <ConnectedApp />
  </Provider>
);

If you use typescript, you can also inject your app state type info:

import { connect, IMobxStateClassConfigs } from 'mobx-state-class'

// if you use typescript, you can
declare module 'mobx-state-class' {
  interface IMobxStateClassConfigs {
    state: AppState
  }
}

const connected = connect(state => {
  return { 
    todos: state.todos,
    count: state.todos.length
  }
});