[Pkg-javascript-commits] [sockjs-client] 118/350: Consolidate some logic from jsonp and ajax-based

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:49 UTC 2016


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.

commit c357fd7c902571cd10741aaed6ddff36433f8d77
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Fri Oct 10 17:02:51 2014 -0400

    Consolidate some logic from jsonp and ajax-based
---
 lib/transport/jsonp-polling.js       | 31 +++----------------------------
 lib/transport/lib/ajax-based.js      | 29 +++--------------------------
 lib/transport/lib/sender-receiver.js | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 54 deletions(-)

diff --git a/lib/transport/jsonp-polling.js b/lib/transport/jsonp-polling.js
index f37e9a9..a192d33 100644
--- a/lib/transport/jsonp-polling.js
+++ b/lib/transport/jsonp-polling.js
@@ -9,41 +9,16 @@
 //   o for Konqueror a dumb timer is needed to detect errors
 
 var util = require('util')
-  , BufferedSender = require('./lib/buffered-sender')
-  , Polling = require('./lib/polling')
+  , SenderReceiver = require('./lib/sender-receiver')
   , JsonpReceiver = require('./receiver/jsonp')
   , jsonpSender = require('./sender/jsonp')
   ;
 
-// TODO this logic is very similar to ajax-based. See if we could combine.
-
 function JsonPTransport(transUrl) {
-  var self = this;
-  BufferedSender.call(this, transUrl, jsonpSender);
-
-  this.poll = new Polling(JsonpReceiver, this.transUrl + '/jsonp');
-  this.poll.on('message', function (msg) {
-    self.emit('message', msg);
-  });
-  this.poll.once('close', function (code, reason) {
-    self.poll = null;
-    self.stop();
-    self.emit('close', code, reason);
-    self.removeAllListeners();
-  });
+  SenderReceiver.call(this, transUrl, '/jsonp', jsonpSender, JsonpReceiver);
 }
 
-util.inherits(JsonPTransport, BufferedSender);
-
-JsonPTransport.prototype.close = function() {
-  if (this.poll) {
-    this.poll.abort();
-    this.poll = null;
-  }
-  this.stop();
-  this.emit('close', null, 'user');
-  this.removeAllListeners();
-};
+util.inherits(JsonPTransport, SenderReceiver);
 
 JsonPTransport.enabled = function() {
   return !!global.document;
diff --git a/lib/transport/lib/ajax-based.js b/lib/transport/lib/ajax-based.js
index 9e92f4e..b62d854 100644
--- a/lib/transport/lib/ajax-based.js
+++ b/lib/transport/lib/ajax-based.js
@@ -1,8 +1,7 @@
 'use strict';
 
 var util = require('util')
-  , BufferedSender = require('./buffered-sender')
-  , Polling = require('./polling')
+  , SenderReceiver = require('./sender-receiver')
   ;
 
 function createAjaxSender(AjaxObject) {
@@ -25,31 +24,9 @@ function createAjaxSender(AjaxObject) {
 }
 
 function AjaxBasedTransport(transUrl, urlSuffix, Receiver, AjaxObject) {
-  var self = this;
-  BufferedSender.call(this, transUrl, createAjaxSender(AjaxObject));
-
-  this.poll = new Polling(Receiver, transUrl + urlSuffix, AjaxObject);
-  this.poll.on('message', function (msg) {
-    self.emit('message', msg);
-  });
-  this.poll.once('close', function (code, reason) {
-    self.poll = null;
-    self.stop();
-    self.emit('close', code, reason);
-    self.removeAllListeners();
-  });
+  SenderReceiver.call(this, transUrl, urlSuffix, createAjaxSender(AjaxObject), Receiver, AjaxObject);
 }
 
-util.inherits(AjaxBasedTransport, BufferedSender);
-
-AjaxBasedTransport.prototype.close = function() {
-  if (this.poll) {
-    this.poll.abort();
-    this.poll = null;
-  }
-  this.stop();
-  this.emit('close', null, 'user');
-  this.removeAllListeners();
-};
+util.inherits(AjaxBasedTransport, SenderReceiver);
 
 module.exports = AjaxBasedTransport;
diff --git a/lib/transport/lib/sender-receiver.js b/lib/transport/lib/sender-receiver.js
new file mode 100644
index 0000000..945570f
--- /dev/null
+++ b/lib/transport/lib/sender-receiver.js
@@ -0,0 +1,36 @@
+'use strict';
+
+var util = require('util')
+  , BufferedSender = require('./buffered-sender')
+  , Polling = require('./polling')
+  ;
+
+function SenderReceiver(transUrl, urlSuffix, senderFunc, Receiver, AjaxObject) {
+  var self = this;
+  BufferedSender.call(this, transUrl, senderFunc);
+
+  this.poll = new Polling(Receiver, transUrl + urlSuffix, AjaxObject);
+  this.poll.on('message', function (msg) {
+    self.emit('message', msg);
+  });
+  this.poll.once('close', function (code, reason) {
+    self.poll = null;
+    self.stop();
+    self.emit('close', code, reason);
+    self.removeAllListeners();
+  });
+}
+
+util.inherits(SenderReceiver, BufferedSender);
+
+SenderReceiver.prototype.close = function() {
+  if (this.poll) {
+    this.poll.abort();
+    this.poll = null;
+  }
+  this.stop();
+  this.emit('close', null, 'user');
+  this.removeAllListeners();
+};
+
+module.exports = SenderReceiver;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list