Skip to content

Commit

Permalink
Finalised
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo Scanferla committed Jul 5, 2014
1 parent f4d1745 commit 4cef1bb
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 63 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"gulpfile.js"
],
"dependencies": {
"ddp.js": "~0.4.0",
"ddp.js": "~0.5.0",
"q": "1.0.1"
}
}
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Asteroid demo</title>
<script src="bower_components/ddp.js/ddp.js"></script>
<script src="bower_components/ddp.js/src/ddp.js"></script>
<script src="bower_components/q/q.js"></script>
<script src="dist/asteroid.js"></script>
<script src="index.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ ceres.on("connected", function () {
console.log("Connected!");
});

window.addEventListener("message", function (e) {
console.log(e.data);
});

window.onload = function () {
document.getElementById("fb").addEventListener("click", function () {
ceres.loginWithFacebook()
Expand Down
96 changes: 55 additions & 41 deletions dist/asteroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ must.beObject = function (o) {
// Asteroid constructor //
//////////////////////////

var Asteroid = function (host, ssl, debug) {
var Asteroid = function (host, ssl, socketInterceptFunction) {
// Assert arguments type
must.beString(host);
// Configure the instance
Expand All @@ -131,13 +131,13 @@ var Asteroid = function (host, ssl, debug) {
this._ddpOptions = {
endpoint: (ssl ? "https://" : "http://") + host + "/sockjs",
SocketConstructor: SockJS,
debug: debug
socketInterceptFunction: socketInterceptFunction
};
} else {
this._ddpOptions = {
endpoint: (ssl ? "wss://" : "ws://") + host + "/websocket",
SocketConstructor: WebSocket,
debug: debug
socketInterceptFunction: socketInterceptFunction
};
}
// Reference containers
Expand Down Expand Up @@ -321,7 +321,7 @@ Asteroid.prototype.apply = function (method, params) {
// Syntactic sugar //
/////////////////////

Asteroid.prototype.createCollection = function (name) {
Asteroid.prototype.getCollection = function (name) {
// Assert arguments type
must.beString(name);
// Only create the collection if it doesn't exist
Expand Down Expand Up @@ -629,44 +629,44 @@ Asteroid.prototype._getOauthClientId = function (serviceName) {
};

Asteroid.prototype._initOauthLogin = function (service, credentialToken, loginUrl) {
var popup = window.open(loginUrl, '_blank', 'location=no,toolbar=no');
var self = this;
var isCordovaApp = !!window.cordova;
var popupclosed = false;

if(isCordovaApp){
popup.addEventListener('loaderror', function(e) {
setTimeout(function() {
popup.close();
}, 100);
});

popup.addEventListener('exit', function(e) {
popupclosed = true;
});
}

var popup = window.open(loginUrl, "_blank", "location=no,toolbar=no");
var self = this;
return Q()
.then(function () {
var deferred = Q.defer();
if (popup.focus) popup.focus();
var request = JSON.stringify({
credentialToken: credentialToken
});
var intervalId = setInterval(function () {
if (
( !isCordovaApp && (popup.closed || popup.closed === undefined) ) ||
( isCordovaApp && popupclosed )
)
{
clearInterval(intervalId);
deferred.resolve();
}
popup.postMessage(request, self._host);
}, 100);
window.addEventListener("message", function (e) {
var message;
try {
message = JSON.parse(e.data);
} catch (err) {
return;
}
if (e.origin === self._host) {
if (message.credentialToken === credentialToken) {
clearInterval(intervalId);
deferred.resolve(message.credentialSecret);
}
if (message.error) {
clearInterval(intervalId);
deferred.reject(message.error);
}
}
});
return deferred.promise;
})
.then(function () {
.then(function (credentialSecret) {
var deferred = Q.defer();
var loginParameters = {
oauth: {
credentialToken: credentialToken
credentialToken: credentialToken,
credentialSecret: credentialSecret
}
};
self.ddp.method("login", [loginParameters], function (err, res) {
Expand Down Expand Up @@ -987,20 +987,34 @@ Subscription.prototype._onError = function (err) {
// Subscribe method //
//////////////////////

Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// Assert arguments type
must.beString(name);
// Collect arguments into array
var params = Array.prototype.slice.call(arguments, 1);
var sub = new Subscription(name, params, this);
this.subscriptions[sub.id] = sub;
return sub;
};
Asteroid.prototype.subscribe = (function () {
// Memoize calls to the method, since subscribing to
// a resource twice with the same arguments yields the
// same results.
var calls = {};
// Actual subscribe function
return function (name /* , param1, param2, ... */) {
// Assert arguments type
must.beString(name);
// Hash the arguments (using JSON.stringify as hash function)
var hash = JSON.stringify(arguments);
// Only subscribe if there is no cached subscription
if (!calls[hash]) {
// Collect arguments into array
var params = Array.prototype.slice.call(arguments, 1);
calls[hash] = new Subscription(name, params, this);
this.subscriptions[sub.id] = calls[hash];
}
return calls[hash];
};
})();

Asteroid.prototype._reEstablishSubscriptions = function () {
var subs = this.subscriptions;
for (var id in subs) {
subs[id] = new Subscription(subs[id]._name, subs[id]._params, this);
if (subs.hasOwnProperty(id)) {
subs[id] = new Subscription(subs[id]._name, subs[id]._params, this);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/asteroid.min.js

Large diffs are not rendered by default.

40 changes: 27 additions & 13 deletions dist/node.asteroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ var WebSocket = require("faye-websocket");
// Asteroid constructor //
//////////////////////////

var Asteroid = function (host, ssl, debug) {
var Asteroid = function (host, ssl, socketInterceptFunction) {
// Assert arguments type
must.beString(host);
// Configure the instance
this._host = (ssl ? "https://" : "http://") + host;
this._ddpOptions = {
endpoint: (ssl ? "wss://" : "ws://") + host + "/websocket",
SocketConstructor: WebSocket.Client,
debug: debug
socketInterceptFunction: socketInterceptFunction
};
// Reference containers
this.collections = {};
Expand Down Expand Up @@ -304,7 +304,7 @@ Asteroid.prototype.apply = function (method, params) {
// Syntactic sugar //
/////////////////////

Asteroid.prototype.createCollection = function (name) {
Asteroid.prototype.getCollection = function (name) {
// Assert arguments type
must.beString(name);
// Only create the collection if it doesn't exist
Expand Down Expand Up @@ -826,20 +826,34 @@ Subscription.prototype._onError = function (err) {
// Subscribe method //
//////////////////////

Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) {
// Assert arguments type
must.beString(name);
// Collect arguments into array
var params = Array.prototype.slice.call(arguments, 1);
var sub = new Subscription(name, params, this);
this.subscriptions[sub.id] = sub;
return sub;
};
Asteroid.prototype.subscribe = (function () {
// Memoize calls to the method, since subscribing to
// a resource twice with the same arguments yields the
// same results.
var calls = {};
// Actual subscribe function
return function (name /* , param1, param2, ... */) {
// Assert arguments type
must.beString(name);
// Hash the arguments (using JSON.stringify as hash function)
var hash = JSON.stringify(arguments);
// Only subscribe if there is no cached subscription
if (!calls[hash]) {
// Collect arguments into array
var params = Array.prototype.slice.call(arguments, 1);
calls[hash] = new Subscription(name, params, this);
this.subscriptions[sub.id] = calls[hash];
}
return calls[hash];
};
})();

Asteroid.prototype._reEstablishSubscriptions = function () {
var subs = this.subscriptions;
for (var id in subs) {
subs[id] = new Subscription(subs[id]._name, subs[id]._params, this);
if (subs.hasOwnProperty(id)) {
subs[id] = new Subscription(subs[id]._name, subs[id]._params, this);
}
}
};

Expand Down
15 changes: 9 additions & 6 deletions src/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ Asteroid.prototype._initOauthLogin = function (service, credentialToken, loginUr
} catch (err) {
return;
}
if (
e.origin === self._host &&
message.credentialToken === credentialToken
) {
clearInterval(intervalId);
deferred.resolve(message.credentialSecret);
if (e.origin === self._host) {
if (message.credentialToken === credentialToken) {
clearInterval(intervalId);
deferred.resolve(message.credentialSecret);
}
if (message.error) {
clearInterval(intervalId);
deferred.reject(message.error);
}
}
});
return deferred.promise;
Expand Down

0 comments on commit 4cef1bb

Please sign in to comment.