Replace legacy emitter with EventEmitter prototype

This commit is contained in:
calzoneman 2016-04-02 11:23:34 -07:00
parent a45148863a
commit 20538e328f
4 changed files with 9 additions and 79 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.14.5", "version": "3.14.6",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -1,4 +1,3 @@
var MakeEmitter = require("../emitter");
var Logger = require("../logger"); var Logger = require("../logger");
var ChannelModule = require("./module"); var ChannelModule = require("./module");
var Flags = require("../flags"); var Flags = require("../flags");
@ -11,6 +10,7 @@ var db = require("../database");
import * as ChannelStore from '../channel-storage/channelstore'; import * as ChannelStore from '../channel-storage/channelstore';
import { ChannelStateSizeError } from '../errors'; import { ChannelStateSizeError } from '../errors';
import Promise from 'bluebird'; import Promise from 'bluebird';
import { EventEmitter } from 'events';
class ReferenceCounter { class ReferenceCounter {
constructor(channel) { constructor(channel) {
@ -73,7 +73,6 @@ class ReferenceCounter {
} }
function Channel(name) { function Channel(name) {
MakeEmitter(this);
this.name = name; this.name = name;
this.uniqueName = name.toLowerCase(); this.uniqueName = name.toLowerCase();
this.modules = {}; this.modules = {};
@ -93,6 +92,8 @@ function Channel(name) {
}); });
} }
Channel.prototype = Object.create(EventEmitter.prototype);
Channel.prototype.is = function (flag) { Channel.prototype.is = function (flag) {
return Boolean(this.flags & flag); return Boolean(this.flags & flag);
}; };
@ -114,7 +115,7 @@ Channel.prototype.waitFlag = function (flag, cb) {
} else { } else {
var wait = function (f) { var wait = function (f) {
if (f === flag) { if (f === flag) {
self.unbind("setFlag", wait); self.removeListener("setFlag", wait);
cb(); cb();
} }
}; };

View File

@ -1,72 +0,0 @@
function MakeEmitter(obj) {
obj.__evHandlers = {};
obj.on = function (ev, fn) {
if (!(ev in this.__evHandlers)) {
this.__evHandlers[ev] = [];
}
this.__evHandlers[ev].push({
fn: fn,
remove: false
});
};
obj.once = function (ev, fn) {
if (!(ev in this.__evHandlers)) {
this.__evHandlers[ev] = [];
}
this.__evHandlers[ev].push({
fn: fn,
remove: true
});
};
obj.emit = function (ev /*, arguments */) {
var self = this;
var handlers = self.__evHandlers[ev];
if (!(handlers instanceof Array)) {
handlers = [];
} else {
handlers = Array.prototype.slice.call(handlers);
}
var args = Array.prototype.slice.call(arguments);
args.shift();
handlers.forEach(function (handler) {
setImmediate(function () {
handler.fn.apply(self, args);
});
if (handler.remove) {
var i = self.__evHandlers[ev].indexOf(handler);
if (i >= 0) {
self.__evHandlers[ev].splice(i, 1);
}
}
});
};
obj.unbind = function (ev, fn) {
var self = this;
if (ev in self.__evHandlers) {
if (!fn) {
self.__evHandlers[ev] = [];
} else {
var j = -1;
for (var i = 0; i < self.__evHandlers[ev].length; i++) {
if (self.__evHandlers[ev][i].fn === fn) {
j = i;
break;
}
}
if (j >= 0) {
self.__evHandlers[ev].splice(j, 1);
}
}
}
};
}
module.exports = MakeEmitter;

View File

@ -1,17 +1,16 @@
var Logger = require("./logger"); var Logger = require("./logger");
var Server = require("./server"); var Server = require("./server");
var util = require("./utilities"); var util = require("./utilities");
var MakeEmitter = require("./emitter");
var db = require("./database"); var db = require("./database");
var InfoGetter = require("./get-info"); var InfoGetter = require("./get-info");
var Config = require("./config"); var Config = require("./config");
var ACP = require("./acp"); var ACP = require("./acp");
var Account = require("./account"); var Account = require("./account");
var Flags = require("./flags"); var Flags = require("./flags");
import { EventEmitter } from 'events';
function User(socket) { function User(socket) {
var self = this; var self = this;
MakeEmitter(self);
self.flags = 0; self.flags = 0;
self.socket = socket; self.socket = socket;
self.realip = socket._realip; self.realip = socket._realip;
@ -103,6 +102,8 @@ function User(socket) {
}); });
} }
User.prototype = Object.create(EventEmitter.prototype);
User.prototype.die = function () { User.prototype.die = function () {
for (var key in this.socket._events) { for (var key in this.socket._events) {
delete this.socket._events[key]; delete this.socket._events[key];
@ -143,7 +144,7 @@ User.prototype.waitFlag = function (flag, cb) {
} else { } else {
var wait = function (f) { var wait = function (f) {
if (f === flag) { if (f === flag) {
self.unbind("setFlag", wait); self.removeListener("setFlag", wait);
cb(); cb();
} }
}; };