Refactor events module to lib/events.js
This commit is contained in:
parent
4badb227cf
commit
7ff53f4c6a
@ -1,4 +1,70 @@
|
|||||||
exports.EventEmitter = process.EventEmitter;
|
exports.EventEmitter = process.EventEmitter;
|
||||||
|
|
||||||
exports.Promise = removed('Promise has been removed. See http://groups.google.com/group/nodejs/msg/0c483b891c56fea2 for more information.');
|
// process.EventEmitter is defined in src/node_events.cc
|
||||||
|
// process.EventEmitter.prototype.emit() is also defined there.
|
||||||
|
process.EventEmitter.prototype.addListener = function (type, listener) {
|
||||||
|
if ('function' !== typeof listener) {
|
||||||
|
throw new Error('addListener only takes instances of Function');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this._events) this._events = {};
|
||||||
|
|
||||||
|
// To avoid recursion in the case that type == "newListeners"! Before
|
||||||
|
// adding it to the listeners, first emit "newListeners".
|
||||||
|
this.emit("newListener", type, listener);
|
||||||
|
|
||||||
|
if (!this._events[type]) {
|
||||||
|
// Optimize the case of one listener. Don't need the extra array object.
|
||||||
|
this._events[type] = listener;
|
||||||
|
} else if (this._events[type] instanceof Array) {
|
||||||
|
// If we've already got an array, just append.
|
||||||
|
this._events[type].push(listener);
|
||||||
|
} else {
|
||||||
|
// Adding the second element, need to change to array.
|
||||||
|
this._events[type] = [this._events[type], listener];
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
process.EventEmitter.prototype.removeListener = function (type, listener) {
|
||||||
|
if ('function' !== typeof listener) {
|
||||||
|
throw new Error('removeListener only takes instances of Function');
|
||||||
|
}
|
||||||
|
|
||||||
|
// does not use listeners(), so no side effect of creating _events[type]
|
||||||
|
if (!this._events || !this._events[type]) return this;
|
||||||
|
|
||||||
|
var list = this._events[type];
|
||||||
|
|
||||||
|
if (list instanceof Array) {
|
||||||
|
var i = list.indexOf(listener);
|
||||||
|
if (i < 0) return this;
|
||||||
|
list.splice(i, 1);
|
||||||
|
} else {
|
||||||
|
this._events[type] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
process.EventEmitter.prototype.removeAllListeners = function (type) {
|
||||||
|
// does not use listeners(), so no side effect of creating _events[type]
|
||||||
|
if (!type || !this._events || !this._events[type]) return this;
|
||||||
|
this._events[type] = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
process.EventEmitter.prototype.listeners = function (type) {
|
||||||
|
if (!this._events) this._events = {};
|
||||||
|
if (!this._events[type]) this._events[type] = [];
|
||||||
|
if (!(this._events[type] instanceof Array)) {
|
||||||
|
this._events[type] = [this._events[type]];
|
||||||
|
}
|
||||||
|
return this._events[type];
|
||||||
|
};
|
||||||
|
exports.Promise = function removed () {
|
||||||
|
throw new Error(
|
||||||
|
'Promise has been removed. See '+
|
||||||
|
'http://groups.google.com/group/nodejs/msg/0c483b891c56fea2 for more information.');
|
||||||
|
}
|
||||||
process.Promise = exports.Promise;
|
process.Promise = exports.Promise;
|
||||||
|
@ -1303,10 +1303,11 @@ static Handle<Value> Binding(const Arguments& args) {
|
|||||||
exports->Set(String::New("repl"), String::New(native_repl));
|
exports->Set(String::New("repl"), String::New(native_repl));
|
||||||
exports->Set(String::New("sys"), String::New(native_sys));
|
exports->Set(String::New("sys"), String::New(native_sys));
|
||||||
exports->Set(String::New("tcp"), String::New(native_tcp));
|
exports->Set(String::New("tcp"), String::New(native_tcp));
|
||||||
exports->Set(String::New("tcp_old"), String::New(native_tcp_old));
|
exports->Set(String::New("tcp_old"), String::New(native_tcp_old));
|
||||||
exports->Set(String::New("uri"), String::New(native_uri));
|
exports->Set(String::New("uri"), String::New(native_uri));
|
||||||
exports->Set(String::New("url"), String::New(native_url));
|
exports->Set(String::New("url"), String::New(native_url));
|
||||||
exports->Set(String::New("utils"), String::New(native_utils));
|
exports->Set(String::New("utils"), String::New(native_utils));
|
||||||
|
exports->Set(String::New("events"), String::New(native_events));
|
||||||
binding_cache->Set(module, exports);
|
binding_cache->Set(module, exports);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
79
src/node.js
79
src/node.js
@ -103,80 +103,15 @@ process.assert = function (x, msg) {
|
|||||||
process.evalcx = process.binding('evals').Script.runInNewContext;
|
process.evalcx = process.binding('evals').Script.runInNewContext;
|
||||||
|
|
||||||
// Event
|
// Event
|
||||||
|
var eventsModule = createInternalModule
|
||||||
var eventsModule = createInternalModule('events', function (exports) {
|
( 'events'
|
||||||
exports.EventEmitter = process.EventEmitter;
|
, process.compile
|
||||||
|
( "(function (exports) {" + natives.events + "})"
|
||||||
// process.EventEmitter is defined in src/node_events.cc
|
, "events"
|
||||||
// process.EventEmitter.prototype.emit() is also defined there.
|
)
|
||||||
process.EventEmitter.prototype.addListener = function (type, listener) {
|
);
|
||||||
if (typeof listener != 'function') {
|
|
||||||
throw new Error('addListener only takes functions');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this._events) this._events = {};
|
|
||||||
|
|
||||||
// To avoid recursion in the case that type == "newListeners"! Before
|
|
||||||
// adding it to the listeners, first emit "newListeners".
|
|
||||||
this.emit("newListener", type, listener);
|
|
||||||
|
|
||||||
if (!this._events[type]) {
|
|
||||||
// Optimize the case of one listener. Don't need the extra array object.
|
|
||||||
this._events[type] = listener;
|
|
||||||
} else if (this._events[type] instanceof Array) {
|
|
||||||
// If we've already got an array, just append.
|
|
||||||
this._events[type].push(listener);
|
|
||||||
} else {
|
|
||||||
// Adding the second element, need to change to array.
|
|
||||||
this._events[type] = [this._events[type], listener];
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
process.EventEmitter.prototype.removeListener = function (type, listener) {
|
|
||||||
if (typeof listener != 'function') {
|
|
||||||
throw new Error('removeListener only takes functions');
|
|
||||||
}
|
|
||||||
|
|
||||||
// does not use listeners(), so no side effect of creating _events[type]
|
|
||||||
if (!this._events || !this._events[type]) return this;
|
|
||||||
|
|
||||||
var list = this._events[type];
|
|
||||||
|
|
||||||
if (list instanceof Array) {
|
|
||||||
var i = list.indexOf(listener);
|
|
||||||
if (i < 0) return this;
|
|
||||||
list.splice(i, 1);
|
|
||||||
} else {
|
|
||||||
this._events[type] = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
process.EventEmitter.prototype.removeAllListeners = function (type) {
|
|
||||||
// does not use listeners(), so no side effect of creating _events[type]
|
|
||||||
if (!type || !this._events || !this._events[type]) return this;
|
|
||||||
this._events[type] = null;
|
|
||||||
};
|
|
||||||
|
|
||||||
process.EventEmitter.prototype.listeners = function (type) {
|
|
||||||
if (!this._events) this._events = {};
|
|
||||||
if (!this._events[type]) this._events[type] = [];
|
|
||||||
if (!(this._events[type] instanceof Array)) {
|
|
||||||
this._events[type] = [this._events[type]];
|
|
||||||
}
|
|
||||||
return this._events[type];
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.Promise = removed('Promise has been removed. See http://groups.google.com/group/nodejs/msg/0c483b891c56fea2 for more information.');
|
|
||||||
process.Promise = exports.Promise;
|
|
||||||
});
|
|
||||||
|
|
||||||
var events = eventsModule.exports;
|
var events = eventsModule.exports;
|
||||||
|
|
||||||
|
|
||||||
// nextTick()
|
// nextTick()
|
||||||
|
|
||||||
var nextTickQueue = [];
|
var nextTickQueue = [];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user