Permalink
Browse files

updated intro and example

  • Loading branch information...
1 parent f7350bb commit 984356623031519d2e2b81fcd252915404352326 @joshwnj committed Aug 22, 2012
Showing with 39 additions and 22 deletions.
  1. +39 −22 README.md
View
@@ -2,56 +2,73 @@
[![Build Status](https://secure.travis-ci.org/joshwnj/statelet.png)](http://travis-ci.org/joshwnj/statelet)
-Instead of this:
+Sometimes we have a pattern like:
```js
if (is_ready) {
- // do the thing
+ // do the thing
}
else {
- // put into a queue and execute when is_ready=true
+ // put into a queue and execute when is_ready=true
}
```
-Do this:
+Events make it better:
```js
-is_ready.when(true, function () {
+emitter.on('ready', function () {
// do the thing
});
```
-## Logic Gates
+But what if you start listening too late and you miss the event? You'll be waiting forever...
+
+`statelet` takes a different approach where instead of listening for events we're watching for changes in state:
```js
-var burger = new State();
-var fries = new State();
-var meal_is_ready = new State();
-
-function onMealChange () {
- var burger_ready = burger.get() === 'flipped';
- var fries_ready = fries.get() === 'fried';
- meal_is_ready.set(burger_ready && fries_ready);
-}
-burger.watch(onMealChange);
-fries.watch(onMealChange);
-meal_is_ready.when(true, function () {
- console.log('ding ding!');
+is_ready.when(true, function () {
+ // do the thing
});
```
-## To do
+## Combining States
- * make sure examples have decent cross-browser support
- * more examples
+Sometimes you want to know when multiple states align in a certain way.
+
+```js
+var State = require('statelet');
+var is_happy = new State();
+var knows_it = new State();
+var action = new State();
+
+function onChange () {
+ var youre_happy = is_happy.get();
+ var you_know_it = knows_it.get();
+
+ if (youre_happy && you_know_it) {
+ action.set('clap your hands');
+ }
+}
+is_happy.watch(onChange);
+knows_it.watch(onChange);
+```
+
+## Where can I use it?
+
+Works in both node.js and browser.
## Install
```
npm install statelet
```
+## To do
+
+ * make sure examples have decent cross-browser support
+ * more examples
+
## License
MIT

0 comments on commit 9843566

Please sign in to comment.