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

microstates / microstates.js / 525
99%

Build:
DEFAULT BRANCH: master
Ran 01 Mar 2018 06:41PM UTC
Jobs 4
Files 1
Run time 6min
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

pending completion
525

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%)

1839.65 hits per line

Jobs
ID Job ID Ran Files Coverage
1 525.1 01 Mar 2018 06:41PM UTC 0
89.23
Travis Job 525.1
2 525.2 01 Mar 2018 06:42PM UTC 0
89.23
Travis Job 525.2
3 525.3 01 Mar 2018 06:47PM UTC 0
89.23
Travis Job 525.3
4 525.4 01 Mar 2018 06:45PM UTC 0
89.23
Travis Job 525.4
Source Files on build 525
Detailed source file information is not available for this build.
  • Back to Repo
  • Travis Build #525
  • b416f541 on github
  • Prev Build on master (#511)
  • Next Build on master (#526)
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