[Groonga-commit] groonga/express-kotoumi [master] Reconstruct connection APIs based on EventEmitter

Back to archive index

YUKI Hiroshi null+****@clear*****
Fri Dec 28 17:39:04 JST 2012


YUKI Hiroshi	2012-12-28 17:39:04 +0900 (Fri, 28 Dec 2012)

  New Revision: b51007b31d6fb817290e8b147998a8ab4937f3eb
  https://github.com/groonga/express-kotoumi/commit/b51007b31d6fb817290e8b147998a8ab4937f3eb

  Log:
    Reconstruct connection APIs based on EventEmitter

  Modified files:
    lib/backend-adaptor.js

  Modified: lib/backend-adaptor.js (+38 -15)
===================================================================
--- lib/backend-adaptor.js    2012-12-28 17:26:10 +0900 (f5266a4)
+++ lib/backend-adaptor.js    2012-12-28 17:39:04 +0900 (ff1a1e3)
@@ -1,3 +1,22 @@
+/**
+ * var connection = new Connection({ tag:  'groonga',
+ *                                   host: 'localhost',
+ *                                   port: 24224 });
+ *
+ * // for REST APIs
+ * connection.emitMessage(
+ *   { command: 'search',
+ *     query:   'foobar' },
+ *   function callback(result) {
+ *     // codes to handle results from the backend
+ *   }
+ * );
+ *
+ * // for socket.io APIs
+ * connection.on('notification', function listener(messageBody) { ... });
+ * connection.emitVolatileMessage({ command: 'reindex' });
+ */
+
 var EventEmitter = require('events').EventEmitter;
 var http = require('http');
 var socketIo = require('socket.io');
@@ -24,8 +43,8 @@ Connection.prototype._init = function() {
 
   var self = this;
   this._socket.sockets.on('connection', function(socket) {
-    socket.on(self._params.tag + '.result',
-              self._handleResult.bind(self));
+    socket.on(self._params.tag + '.message',
+              self._handleMessage.bind(self));
   });
   this._socket.set('log level', 1);
 };
@@ -33,14 +52,10 @@ Connection.prototype._init = function() {
 Connection.prototype._handleHttpRequest = function(request, response) {
 };
 
-Connection.prototype._handleResult = function(data) {
-  var id = data['query']['requestid']
-  var callbck = this._callbacks[id];
-  if (callback) {
-    delete this._callbacks[id];
-    callback(data['result']);
-  } else {
-    // something went wrong;
+Connection.prototype._handleMessage = function(message) {
+  var replyTo = message['reply_to'];
+  if (replyTo) {
+    this.emit('replyTo:' + replyTo, message.body);
   }
 };
 
@@ -51,12 +66,20 @@ function createId() {
 Connection.prototype.emitMessage = function(message, callback) {
   var id = createId();
   var message = {
-    requestid: id,
-    command:   'search',
-    query:     query
+    id:   id,
+    body: message
+  };
+  this.once('replyTo:' + id, callback);
+  this._sender.emit('message', message);
+};
+
+Connection.prototype.emitVolatileMessage = function(message) {
+  var id = createId();
+  var message = {
+    id:   id,
+    body: message
   };
-  this._callbacks[id] = callback;
-  this._sender.emit('query', message);
+  this._sender.emit('message', message);
 };
 
 export.Connection = Connection;
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index