mirror of https://github.com/calzoneman/sync.git
Replace legacy emitter with EventEmitter prototype
This commit is contained in:
parent
a45148863a
commit
20538e328f
|
@ -2,7 +2,7 @@
|
|||
"author": "Calvin Montgomery",
|
||||
"name": "CyTube",
|
||||
"description": "Online media synchronizer and chat",
|
||||
"version": "3.14.5",
|
||||
"version": "3.14.6",
|
||||
"repository": {
|
||||
"url": "http://github.com/calzoneman/sync"
|
||||
},
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
var MakeEmitter = require("../emitter");
|
||||
var Logger = require("../logger");
|
||||
var ChannelModule = require("./module");
|
||||
var Flags = require("../flags");
|
||||
|
@ -11,6 +10,7 @@ var db = require("../database");
|
|||
import * as ChannelStore from '../channel-storage/channelstore';
|
||||
import { ChannelStateSizeError } from '../errors';
|
||||
import Promise from 'bluebird';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
class ReferenceCounter {
|
||||
constructor(channel) {
|
||||
|
@ -73,7 +73,6 @@ class ReferenceCounter {
|
|||
}
|
||||
|
||||
function Channel(name) {
|
||||
MakeEmitter(this);
|
||||
this.name = name;
|
||||
this.uniqueName = name.toLowerCase();
|
||||
this.modules = {};
|
||||
|
@ -93,6 +92,8 @@ function Channel(name) {
|
|||
});
|
||||
}
|
||||
|
||||
Channel.prototype = Object.create(EventEmitter.prototype);
|
||||
|
||||
Channel.prototype.is = function (flag) {
|
||||
return Boolean(this.flags & flag);
|
||||
};
|
||||
|
@ -114,7 +115,7 @@ Channel.prototype.waitFlag = function (flag, cb) {
|
|||
} else {
|
||||
var wait = function (f) {
|
||||
if (f === flag) {
|
||||
self.unbind("setFlag", wait);
|
||||
self.removeListener("setFlag", wait);
|
||||
cb();
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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;
|
|
@ -1,17 +1,16 @@
|
|||
var Logger = require("./logger");
|
||||
var Server = require("./server");
|
||||
var util = require("./utilities");
|
||||
var MakeEmitter = require("./emitter");
|
||||
var db = require("./database");
|
||||
var InfoGetter = require("./get-info");
|
||||
var Config = require("./config");
|
||||
var ACP = require("./acp");
|
||||
var Account = require("./account");
|
||||
var Flags = require("./flags");
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
function User(socket) {
|
||||
var self = this;
|
||||
MakeEmitter(self);
|
||||
self.flags = 0;
|
||||
self.socket = socket;
|
||||
self.realip = socket._realip;
|
||||
|
@ -103,6 +102,8 @@ function User(socket) {
|
|||
});
|
||||
}
|
||||
|
||||
User.prototype = Object.create(EventEmitter.prototype);
|
||||
|
||||
User.prototype.die = function () {
|
||||
for (var key in this.socket._events) {
|
||||
delete this.socket._events[key];
|
||||
|
@ -143,7 +144,7 @@ User.prototype.waitFlag = function (flag, cb) {
|
|||
} else {
|
||||
var wait = function (f) {
|
||||
if (f === flag) {
|
||||
self.unbind("setFlag", wait);
|
||||
self.removeListener("setFlag", wait);
|
||||
cb();
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue