• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microstates / microstates.js / 525 / 3
99%
master: 99%

Build:
DEFAULT BRANCH: master
Ran 01 Mar 2018 06:47PM UTC
Files 1
Run time 0s
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

01 Mar 2018 06:38PM UTC coverage: 89.229% (+0.5%) from 88.688%
525.3

push

travis-ci

web-flow
Introducing `static create` method for initialization (#73)

When modeling state machines, it's necessary to be able to initialize into a particular
state based on value. Previously, this was accomplished by returning a microstate from the constructor. This lead to exceeds maximum call stack when destination type extended from type shifting class. 

This PR introduces a new convention which allows the developer to describe how a state should be initialized for a specific type. `create` method must return a microstate that represents the state that you want the state to initialize into or nothing.  The create method takes one argument which is any `value`.

```js
import { create } from 'microstates';

class Session {
  content = null;

  static create(session) {
    if (session) {
      return create(AuthenticatedSession, session);
    } else {
      return create(AnonymousSession);
    }
  }
}

class AuthenticatedSession extends Session {
  isAuthenticated = true;
  content = Object;

  logout() {
    return create(AnonymousSession);
  }
}

class AnonymousSession extends Session {
  content = null;
  isAuthenticated = false;

  authenticate(user) {
    return create(AuthenticatedSession, { content: user });
  }
}
```

The create method follows the same semantics as the constructor. When you return a microstate, it'll use the type and value in the microstate for `this` node. If you return anything else, it'll ignore the result and create an empty instance of that type. 

One perk of this solution is that all types that extend from the root type will automatically get this create method.

182 of 229 branches covered (79.48%)

Branch coverage included in aggregate %.

431 of 458 relevant lines covered (94.1%)

459.91 hits per line

Source Files on job 525.3
  • Tree
  • List 0
  • Changed 1
  • Source Changed 1
  • Coverage Changed 1
Coverage ∆ File Lines Relevant Covered Missed Hits/Line Branch Hits Branch Misses
  • Back to Build 1
  • Travis Job 525.3
  • b416f541 on github
  • Prev Job for on master (#511.3)
  • Next Job for on master (#526.1)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc