state
Version:
First-class states
158 lines (134 loc) • 4.03 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var O, State, StateEventEmitter, state,
__hasProp = {}.hasOwnProperty;
state = require('./state-function');
State = require('./state');
O = state.O;
module.exports = StateEventEmitter = (function() {
var guid, isArray;
isArray = O.isArray;
guid = 0;
function StateEventEmitter(state) {
this.state = state;
this.items = {};
this.length = 0;
}
StateEventEmitter.prototype.get = function(id) {
return this.items[id];
};
StateEventEmitter.prototype.getAll = function() {
var key, value, _ref, _results;
_ref = this.items;
_results = [];
for (key in _ref) {
if (!__hasProp.call(_ref, key)) continue;
value = _ref[key];
_results.push(value);
}
return _results;
};
StateEventEmitter.prototype.set = function(id, callback) {
var items;
items = this.items;
if (!(id in items)) {
this.length += 1;
}
items[id] = callback;
return id;
};
StateEventEmitter.prototype.key = function(callback) {
var key, value;
if ((function() {
var _ref, _results;
_ref = this.items;
_results = [];
for (key in _ref) {
if (!__hasProp.call(_ref, key)) continue;
value = _ref[key];
_results.push(value === callback);
}
return _results;
}).call(this)) {
return key;
}
};
StateEventEmitter.prototype.keys = function() {
var key, _ref, _results;
_ref = this.items;
_results = [];
for (key in _ref) {
if (!__hasProp.call(_ref, key)) continue;
_results.push(key);
}
return _results;
};
StateEventEmitter.prototype.add = function(callback, context) {
var id;
id = guid += 1;
this.items[id] = context != null ? [callback, context] : callback;
this.length += 1;
return id;
};
StateEventEmitter.prototype.on = StateEventEmitter.prototype.bind = StateEventEmitter.prototype.add;
StateEventEmitter.prototype.remove = function(id) {
var callback, items;
items = this.items;
callback = items[typeof id === 'function' ? this.key(id) : id];
if (!callback) {
return false;
}
delete items[id];
this.length -= 1;
return callback;
};
StateEventEmitter.prototype.off = StateEventEmitter.prototype.unbind = StateEventEmitter.prototype.remove;
StateEventEmitter.prototype.empty = function() {
var n;
if (!(n = this.length)) {
return 0;
}
this.items = {};
this.length = 0;
return n;
};
StateEventEmitter.prototype.emit = function(args, autostate) {
var context, eventualTarget, fn, item, key, owner, _ref;
if (autostate == null) {
autostate = this.state;
}
if (!(owner = autostate != null ? autostate.owner : void 0)) {
throw TypeError;
}
_ref = this.items;
for (key in _ref) {
if (!__hasProp.call(_ref, key)) continue;
item = _ref[key];
fn = context = null;
if (typeof item === 'string' || item instanceof State) {
eventualTarget = item;
continue;
}
if (typeof item === 'function') {
fn = item;
} else if (isArray(item)) {
fn = item[0], context = item[1];
} else if ((item != null ? item.type : void 0) === 'state-bound-function') {
fn = item.fn;
context || (context = autostate);
}
fn.apply(context || owner, args);
}
if (eventualTarget) {
return this.state.change(eventualTarget);
}
};
StateEventEmitter.prototype.trigger = StateEventEmitter.prototype.emit;
StateEventEmitter.prototype.destroy = function() {
this.empty();
this.state = this.items = null;
return true;
};
return StateEventEmitter;
})();
}).call(this);