Skip to content

Instantly share code, notes, and snippets.

@nickfargo
nickfargo / animal-consciousness.js
Created April 14, 2011 21:55
Animal consciousness - State.js slapdash example
( function ( $, undefined ) {
function Animal () {
State( this, 'consciousness', {
die: function () { this.consciousState.change( 'Dead' ); },
Awake: {
breathe: function () { return 'huff puff'; },
heard: function ( noise ) {
if ( noise == "Boo!" ) {
@nickfargo
nickfargo / chronograph.js
Created April 14, 2011 21:56
Chronograph - State.js slapdash example
( function ( $, undefined ) {
var Chronograph = $.extend( true,
function Chronograph () {
this.controller = new Chronograph.Controller( this );
this.model = new Chronograph.Model( this.controller );
this.view = new Chronograph.View( this.controller );
this.engine = new Chronograph.Engine( this );
}, {
@nickfargo
nickfargo / animal-bird-ostrich.js
Created April 14, 2011 21:59
Animal-Bird-Ostrich - State.js slapdash example
function Animal () {
this.move = function () { return 0; };
State( this, {
Stationary: {
move: function () { return false; }
},
Moving: {
move: function () { return true; }
}
}, 'Stationary' );
@nickfargo
nickfargo / Hipster.coffee
Created January 13, 2012 11:35
Applying state to a class prototype in CoffeeScript using State.js
state = require 'state'
class Hipster
emote: -> ":|"
state @::,
Stoked:
emote: -> ":)"
Bummed:
emote: -> ":("
@nickfargo
nickfargo / TextDocument.coffee
Created January 13, 2012 11:54
Example for proposed usage of attributes in state definitions
state = require 'state'
io = require 'some-imaginary-io-module'
owner = -> @owner()
class TextDocument
constructor: (@url) ->
text = ''
@text = -> text
@edit = (newText) -> text = newText; @
@nickfargo
nickfargo / gist:1826082
Created February 14, 2012 11:38
State attributes example
# # State attributes
# ## Declaration
state = require 'state'
class Emo
state @::, 'abstract history'
Happy: state 'initial retained'
emote: -> ":)"
@nickfargo
nickfargo / gist:1901883
Created February 24, 2012 16:30
Using `Z.NIL` and `Z.delta` to create a traversable differential history
var Z = require('zcore');
function H () {
var NIL = Z.NIL,
h = this.history = [
{},
{ a: 1, b: 2 },
{ b: NIL, d: 4 },
{ a: NIL, e: 5 },
@nickfargo
nickfargo / gist:1942014
Created February 29, 2012 16:04
Example of information hiding in JavaScript
function Seeker () {
var me = this,
knowledge = null;
function learn ( secret ) { knowledge = secret; }
this.ask = function ( whom ) {
whom.tell( me, learn );
};
this.spill = function () {
@nickfargo
nickfargo / gist:1986146
Created March 6, 2012 12:58
Expression monolith vs. Statement spaghetti
{
// ... ,
// #### method
//
// Retrieves the named method held on this state. If no method is found, step through
// this state’s protostate chain to find one. If no method is found there, step up the
// superstate hierarchy and repeat the search.
method: function ( methods ) {
return function (
@nickfargo
nickfargo / event-emitter-factory.coffee
Created June 14, 2012 19:11
A simple event emitter mixin module in 10 sloc
createEventEmitter = ( add, remove ) ->
types = {}
this[ add or 'addListener' ] = ( type, fn, context ) ->
( types[ type ] ||= [] ).push fn: fn, context: context or this
this[ remove or 'removeListener' ] = ( type, fn ) ->
for listener, index in list = types[ type ]
if fn is listener.fn then list.splice index, 1; return fn