UNPKG

janus

Version:

the two-faced application library-framework

174 lines (153 loc) 4.46 kB
// Generated by CoffeeScript 1.12.2 (function() { var Base, EventEmitter; EventEmitter = require('eventemitter2').EventEmitter2; Base = (function() { function Base() { this._outwardListeners = []; this._outwardReactions = []; this._refCount = 1; } Base.prototype.on = function(type, listener) { if (this.events == null) { this.events = new EventEmitter({ delimeter: ':', maxListeners: 0 }); } this.events.on(type, listener); return this; }; Base.prototype._on = function(type, listener) { if (this.events == null) { this.events = new EventEmitter({ delimeter: ':', maxListeners: 0 }); } this.events.prependListener(type, listener); return this; }; Base.prototype.off = function(type, listener) { var ref; if ((ref = this.events) != null) { ref.off(type, listener); } return this; }; Base.prototype.emit = function(e, x, y) { var length; if (this.events == null) { return false; } length = arguments.length; if (length === 2) { return this.events.emit(e, x); } else if (length === 3) { return this.events.emit(e, x, y); } else { return EventEmitter.prototype.emit.apply(this.events, arguments); } }; Base.prototype.listeners = function(name) { var ref, ref1; return (ref = (ref1 = this.events) != null ? ref1.listeners(name) : void 0) != null ? ref : []; }; Base.prototype.removeAllListeners = function(event) { var ref; return (ref = this.events) != null ? ref.removeAllListeners(event) : void 0; }; Base.prototype.listenTo = function(target, event, handler) { this._outwardListeners.push(arguments); if (target != null) { if (typeof target.on === "function") { target.on(event, handler); } } return this; }; Base.prototype.unlistenTo = function(tgt) { var event, handler, i, len, ref, ref1, target; ref = this._outwardListeners; for (i = 0, len = ref.length; i < len; i++) { ref1 = ref[i], target = ref1[0], event = ref1[1], handler = ref1[2]; if (target === tgt) { if (target != null) { if (typeof target.off === "function") { target.off(event, handler); } } } } return this; }; Base.prototype.reactTo = function(varying, x, y) { var observation; observation = varying.react(x, y); this._outwardReactions.push(observation); return observation; }; Base.prototype.stopAll = function() { var event, handler, i, j, len, len1, o, ref, ref1, ref2, target; ref = this._outwardListeners; for (i = 0, len = ref.length; i < len; i++) { ref1 = ref[i], target = ref1[0], event = ref1[1], handler = ref1[2]; if (target != null) { if (typeof target.off === "function") { target.off(event, handler); } } } ref2 = this._outwardReactions; for (j = 0, len1 = ref2.length; j < len1; j++) { o = ref2[j]; o.stop(); } this.removeAllListeners(); }; Base.prototype.destroy = function() { if ((this._refCount -= 1) === 0) { this.emit('destroying'); this.stopAll(); if (typeof this._destroy === "function") { this._destroy(); } if (typeof this.__destroy === "function") { this.__destroy(); } this.destroyed = true; } }; Base.prototype.destroyWith = function(other) { this.listenTo(other, 'destroying', (function(_this) { return function() { return _this.destroy(); }; })(this)); return this; }; Base.prototype.tap = function() { this._refCount += 1; return this; }; Base.managed = function(f) { var resource; resource = null; return function() { if (resource != null) { return resource.tap(); } else { resource = f.call(this); resource.on('destroying', function() { return resource = null; }); return resource; } }; }; return Base; })(); module.exports = { Base: Base }; }).call(this);