Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

lib: eventemitter

  • Loading branch information...
commit 8bdd9c65a4568c5f4436af2ed3253048b485c2c4 1 parent e6da8f4
@indutny authored
Showing with 49 additions and 8 deletions.
  1. +1 −0  candor.io.gyp
  2. +29 −0 lib/events.can
  3. +1 −1  lib/module.can
  4. +18 −7 lib/tcp.can
View
1  candor.io.gyp
@@ -1,6 +1,7 @@
{
'variables': {
'library_files': [
+ 'lib/events.can',
'lib/fs.can',
'lib/tcp.can',
'lib/module.can',
View
29 lib/events.can
@@ -0,0 +1,29 @@
+// EventEmitter
+exports = {}
+
+EventEmitter = exports.EventEmitter = {
+ _listeners: {}
+}
+
+EventEmitter.init = (self) {
+}
+
+EventEmitter.on = (self, event, listener) {
+ if (!self._listeners[event]) self._listeners[event] = []
+ listeners = self._listeners[event]
+
+ listeners[sizeof listeners] = listener
+}
+
+EventEmitter.emit = (self, event, args...) {
+ i = 0
+ listeners = self._listeners[event]
+ while (i < sizeof listeners) {
+ listeners[i](args...)
+ i++
+ }
+
+ return sizeof listeners
+}
+
+return exports
View
2  lib/module.can
@@ -9,7 +9,7 @@ global.require = (filename) {
content = buffer.stringify(fs.readFileSync(filename) ||
fs.readFileSync(filename + '.can'))
- content = "return (require, module, exports) {\n" + content + "\n}"
+ content = 'return (require, module, exports) {\n' + content + '\n}'
module = {}
module.exports = cache[filename] = {}
View
25 lib/tcp.can
@@ -1,29 +1,40 @@
// TCP Module
tcp = global._bindings.tcp
+EventEmitter = global._natives.events.EventEmitter
exports = {}
-Server = exports.Server = {}
+Socket = exports.Socket = clone EventEmitter
+Socket.init = (self, handle) {
+ EventEmitter.init(self)
+ self._handle = hande
+}
+
+Server = exports.Server = clone EventEmitter
Server.init = (self, listener) {
- global.log(self, listener)
self._handle = tcp.new()
- self._listener = listener
+
+ if (listener) self:on('connection', listener)
return self
}
Server.listen = (self, port, host) {
- tcp.bind(self._handle, port, host || "0.0.0.0")
- tcp.listen(self._handle, 511, (socket) {
- self._listener(socket)
+ tcp.bind(self._handle, port, host || '0.0.0.0')
+ tcp.listen(self._handle, 511, (handle) {
+ socket = clone Socket
+ self:emit('connection', socket:init(handle))
})
return self
}
Server.close = (self, cb) {
- tcp.close(self._handle, cb || () {})
+ if (cb) self:on('close', cb)
+ tcp.close(self._handle, () {
+ self:emit('close')
+ })
}
exports.createServer = (listener) {
Please sign in to comment.
Something went wrong with that request. Please try again.