channel:join + channel:leave plugin events
[KiwiIRC.git] / server / websocketrpc.js
index 1fa035a4140986ac5935f48d6e9b14543e8cbd05..a8d287b98d8c82e223efbaddb13e0aa5d1443f7d 100644 (file)
@@ -4,19 +4,42 @@
     Some way to expire unused callbacks? TTL? expireCallback() function?
 */
 
+/**
+ * Wrapper around creating a new WebsocketRpcCaller
+ * This lets us use the WebsocketRpc object as a function
+ */
 function WebsocketRpc(eio_socket) {
-    var self = this;
+    var caller = new WebsocketRpcCaller(eio_socket);
+    var ret = function WebsocketRpcInstance() {
+        return ret.makeCall.apply(ret, arguments);
+    };
+
+    for(var prop in caller){
+        ret[prop] = caller[prop];
+    }
 
+    ret._mixinEmitter();
+    ret._bindSocketListeners();
+
+    // Keep a reference to the main Rpc object so namespaces can find calling functions
+    ret._rpc = ret;
+
+    return ret;
+}
+
+
+function WebsocketRpcCaller(eio_socket) {
     this._next_id = 0;
     this._rpc_callbacks = {};
     this._socket = eio_socket;
 
-    this._mixinEmitter();
-    this._bindSocketListeners();
+    this._rpc = this;
+    this._namespace = '';
+    this._namespaces = [];
 }
 
 
-WebsocketRpc.prototype._bindSocketListeners = function() {
+WebsocketRpcCaller.prototype._bindSocketListeners = function() {
     var self = this;
 
     // Proxy the onMessage listener
@@ -28,27 +51,69 @@ WebsocketRpc.prototype._bindSocketListeners = function() {
 
 
 
-WebsocketRpc.prototype.dispose = function() {
+WebsocketRpcCaller.prototype.dispose = function() {
     if (this._onMessageProxy) {
         this._socket.removeListener('message', this._onMessageProxy);
         delete this._onMessageProxy;
     }
 
+    // Clean up any namespaces
+    for (var idx in this._namespaces) {
+        this._namespaces[idx].dispose();
+    }
+
     this.removeAllListeners();
 };
 
 
 
+WebsocketRpcCaller.prototype.namespace = function(namespace_name) {
+    var complete_namespace, namespace;
+
+    if (this._namespace) {
+        complete_namespace = this._namespace + '.' + namespace_name;
+    } else {
+        complete_namespace = namespace_name;
+    }
+
+    namespace = new this._rpc.Namespace(this._rpc, complete_namespace);
+    this._rpc._namespaces.push(namespace);
+
+    return namespace;
+};
+
+
+
+// Find all namespaces that either matches or starts with namespace_name
+WebsocketRpcCaller.prototype._findRelevantNamespaces = function(namespace_name) {
+    var found_namespaces = [];
+
+    for(var idx in this._namespaces) {
+        if (this._namespaces[idx]._namespace === namespace_name) {
+            found_namespaces.push(this._namespaces[idx]);
+        }
+
+        if (this._namespaces[idx]._namespace.indexOf(namespace_name + '.') === 0) {
+            found_namespaces.push(this._namespaces[idx]);
+        }
+    }
+
+    return found_namespaces;
+};
+
+
 
 /**
  * The engine.io socket already has an emitter mixin so steal it from there
  */
-WebsocketRpc.prototype._mixinEmitter = function() {
+WebsocketRpcCaller.prototype._mixinEmitter = function(target_obj) {
     var funcs = ['on', 'once', 'off', 'removeListener', 'removeAllListeners', 'emit', 'listeners', 'hasListeners'];
 
+    target_obj = target_obj || this;
+
     for (var i=0; i<funcs.length; i++) {
         if (typeof this._socket[funcs[i]] === 'function')
-            this[funcs[i]] = this._socket[funcs[i]];
+            target_obj[funcs[i]] = this._socket[funcs[i]];
     }
 };
 
@@ -56,7 +121,7 @@ WebsocketRpc.prototype._mixinEmitter = function() {
 /**
  * Check if a packet is a valid RPC call
  */
-WebsocketRpc.prototype._isCall = function(packet) {
+WebsocketRpcCaller.prototype._isCall = function(packet) {
     return (typeof packet.method !== 'undefined' &&
             typeof packet.params !== 'undefined');
 };
@@ -65,7 +130,7 @@ WebsocketRpc.prototype._isCall = function(packet) {
 /**
  * Check if a packet is a valid RPC response
  */
-WebsocketRpc.prototype._isResponse = function(packet) {
+WebsocketRpcCaller.prototype._isResponse = function(packet) {
     return (typeof packet.id !== 'undefined' &&
             typeof packet.response !== 'undefined');
 };
@@ -77,9 +142,9 @@ WebsocketRpc.prototype._isResponse = function(packet) {
  * First argument must be the method name to call
  * If the last argument is a function, it is used as a callback
  * All other arguments are passed to the RPC method
- * Eg. Rpc.call('namespace.method_name', 1, 2, 3, callbackFn)
+ * Eg. Rpc.makeCall('namespace.method_name', 1, 2, 3, callbackFn)
  */
-WebsocketRpc.prototype.call = function(method) {
+WebsocketRpcCaller.prototype.makeCall = function(method) {
     var params, callback, packet;
 
     // Get a normal array of passed in arguments
@@ -110,7 +175,7 @@ WebsocketRpc.prototype.call = function(method) {
 /**
  * Encode the packet into JSON and send it over the websocket
  */
-WebsocketRpc.prototype.send = function(packet) {
+WebsocketRpcCaller.prototype.send = function(packet) {
     if (this._socket)
         this._socket.send(JSON.stringify(packet));
 };
@@ -119,11 +184,12 @@ WebsocketRpc.prototype.send = function(packet) {
 /**
  * Handler for the websocket `message` event
  */
-WebsocketRpc.prototype._onMessage = function(message_raw) {
+WebsocketRpcCaller.prototype._onMessage = function(message_raw) {
     var self = this,
         packet,
         returnFn,
-        callback;
+        callback,
+        namespace, namespaces, idx;
 
     try {
         packet = JSON.parse(message_raw);
@@ -152,7 +218,17 @@ WebsocketRpc.prototype._onMessage = function(message_raw) {
             returnFn = this._noop;
         }
 
+        this.emit.apply(this, ['all', packet.method, returnFn].concat(packet.params));
         this.emit.apply(this, [packet.method, returnFn].concat(packet.params));
+
+        if (packet.method.indexOf('.') > 0) {
+            namespace = packet.method.substring(0, packet.method.lastIndexOf('.'));
+            namespaces = this._findRelevantNamespaces(namespace);
+            for(idx in namespaces){
+                packet.method = packet.method.replace(namespaces[idx]._namespace + '.', '');
+                namespaces[idx].emit.apply(namespaces[idx], [packet.method, returnFn].concat(packet.params));
+            }
+        }
     }
 };
 
@@ -160,7 +236,7 @@ WebsocketRpc.prototype._onMessage = function(message_raw) {
 /**
  * Returns a function used as a callback when responding to a call
  */
-WebsocketRpc.prototype._createReturnCallFn = function(packet_id) {
+WebsocketRpcCaller.prototype._createReturnCallFn = function(packet_id) {
     var self = this;
 
     return function returnCallFn() {
@@ -177,7 +253,32 @@ WebsocketRpc.prototype._createReturnCallFn = function(packet_id) {
 
 
 
-WebsocketRpc.prototype._noop = function() {};
+WebsocketRpcCaller.prototype._noop = function() {};
+
+
+
+WebsocketRpcCaller.prototype.Namespace = function(rpc, namespace) {
+    var ret = function WebsocketRpcNamespaceInstance() {
+        if (typeof arguments[0] === 'undefined') {
+            return;
+        }
+
+        arguments[0] = ret._namespace + '.' + arguments[0];
+        return ret._rpc.apply(ret._rpc, arguments);
+    };
+
+    ret._rpc = rpc;
+    ret._namespace = namespace;
+
+    ret.dispose = function() {
+        ret.removeAllListeners();
+        ret._rpc = null;
+    };
+
+    rpc._mixinEmitter(ret);
+
+    return ret;
+};