Skip to content
Uni is a runner and composer of state machines
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
source
test
use-cases
.gitignore
API.md
LICENSE
README.md
bower.json
gulpfile.js
package.json

README.md

Uni

Uni is a runner and composer of state machines.

##Machine

A Uni machine is a state machine that can respond to changes over time. States can be determined by setters or by listening to other machines.

####Machine(value, options)

A basic sate machine is useful for simple conditions and basic feature detection:

var isTrueTrue = Uni.Machine(true);

State machines are not restricted to booleans, they can keep track of any variable types, including strings or objects.

var currentHotWord = Uni.Machine(API.get('hot-word'));

####Machine(function, options)

State Machines can also be functions that can be updated from external event listeners.

var currentHotWord = Uni.Machine(function() {
    return API.get('hot-word'); 
});

var isMediaSmall = Uni.Machine(function(width) {
    return width < 600;
});

var isMediaLarge = Uni.Machine(function(width) {
    return width > 1280;
});

###Machine Instance API

######Instance Setup

var hasEventListener = Uni.Machine('radEventListener' in window); // typo!
var windowWidth = Uni.Machine(function() {
    return window.innerWidth;
});

####Setters

There are multiple ways of setting and updating the state of a Uni Machine. set can create a new record or configure a function to be called from the update method.

#####set(value)

hasEventListener.set('addEventListener' in window); // fix typo

#####set(func)

hasEventListener.set(function(object) {
    return 'addEventListener' in object;
});

#####update(args...)

hasEventListener.update(window);
windowWidth.update();

####Getters

Machines provide a number of ways to access current and historical data. value, prev, first, and history are static properties, while the nth and get methods are available for more intricate retrieval.

#####value

Returns the current state of the Machine. If no state has been determined returns undefined

hasEventListener.value; // ==> true
windowWidth.value; // ==> 1000

#####nth(index)

nth returns a single record based on an index.

var value = windowWidth.nth(0);
var prev  = windowWidth.nth(1);
var first = windowWidth.nth(history.length);
hasEventListener.nth(0); // ==> true
windowWidth.nth(1); // ==> 995

#####history

Available history of the Machine, sorted with the newest state first. Different types

hasEventListener.history // ==> [true, true]
windowWidth.history; // ==> [1000, 995, 994];

####Responders

Responders define methods of listening to machine state changes.

#####respond(func)

isMediaSmall.respond(function() {
    html.addClass('media-small');
})

#####respond(value, func)

windowWidth.respond(function() {
    return this.value > 1140;    
}, function() {
    html.addClass('media-large');
})

#####once(func)

isMediaSmall.once(function() {
    html.addClass('media-small');
})

#####when(func)

isMediaSmall.when(function() {
    html.addClass('media-small');
})

#####while(func)

isMediaSmall.while(function() {
    html.addClass('media-small');
})

####Listeners

#####listenTo(func)

isMediaSmall.listenTo(windowWidth);

#####addListener(name, func)

isMediaSmall.addListener("change", function(value, prev) {
    dom.text(value);
});

#####addListener(name, func)

isMediaSmall.removeListener("change");

#####Listener Events

  • set
  • update
  • change

##Uni.Compose(action, machines)

Uni.Compose is used to build complex machines. Compositions are special machines that can build complex logic around other listened machines.

#####Composing sugar

  • Uni.not(args) ==> Uni.Compose('not', args)
  • Uni.and(args) ==> Uni.Compose('and', args)
  • Uni.or(args) ==> Uni.Compose('or', args)

##Static Methods

####Uni.addComposer()

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.