Browse Source

Working server-to-client refreshes.

Frederic G. MARAND 8 years ago
parent
commit
f9f45bd938
7 changed files with 51 additions and 0 deletions
  1. 1 0
      client/helpers.js
  2. 1 0
      client/sso.js
  3. 4 0
      lib/constants.js
  4. 15 0
      lib/drupalsso.js
  5. 4 0
      package.js
  6. 4 0
      server/sso.js
  7. 22 0
      server/web.js

+ 1 - 0
client/helpers.js

@@ -1,3 +1,4 @@
+Meteor._debug("Loading client/helpers", CHANNEL_NAME, EVENT_NAME);
 Meteor.startup(function () {
   // Helper names have to be well-formed JS identifiers, so they cannot use a
   // "namespace.symbol" format... but a "namespace$symbol" is usable.

+ 1 - 0
client/sso.js

@@ -1,4 +1,5 @@
 // Write your package code here!
+Meteor._debug("Loading client/sso", CHANNEL_NAME, EVENT_NAME);
 
 Meteor.startup(function () {
   var sso = new DrupalSSO();

+ 4 - 0
lib/constants.js

@@ -0,0 +1,4 @@
+Meteor._debug("Loading lib/constants");
+
+CHANNEL_NAME = 'drupal-sso:refresh';
+EVENT_NAME = 'drupal-sso:userRefresh';

+ 15 - 0
lib/drupalsso.js

@@ -1,3 +1,10 @@
+
+Meteor._debug("Loading lib/drupalsso");
+
+if (Meteor.isClient) {
+  var stream = new Meteor.Stream(CHANNEL_NAME);
+}
+
 /**
  * The SSO constructor.
  *
@@ -21,6 +28,7 @@ DrupalSSO = function () {
   // Work around "this" interpretation in local scope methods.
   var that = this;
 
+
   this.settings = {
     client: {}
   };
@@ -61,6 +69,13 @@ DrupalSSO = function () {
    * @param {string} cookies
    */
   this.updateUser = function (cookies) {
+    if (Meteor.isClient) {
+      Meteor._debug('Setting up once on ' + CHANNEL_NAME);
+      // Just listen once, since we rearm immediately.
+      stream.once(EVENT_NAME, function (e) {
+        that.updateUser(document.cookie);
+      });
+    }
     Meteor.call('drupal-sso.whoami', cookies, function (err, res) {
       if (err) {
         throw new Meteor.Error('whoami', err);

+ 4 - 0
package.js

@@ -8,11 +8,15 @@ Package.describe({
 
 Package.onUse(function(api) {
   api.versionsFrom('1.1.0.3');
+  api.use('arunoda:streams');
+  api.use('webapp');
 
+  api.addFiles('lib/constants.js');
   api.addFiles('lib/drupalsso.js');
   api.addFiles('client/helpers.js', 'client');
   api.addFiles('client/sso.js', 'client');
   api.addFiles('server/sso.js', 'server');
+  api.addFiles('server/web.js', 'server');
 
   api.export('DrupalSSO');
 });

+ 4 - 0
server/sso.js

@@ -1,5 +1,9 @@
+Meteor._debug("Loading server/sso");
+
 // Cause an error on startup if the Drupal server is unreachable.
 Meteor.startup(function () {
+  Meteor._debug("Startup server/sso", CHANNEL_NAME, EVENT_NAME);
+
   sso = new DrupalSSO();
   sso.initServerState(Meteor.settings);
 

+ 22 - 0
server/web.js

@@ -0,0 +1,22 @@
+Meteor._debug("Loading server/web");
+
+Meteor.startup(function () {
+  Meteor._debug("Startup server/sso", CHANNEL_NAME, EVENT_NAME);
+  var stream = new Meteor.Stream(CHANNEL_NAME);
+
+  WebApp.connectHandlers.use('/updateUser', function (req, res, next) {
+    res.writeHead(200);
+    res.end('Send refresh request');
+    Meteor._debug('Emitting refresh request.');
+    stream.emit(EVENT_NAME);
+  });
+
+  WebApp.connectHandlers.use('/updateUserDeferred', function (req, res, next) {
+    res.writeHead(200);
+    res.end('Send refresh request');
+    Meteor.setTimeout(function () {
+      Meteor._debug('Emitting refresh request.');
+      stream.emit(EVENT_NAME);
+    }, 1000);
+  });
+});