From 550033ef2f3fa396b1f1e920407b2c560cb22de1 Mon Sep 17 00:00:00 2001 From: Gareth Powell Date: Thu, 17 Oct 2013 16:53:33 -0400 Subject: [PATCH 1/2] Resolved race condition when starting adapter after logging in. --- app/initializers/ziniki.js | 1 - app/routes/application.js | 4 +- glazier-server | 2 +- vendor/ziniki-ea.js | 352 ---------------------- vendor/ziniki-sr.js | 175 ----------- vendor/ziniki.js | 304 ------------------- vendor/ziniki/ziniki-ea.js | 2 +- vendor/ziniki/ziniki.js | 581 +++++++++++++++++++------------------ 8 files changed, 299 insertions(+), 1122 deletions(-) delete mode 100644 vendor/ziniki-ea.js delete mode 100644 vendor/ziniki-sr.js delete mode 100644 vendor/ziniki.js diff --git a/app/initializers/ziniki.js b/app/initializers/ziniki.js index d7f5528..bf56c55 100644 --- a/app/initializers/ziniki.js +++ b/app/initializers/ziniki.js @@ -9,7 +9,6 @@ var initializer = { after: 'store', initialize: function (container, application) { - Ember.Logger.log("Should be an initializer"); Ziniki.init(host + "/resources", host + "/adapter"); var dashboard = ZinikiSerializer.mapType(Glazier.Dashboard, "dashboard", "us.yapp.glazier.Dashboard", "us.yapp.glazier.dashboard"); // Glazier's rails server stupidly uses non-standard ids diff --git a/app/routes/application.js b/app/routes/application.js index 2d9c887..2a87df0 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -5,14 +5,12 @@ var ApplicationRoute = Ember.Route.extend({ var userController = this.controllerFor('user'); var login = this.container.lookup('behavior:login'); - login().then(function(user){ + return login().then(function(user){ // null object pattern might be better. if (user) { userController.set('content', user); } }); - - return null; // Ziniki related race condition }, setupController: function(data) { this.controllerFor('application').set('isReady', true); diff --git a/glazier-server b/glazier-server index daae008..50b5cfb 160000 --- a/glazier-server +++ b/glazier-server @@ -1 +1 @@ -Subproject commit daae0082c25bef6e9d79cfb3cd9f6f9f198ca5b3 +Subproject commit 50b5cfb37534fe23be21b4858510bdb1a45dca3c diff --git a/vendor/ziniki-ea.js b/vendor/ziniki-ea.js deleted file mode 100644 index 5fb9b4b..0000000 --- a/vendor/ziniki-ea.js +++ /dev/null @@ -1,352 +0,0 @@ -define("ziniki/ea", - ["ziniki", "ziniki/sr"], function (Ziniki, ZinikiSerializer) { - - "use strict"; - - /** When issuing requests to the server, we need to keep a record of - * what we expected them to do and any other contextual information so - * that we can handle the response. - * - * We issue requests in a batch and expect to get a response which is the - * same "shape" (i.e. an array with the same number of entries and compatible - * with the answers). - */ - function Batch(webSocket) { - this.requests = []; - this.payload = []; - - /** Add a request to the batch. Append the actual request object - * to requests, and its serialization into payload - * - * request - any suitable request object (Find, FindAll, Create, etc) - */ - this.add = function(request) { - this.requests.push(request); - this.payload.push(request.serialize()); - } - - /** When the batch is ready, send it across to the server - */ - this.send = function() { - webSocket.send(this.payload, this, this.respond); - } - - /** Handle the incoming batch response by processing each of the request elements in turn - * and calling the original request with the slice of response. - * - * json - the JSON object which comprises the response - */ - this.respond = function(json) { - if (json.length != this.requests.length) - throw "The response had a different number of entries to the request (" + json.length + "!=" + this.requests.length + ")"; - for (var i=0;i Date: Fri, 25 Oct 2013 07:38:22 -0400 Subject: [PATCH 2/2] Used latest ziniki.js --- vendor/ziniki/glazier-overrides.js | 2 +- vendor/ziniki/ziniki.js | 297 +++++++++++++++++++---------- 2 files changed, 193 insertions(+), 106 deletions(-) diff --git a/vendor/ziniki/glazier-overrides.js b/vendor/ziniki/glazier-overrides.js index f65576b..07b0748 100644 --- a/vendor/ziniki/glazier-overrides.js +++ b/vendor/ziniki/glazier-overrides.js @@ -4,7 +4,7 @@ define("glazier/behaviors/login", "use strict"; function login() { // There should be a proper login screen - return Ziniki.login("basic", "gareth", { password:"powell"}); + return Ziniki.login("gareth", "powell"); } return login; } diff --git a/vendor/ziniki/ziniki.js b/vendor/ziniki/ziniki.js index 8d7ce91..8d70b79 100644 --- a/vendor/ziniki/ziniki.js +++ b/vendor/ziniki/ziniki.js @@ -3,14 +3,67 @@ define("ziniki", "use strict"; +/* Interface to Ziniki from JavaScript + * (C) 2013 Ziniki Infrastructure Software, LLC + * Author: Gareth Powell + */ var Ziniki = {}; +function getURLParameterByName(window, name) +{ + name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); + var regexS = "[\\?&]" + name + "=([^&#]*)"; + var regex = new RegExp(regexS); + var results = regex.exec(window.location.search); + if (results == null) + return ""; + else + return decodeURIComponent(results[1].replace(/\+/g, " ")); +} var url = null; var wsUrl = null; var webSocket = null; - var token = null; var performingLogin = false; -//var onSuccessfulLogin = []; + + function setLoggedInUser(newValue) + { + sessionStorage.setItem("userName", newValue.name); + sessionStorage.setItem("userToken", newValue.token); + } + + function clearLoggedInUser() + { + sessionStorage.removeItem("userName"); + sessionStorage.removeItem("userToken"); + } + + function getLoggedInUser() + { + var name = sessionStorage.getItem("userName"); + var token = sessionStorage.getItem("userToken"); + return token ? new LoggedInUser(name, token) : null; + } + + function getLoggedInUserName() + { + return getLoggedInUser().name; + } + + function getToken() + { + var user = getLoggedInUser(); + return user ? user.token : null; + } + + function hasToken() + { + return getToken() != null; + } + + // If you already have a token, you can call this + function reuseToken(inToken) { + setLoggedInUser(new LoggedInUser(inToken, inToken)); + }; /** We need to keep track of all server requests by id * and then map those ids back to the requests so that @@ -52,16 +105,16 @@ define("ziniki", }; this.conn = function() { - if (!this._conn) { + if (!this._conn) this._conn = this.create(); - return this._conn; - }; + return this._conn; } /** Do the work to create a websocket: initialize atmosphere, * pass a request, and handle the "open" message */ this.create = function() { + var token = getToken(); if (token == null && !performingLogin) throw "Cannot open websocket until logged in"; var self = this; @@ -81,18 +134,8 @@ define("ziniki", // and then handle each incoming message onMessage: function(response) { var body = response.responseBody; - if (body == "Confirm") { + if (body == "Confirm") resolve(actualConn); - // All replaced by promise -// if (onReady) { -// if (!obj) -// onReady(self); -// else -// onReady.apply(obj, [self]); -// } -// for (var i=0;i