From d038cb538376c1cc8c60f705f12f3b6a48373f2f Mon Sep 17 00:00:00 2001 From: Paolo Scanferla Date: Thu, 10 Jul 2014 22:40:28 +0200 Subject: [PATCH] Rebuilt dist files. Version bump --- bower.json | 2 +- dist/asteroid.js | 6 ++++-- dist/asteroid.min.js | 2 +- dist/node.asteroid.js | 6 ++++-- package.json | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/bower.json b/bower.json index 2d37198..69d3617 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "asteroid", - "version": "0.3.3", + "version": "0.3.4", "homepage": "https://github.com/mondora/asteroid", "authors": [ "Paolo Scanferla " diff --git a/dist/asteroid.js b/dist/asteroid.js index 61153ad..a6aa20e 100644 --- a/dist/asteroid.js +++ b/dist/asteroid.js @@ -992,9 +992,10 @@ Asteroid.Set = Set; // Subscription class // //////////////////////// -var Subscription = function (name, params, asteroid) { +var Subscription = function (name, params, hash, asteroid) { this._name = name; this._params = params; + this._hash = hash; this._asteroid = asteroid; // Subscription promises this._ready = Q.defer(); @@ -1009,6 +1010,7 @@ Subscription.constructor = Subscription; Subscription.prototype.stop = function () { this._asteroid.ddp.unsub(this.id); + delete this._asteroid._subscriptionsCache[this._hash]; }; Subscription.prototype._onReady = function () { @@ -1041,7 +1043,7 @@ Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) { if (!this._subscriptionsCache[hash]) { // Collect arguments into array var params = Array.prototype.slice.call(arguments, 1); - var sub = new Subscription(name, params, this); + var sub = new Subscription(name, params, hash, this); this._subscriptionsCache[hash] = sub; this.subscriptions[sub.id] = sub; } diff --git a/dist/asteroid.min.js b/dist/asteroid.min.js index 37e6650..05ff65d 100644 --- a/dist/asteroid.min.js +++ b/dist/asteroid.min.js @@ -1 +1 @@ -!function(t,e){"function"==typeof define&&define.amd?define(e):"object"==typeof exports?module.exports=e():t.Asteroid=e()}(this,function(){"use strict";function t(t){if("undefined"!=typeof EJSON)return EJSON.clone(t);var e=typeof t;switch(e){case"undefined":case"function":return void 0;case"string":case"number":case"boolean":return t;case"object":return null===t?null:JSON.parse(JSON.stringify(t));default:return}}function e(t){var e="";for(var o in t)e+=o+"="+t[o]+"&";return e=e.slice(0,-1)}function o(){for(var t="",e=0;8>e;e++)t+=Math.floor(65536*(1+Math.random())).toString(16).substring(1);return t}function i(t){return-1!==t.indexOf("@")}var r=function(){};r.prototype={constructor:r,on:function(t,e){this._events||(this._events={}),this._events[t]=this._events[t]||[],this._events[t].push(e)},off:function(t,e){this._events||(this._events={}),this._events[t]&&this._events[t].splice(this._events[t].indexOf(e),1)},_emit:function(t){if(this._events||(this._events={}),this._events[t]){var e=arguments,o=this;this._events[t].forEach(function(t){t.apply(o,Array.prototype.slice.call(e,1))})}}};var n={};n._toString=function(t){return Object.prototype.toString.call(t).slice(8,-1)},n.beString=function(t){var e=this._toString(t);if("String"!==e)throw new Error("Assertion failed: expected String, instead got "+e)},n.beArray=function(t){var e=this._toString(t);if("Array"!==e)throw new Error("Assertion failed: expected Array, instead got "+e)},n.beObject=function(t){var e=this._toString(t);if("Object"!==e)throw new Error("Assertion failed: expected Object, instead got "+e)};var s=function(t,e,o){n.beString(t),this._host=(e?"https://":"http://")+t,this._ddpOptions="function"==typeof SockJS?{endpoint:(e?"https://":"http://")+t+"/sockjs",SocketConstructor:SockJS,socketInterceptFunction:o}:{endpoint:(e?"wss://":"ws://")+t+"/websocket",SocketConstructor:WebSocket,socketInterceptFunction:o},this.collections={},this.subscriptions={},this._subscriptionsCache={},this._init()};s.prototype=Object.create(r.prototype),s.prototype.constructor=s,s.prototype._init=function(){var t=this;t.ddp=new DDP(this._ddpOptions),t.ddp.on("connected",function(){t.resumeLoginPromise=t._tryResumeLogin(),t.subscribe("meteor.loginServiceConfiguration"),t._emit("connected")}),t.ddp.on("reconnected",function(){t.resumeLoginPromise=t._tryResumeLogin(),t._reEstablishSubscriptions(),t._emit("reconnected")}),t.ddp.on("added",function(e){t._onAdded(e)}),t.ddp.on("changed",function(e){t._onChanged(e)}),t.ddp.on("removed",function(e){t._onRemoved(e)})},s.prototype._onAdded=function(t){var e=t.collection;this.collections[e]||(this.collections[e]=new s._Collection(e,this));var o=t.fields||{};o._id=t.id,this.collections[e]._remoteToLocalInsert(o)},s.prototype._onRemoved=function(t){this.collections[t.collection]&&this.collections[t.collection]._remoteToLocalRemove(t.id)},s.prototype._onChanged=function(t){this.collections[t.collection]&&(t.fields||(t.fields={}),t.cleared&&t.cleared.forEach(function(e){t.fields[e]=void 0}),this.collections[t.collection]._remoteToLocalUpdate(t.id,t.fields))},s.prototype.call=function(t){n.beString(t);var e=Array.prototype.slice.call(arguments,1);return this.apply(t,e)},s.prototype.apply=function(t,e){n.beString(t),Array.isArray(e)||(e=[]);var o=Q.defer(),i=Q.defer(),r=function(t,e){t?(o.reject(t),i.reject()):o.resolve(e)},s=function(){i.resolve()};return this.ddp.method(t,e,r,s),{result:o.promise,updated:i.promise}},s.prototype.getCollection=function(t){return n.beString(t),this.collections[t]||(this.collections[t]=new s._Collection(t,this)),this.collections[t]};var c="__del__",l="__upd__",a=function(t){var e=c.length,o=l.length,i=t.slice(-1*e),r=t.slice(-1*o);return i===c||r===l},u=function(t,e){this.name=t,this.asteroid=e,this._set=new p};u.prototype.constructor=u,u.prototype._localToLocalInsert=function(t){if(this._set.contains(t._id))throw new Error("Item "+t._id+" already exists");return this._set.put(t._id,t),Q(t._id)},u.prototype._remoteToLocalInsert=function(t){this._set.put(t._id,t)},u.prototype._localToRemoteInsert=function(t){var e=this,o=Q.defer(),i="/"+e.name+"/insert";return e.asteroid.ddp.method(i,[t],function(i){i?(e._set.del(t._id),o.reject(i)):o.resolve(t._id)}),o.promise},u.prototype.insert=function(t){return t._id||(t._id=o()),{local:this._localToLocalInsert(t),remote:this._localToRemoteInsert(t)}},u.prototype._localToLocalRemove=function(t){var e=this._set.get(t);return e&&(this._set.put(t+c,e),this._set.del(t)),Q(t)},u.prototype._remoteToLocalRemove=function(t){this._set.del(t)},u.prototype._localToRemoteRemove=function(t){var e=this,o=Q.defer(),i="/"+e.name+"/remove";return e.asteroid.ddp.method(i,[{_id:t}],function(i){if(i){var r=e._set.get(t+c);r&&(e._set.put(t,r),e._set.del(t+c)),o.reject(i)}else e._set.del(t+c),o.resolve(t)}),o.promise},u.prototype.remove=function(t){return{local:this._localToLocalRemove(t),remote:this._localToRemoteRemove(t)}},u.prototype._localToLocalUpdate=function(t,e){var o=this._set.get(t);if(!o)throw new Error("Item "+t+" doesn't exist");if(e._id&&e._id!==t)throw new Error("Modifying the _id of a document is not allowed");this._set.put(t+l,o);for(var i in e)o[i]=e[i];return this._set.put(t,o),Q(t)},u.prototype._remoteToLocalUpdate=function(t,e){var o=this._set.get(t);if(!o)return void console.warn("Server misbehaviour: item "+t+" doesn't exist");for(var i in e){if("_id"===i&&e._id!==t)return void console.warn("Server misbehaviour: modifying the _id of a document is not allowed");o[i]=e[i]}this._set.put(t,o)},u.prototype._localToRemoteUpdate=function(t,e){var o=this,i=Q.defer(),r="/"+o.name+"/update",n={_id:t},s={$set:e};return o.asteroid.ddp.method(r,[n,s],function(e){if(e){var r=o._set.get(t+l);o._set.put(t,r),o._set.del(t+l),i.reject(e)}else o._set.del(t+l),i.resolve(t)}),i.promise},u.prototype.update=function(t,e){return{local:this._localToLocalUpdate(t,e),remote:this._localToRemoteUpdate(t,e)}};var d=function(t){var e=this;e.result=[],e._set=t,e._getResult(),e._set.on("put",function(t){e._getResult(),e._emit("change",t)}),e._set.on("del",function(t){e._getResult(),e._emit("change",t)})};d.prototype=Object.create(r.prototype),d.constructor=d,d.prototype._getResult=function(){this.result=this._set.toArray()};var _=function(t){return function(e,o){if(a(e))return!1;var i=function(t,e){return e.split(".").reduce(function(t,e){return t?t=t[e]:t},t)};for(var r in t){var n=i(o,r);if(n!==t[r])return!1}return!0}};u.prototype.reactiveQuery=function(t){var e;e="function"==typeof t?t:_(t);var o=this._set.filter(e);return new d(o)},s._Collection=u,s.prototype._getOauthClientId=function(t){var e="meteor_accounts_loginServiceConfiguration",o=this.collections[e],i=o.reactiveQuery({service:t}).result[0];return i.clientId||i.consumerKey||i.appId},s.prototype._initOauthLogin=function(t,e,o){var i=window.open(o,"_blank","location=no,toolbar=no");i.focus&&i.focus();var r=this;return Q().then(function(){var t=Q.defer();if(window.cordova)i.addEventListener("loadstop",function(o){if(-1!==o.url.indexOf("#")){var r=o.url.indexOf("#"),n=o.url.slice(r+1).split("&");if(n[0]&&"credentialToken"===n[0].split("=")[0]&&n[1]&&"credentialSecret"===n[1].split("=")[0]){var s=n[0].split("=")[1],c=n[1].split("=")[1];s===e&&(t.resolve(c),i.close())}}});else{var o=JSON.stringify({credentialToken:e}),n=setInterval(function(){i.postMessage(o,r._host)},100);window.addEventListener("message",function(o){var i;try{i=JSON.parse(o.data)}catch(s){return}o.origin===r._host&&(i.credentialToken===e&&(clearInterval(n),t.resolve(i.credentialSecret)),i.error&&(clearInterval(n),t.reject(i.error)))})}return t.promise}).then(function(t){var o=Q.defer(),i={oauth:{credentialToken:e,credentialSecret:t}};return r.ddp.method("login",[i],function(t,e){t?(delete r.userId,delete r.loggedIn,delete localStorage[r._host+"__login_token__"],o.reject(t),r._emit("loginError",t)):(r.userId=e.id,r.loggedIn=!0,localStorage[r._host+"__login_token__"]=e.token,r._emit("login",e.id),o.resolve(e.id))}),o.promise})},s.prototype._tryResumeLogin=function(){var t=this,e=Q.defer(),o=localStorage[t._host+"__login_token__"];if(!o)return e.reject("No login token"),e.promise;var i={resume:o};return t.ddp.method("login",[i],function(o,i){o?(delete t.userId,delete t.loggedIn,delete localStorage[t._host+"__login_token__"],t._emit("loginError",o),e.reject(o)):(t.userId=i.id,t.loggedIn=!0,localStorage[t._host+"__login_token__"]=i.token,t._emit("login",i.id),e.resolve(i.id))}),e.promise},s.prototype.loginWithFacebook=function(t){var i=o(),r={client_id:this._getOauthClientId("facebook"),redirect_uri:this._host+"/_oauth/facebook?close",state:i,scope:t||"email"},n="https://www.facebook.com/dialog/oauth?"+e(r);return this._initOauthLogin("facebook",i,n)},s.prototype.loginWithGoogle=function(t){var i=o(),r={response_type:"code",client_id:this._getOauthClientId("google"),redirect_uri:this._host+"/_oauth/google?close",state:i,scope:t||"openid email"},n="https://accounts.google.com/o/oauth2/auth?"+e(r);return this._initOauthLogin("google",i,n)},s.prototype.loginWithGithub=function(t){var i=o(),r={client_id:this._getOauthClientId("github"),redirect_uri:this._host+"/_oauth/github?close",state:i,scope:t||"email"},n="https://github.com/login/oauth/authorize?"+e(r);return this._initOauthLogin("github",i,n)},s.prototype.loginWithTwitter=function(){var t=o(),i=this._host+"/_oauth/twitter?close&state="+t,r={requestTokenAndRedirect:encodeURIComponent(i),state:t},n=this._host+"/_oauth/twitter/?"+e(r);return this._initOauthLogin("twitter",t,n)},s.prototype.createUser=function(t,e,o){var r=this,n=Q.defer(),s={username:i(t)?void 0:t,email:i(t)?t:void 0,password:e,profile:o};return r.ddp.method("createUser",[s],function(t,e){t?(r._emit("createUserError",t),n.reject(t)):(r.userId=e.id,r.loggedIn=!0,localStorage[r._host+"__login_token__"]=e.token,r._emit("createUser",e.id),r._emit("login",e.id),n.resolve(e.id))}),n.promise},s.prototype.loginWithPassword=function(t,e){var o=this,r=Q.defer(),n={password:e,user:{username:i(t)?void 0:t,email:i(t)?t:void 0}};return o.ddp.method("login",[n],function(t,e){t?(delete o.userId,delete o.loggedIn,delete localStorage[o._host+"__login_token__"],r.reject(t),o._emit("loginError",t)):(o.userId=e.id,o.loggedIn=!0,localStorage[o._host+"__login_token__"]=e.token,o._emit("login",e.id),r.resolve(e.id))}),r.promise},s.prototype.logout=function(){var t=this,e=Q.defer();return t.ddp.method("logout",[],function(o){o?(t._emit("logoutError",o),e.reject(o)):(delete t.userId,delete t.loggedIn,delete localStorage[t._host+"__login_token__"],t._emit("logout"),e.resolve())}),e.promise};var p=function(t){t&&(this._put=this.put,this._del=this.del,this.put=this.del=function(){throw new Error("Attempt to modify readonly set")}),this._items={}};p.prototype=Object.create(r.prototype),p.constructor=p,p.prototype.put=function(e,o){return n.beString(e),n.beObject(o),this._items[e]=t(o),this._emit("put",e),this},p.prototype.del=function(t){return n.beString(t),delete this._items[t],this._emit("del",t),this},p.prototype.get=function(e){return n.beString(e),t(this._items[e])},p.prototype.contains=function(t){return n.beString(t),!!this._items[t]},p.prototype.filter=function(e){var o=new p(!0),i=this._items,r=Object.keys(i);return r.forEach(function(r){var n=t(i[r]),s=e(r,n);s&&(o._items[r]=i[r])}),this.on("put",function(r){var n=t(i[r]),s=e(r,n);s&&o._put(r,i[r])}),this.on("del",function(t){o._del(t)}),o},p.prototype.toArray=function(){var e=[],o=this._items,i=Object.keys(this._items);return i.forEach(function(t){e.push(o[t])}),t(e)},p.prototype.toHash=function(){return t(this._items)},s.Set=p;var h=function(t,e,o){this._name=t,this._params=e,this._asteroid=o,this._ready=Q.defer(),this.ready=this._ready.promise;var i=this._onReady.bind(this),r=this._onStop.bind(this),n=this._onError.bind(this);this.id=o.ddp.sub(t,e,i,r,n)};return h.constructor=h,h.prototype.stop=function(){this._asteroid.ddp.unsub(this.id)},h.prototype._onReady=function(){this._ready.resolve(this.id)},h.prototype._onStop=function(){delete this._asteroid.subscriptions[this.id]},h.prototype._onError=function(t){this.ready.isPending()&&this._ready.reject(t),delete this._asteroid.subscriptions[this.id]},s.prototype.subscribe=function(t){n.beString(t);var e=JSON.stringify(arguments);if(!this._subscriptionsCache[e]){var o=Array.prototype.slice.call(arguments,1),i=new h(t,o,this);this._subscriptionsCache[e]=i,this.subscriptions[i.id]=i}return this._subscriptionsCache[e]},s.prototype._reEstablishSubscriptions=function(){var t=this.subscriptions;for(var e in t)t.hasOwnProperty(e)&&(t[e]=new h(t[e]._name,t[e]._params,this))},s}); \ No newline at end of file +!function(t,e){"function"==typeof define&&define.amd?define(e):"object"==typeof exports?module.exports=e():t.Asteroid=e()}(this,function(){"use strict";function t(t){if("undefined"!=typeof EJSON)return EJSON.clone(t);var e=typeof t;switch(e){case"undefined":case"function":return void 0;case"string":case"number":case"boolean":return t;case"object":return null===t?null:JSON.parse(JSON.stringify(t));default:return}}function e(t){var e="";for(var o in t)e+=o+"="+t[o]+"&";return e=e.slice(0,-1)}function o(){for(var t="",e=0;8>e;e++)t+=Math.floor(65536*(1+Math.random())).toString(16).substring(1);return t}function i(t){return-1!==t.indexOf("@")}var r=function(){};r.prototype={constructor:r,on:function(t,e){this._events||(this._events={}),this._events[t]=this._events[t]||[],this._events[t].push(e)},off:function(t,e){this._events||(this._events={}),this._events[t]&&this._events[t].splice(this._events[t].indexOf(e),1)},_emit:function(t){if(this._events||(this._events={}),this._events[t]){var e=arguments,o=this;this._events[t].forEach(function(t){t.apply(o,Array.prototype.slice.call(e,1))})}}};var n={};n._toString=function(t){return Object.prototype.toString.call(t).slice(8,-1)},n.beString=function(t){var e=this._toString(t);if("String"!==e)throw new Error("Assertion failed: expected String, instead got "+e)},n.beArray=function(t){var e=this._toString(t);if("Array"!==e)throw new Error("Assertion failed: expected Array, instead got "+e)},n.beObject=function(t){var e=this._toString(t);if("Object"!==e)throw new Error("Assertion failed: expected Object, instead got "+e)};var s=function(t,e,o){n.beString(t),this._host=(e?"https://":"http://")+t,this._ddpOptions="function"==typeof SockJS?{endpoint:(e?"https://":"http://")+t+"/sockjs",SocketConstructor:SockJS,socketInterceptFunction:o}:{endpoint:(e?"wss://":"ws://")+t+"/websocket",SocketConstructor:WebSocket,socketInterceptFunction:o},this.collections={},this.subscriptions={},this._subscriptionsCache={},this._init()};s.prototype=Object.create(r.prototype),s.prototype.constructor=s,s.prototype._init=function(){var t=this;t.ddp=new DDP(this._ddpOptions),t.ddp.on("connected",function(){t.resumeLoginPromise=t._tryResumeLogin(),t.subscribe("meteor.loginServiceConfiguration"),t._emit("connected")}),t.ddp.on("reconnected",function(){t.resumeLoginPromise=t._tryResumeLogin(),t._reEstablishSubscriptions(),t._emit("reconnected")}),t.ddp.on("added",function(e){t._onAdded(e)}),t.ddp.on("changed",function(e){t._onChanged(e)}),t.ddp.on("removed",function(e){t._onRemoved(e)})},s.prototype._onAdded=function(t){var e=t.collection;this.collections[e]||(this.collections[e]=new s._Collection(e,this));var o=t.fields||{};o._id=t.id,this.collections[e]._remoteToLocalInsert(o)},s.prototype._onRemoved=function(t){this.collections[t.collection]&&this.collections[t.collection]._remoteToLocalRemove(t.id)},s.prototype._onChanged=function(t){this.collections[t.collection]&&(t.fields||(t.fields={}),t.cleared&&t.cleared.forEach(function(e){t.fields[e]=void 0}),this.collections[t.collection]._remoteToLocalUpdate(t.id,t.fields))},s.prototype.call=function(t){n.beString(t);var e=Array.prototype.slice.call(arguments,1);return this.apply(t,e)},s.prototype.apply=function(t,e){n.beString(t),Array.isArray(e)||(e=[]);var o=Q.defer(),i=Q.defer(),r=function(t,e){t?(o.reject(t),i.reject()):o.resolve(e)},s=function(){i.resolve()};return this.ddp.method(t,e,r,s),{result:o.promise,updated:i.promise}},s.prototype.getCollection=function(t){return n.beString(t),this.collections[t]||(this.collections[t]=new s._Collection(t,this)),this.collections[t]};var c="__del__",a="__upd__",l=function(t){var e=c.length,o=a.length,i=t.slice(-1*e),r=t.slice(-1*o);return i===c||r===a},u=function(t,e){this.name=t,this.asteroid=e,this._set=new h};u.prototype.constructor=u,u.prototype._localToLocalInsert=function(t){if(this._set.contains(t._id))throw new Error("Item "+t._id+" already exists");return this._set.put(t._id,t),Q(t._id)},u.prototype._remoteToLocalInsert=function(t){this._set.put(t._id,t)},u.prototype._localToRemoteInsert=function(t){var e=this,o=Q.defer(),i="/"+e.name+"/insert";return e.asteroid.ddp.method(i,[t],function(i){i?(e._set.del(t._id),o.reject(i)):o.resolve(t._id)}),o.promise},u.prototype.insert=function(t){return t._id||(t._id=o()),{local:this._localToLocalInsert(t),remote:this._localToRemoteInsert(t)}},u.prototype._localToLocalRemove=function(t){var e=this._set.get(t);return e&&(this._set.put(t+c,e),this._set.del(t)),Q(t)},u.prototype._remoteToLocalRemove=function(t){this._set.del(t)},u.prototype._localToRemoteRemove=function(t){var e=this,o=Q.defer(),i="/"+e.name+"/remove";return e.asteroid.ddp.method(i,[{_id:t}],function(i){if(i){var r=e._set.get(t+c);r&&(e._set.put(t,r),e._set.del(t+c)),o.reject(i)}else e._set.del(t+c),o.resolve(t)}),o.promise},u.prototype.remove=function(t){return{local:this._localToLocalRemove(t),remote:this._localToRemoteRemove(t)}},u.prototype._localToLocalUpdate=function(t,e){var o=this._set.get(t);if(!o)throw new Error("Item "+t+" doesn't exist");if(e._id&&e._id!==t)throw new Error("Modifying the _id of a document is not allowed");this._set.put(t+a,o);for(var i in e)o[i]=e[i];return this._set.put(t,o),Q(t)},u.prototype._remoteToLocalUpdate=function(t,e){var o=this._set.get(t);if(!o)return void console.warn("Server misbehaviour: item "+t+" doesn't exist");for(var i in e){if("_id"===i&&e._id!==t)return void console.warn("Server misbehaviour: modifying the _id of a document is not allowed");o[i]=e[i]}this._set.put(t,o)},u.prototype._localToRemoteUpdate=function(t,e){var o=this,i=Q.defer(),r="/"+o.name+"/update",n={_id:t},s={$set:e};return o.asteroid.ddp.method(r,[n,s],function(e){if(e){var r=o._set.get(t+a);o._set.put(t,r),o._set.del(t+a),i.reject(e)}else o._set.del(t+a),i.resolve(t)}),i.promise},u.prototype.update=function(t,e){return{local:this._localToLocalUpdate(t,e),remote:this._localToRemoteUpdate(t,e)}};var d=function(t){var e=this;e.result=[],e._set=t,e._getResult(),e._set.on("put",function(t){e._getResult(),e._emit("change",t)}),e._set.on("del",function(t){e._getResult(),e._emit("change",t)})};d.prototype=Object.create(r.prototype),d.constructor=d,d.prototype._getResult=function(){this.result=this._set.toArray()};var _=function(t){return function(e,o){if(l(e))return!1;var i=function(t,e){return e.split(".").reduce(function(t,e){return t?t=t[e]:t},t)};for(var r in t){var n=i(o,r);if(n!==t[r])return!1}return!0}};u.prototype.reactiveQuery=function(t){var e;e="function"==typeof t?t:_(t);var o=this._set.filter(e);return new d(o)},s._Collection=u,s.prototype._getOauthClientId=function(t){var e="meteor_accounts_loginServiceConfiguration",o=this.collections[e],i=o.reactiveQuery({service:t}).result[0];return i.clientId||i.consumerKey||i.appId},s.prototype._initOauthLogin=function(t,e,o){var i=window.open(o,"_blank","location=no,toolbar=no");i.focus&&i.focus();var r=this;return Q().then(function(){var t=Q.defer();if(window.cordova)i.addEventListener("loadstop",function(o){if(-1!==o.url.indexOf("#")){var r=o.url.indexOf("#"),n=o.url.slice(r+1).split("&");if(n[0]&&"credentialToken"===n[0].split("=")[0]&&n[1]&&"credentialSecret"===n[1].split("=")[0]){var s=n[0].split("=")[1],c=n[1].split("=")[1];s===e&&(t.resolve(c),i.close())}}});else{var o=JSON.stringify({credentialToken:e}),n=setInterval(function(){i.postMessage(o,r._host)},100);window.addEventListener("message",function(o){var i;try{i=JSON.parse(o.data)}catch(s){return}o.origin===r._host&&(i.credentialToken===e&&(clearInterval(n),t.resolve(i.credentialSecret)),i.error&&(clearInterval(n),t.reject(i.error)))})}return t.promise}).then(function(t){var o=Q.defer(),i={oauth:{credentialToken:e,credentialSecret:t}};return r.ddp.method("login",[i],function(t,e){t?(delete r.userId,delete r.loggedIn,delete localStorage[r._host+"__login_token__"],o.reject(t),r._emit("loginError",t)):(r.userId=e.id,r.loggedIn=!0,localStorage[r._host+"__login_token__"]=e.token,r._emit("login",e.id),o.resolve(e.id))}),o.promise})},s.prototype._tryResumeLogin=function(){var t=this,e=Q.defer(),o=localStorage[t._host+"__login_token__"];if(!o)return e.reject("No login token"),e.promise;var i={resume:o};return t.ddp.method("login",[i],function(o,i){o?(delete t.userId,delete t.loggedIn,delete localStorage[t._host+"__login_token__"],t._emit("loginError",o),e.reject(o)):(t.userId=i.id,t.loggedIn=!0,localStorage[t._host+"__login_token__"]=i.token,t._emit("login",i.id),e.resolve(i.id))}),e.promise},s.prototype.loginWithFacebook=function(t){var i=o(),r={client_id:this._getOauthClientId("facebook"),redirect_uri:this._host+"/_oauth/facebook?close",state:i,scope:t||"email"},n="https://www.facebook.com/dialog/oauth?"+e(r);return this._initOauthLogin("facebook",i,n)},s.prototype.loginWithGoogle=function(t){var i=o(),r={response_type:"code",client_id:this._getOauthClientId("google"),redirect_uri:this._host+"/_oauth/google?close",state:i,scope:t||"openid email"},n="https://accounts.google.com/o/oauth2/auth?"+e(r);return this._initOauthLogin("google",i,n)},s.prototype.loginWithGithub=function(t){var i=o(),r={client_id:this._getOauthClientId("github"),redirect_uri:this._host+"/_oauth/github?close",state:i,scope:t||"email"},n="https://github.com/login/oauth/authorize?"+e(r);return this._initOauthLogin("github",i,n)},s.prototype.loginWithTwitter=function(){var t=o(),i=this._host+"/_oauth/twitter?close&state="+t,r={requestTokenAndRedirect:encodeURIComponent(i),state:t},n=this._host+"/_oauth/twitter/?"+e(r);return this._initOauthLogin("twitter",t,n)},s.prototype.createUser=function(t,e,o){var r=this,n=Q.defer(),s={username:i(t)?void 0:t,email:i(t)?t:void 0,password:e,profile:o};return r.ddp.method("createUser",[s],function(t,e){t?(r._emit("createUserError",t),n.reject(t)):(r.userId=e.id,r.loggedIn=!0,localStorage[r._host+"__login_token__"]=e.token,r._emit("createUser",e.id),r._emit("login",e.id),n.resolve(e.id))}),n.promise},s.prototype.loginWithPassword=function(t,e){var o=this,r=Q.defer(),n={password:e,user:{username:i(t)?void 0:t,email:i(t)?t:void 0}};return o.ddp.method("login",[n],function(t,e){t?(delete o.userId,delete o.loggedIn,delete localStorage[o._host+"__login_token__"],r.reject(t),o._emit("loginError",t)):(o.userId=e.id,o.loggedIn=!0,localStorage[o._host+"__login_token__"]=e.token,o._emit("login",e.id),r.resolve(e.id))}),r.promise},s.prototype.logout=function(){var t=this,e=Q.defer();return t.ddp.method("logout",[],function(o){o?(t._emit("logoutError",o),e.reject(o)):(delete t.userId,delete t.loggedIn,delete localStorage[t._host+"__login_token__"],t._emit("logout"),e.resolve())}),e.promise};var h=function(t){t&&(this._put=this.put,this._del=this.del,this.put=this.del=function(){throw new Error("Attempt to modify readonly set")}),this._items={}};h.prototype=Object.create(r.prototype),h.constructor=h,h.prototype.put=function(e,o){return n.beString(e),n.beObject(o),this._items[e]=t(o),this._emit("put",e),this},h.prototype.del=function(t){return n.beString(t),delete this._items[t],this._emit("del",t),this},h.prototype.get=function(e){return n.beString(e),t(this._items[e])},h.prototype.contains=function(t){return n.beString(t),!!this._items[t]},h.prototype.filter=function(e){var o=new h(!0),i=this._items,r=Object.keys(i);return r.forEach(function(r){var n=t(i[r]),s=e(r,n);s&&(o._items[r]=i[r])}),this.on("put",function(r){var n=t(i[r]),s=e(r,n);s&&o._put(r,i[r])}),this.on("del",function(t){o._del(t)}),o},h.prototype.toArray=function(){var e=[],o=this._items,i=Object.keys(this._items);return i.forEach(function(t){e.push(o[t])}),t(e)},h.prototype.toHash=function(){return t(this._items)},s.Set=h;var p=function(t,e,o,i){this._name=t,this._params=e,this._hash=o,this._asteroid=i,this._ready=Q.defer(),this.ready=this._ready.promise;var r=this._onReady.bind(this),n=this._onStop.bind(this),s=this._onError.bind(this);this.id=i.ddp.sub(t,e,r,n,s)};return p.constructor=p,p.prototype.stop=function(){this._asteroid.ddp.unsub(this.id),delete this._asteroid._subscriptionsCache[this._hash]},p.prototype._onReady=function(){this._ready.resolve(this.id)},p.prototype._onStop=function(){delete this._asteroid.subscriptions[this.id]},p.prototype._onError=function(t){this.ready.isPending()&&this._ready.reject(t),delete this._asteroid.subscriptions[this.id]},s.prototype.subscribe=function(t){n.beString(t);var e=JSON.stringify(arguments);if(!this._subscriptionsCache[e]){var o=Array.prototype.slice.call(arguments,1),i=new p(t,o,e,this);this._subscriptionsCache[e]=i,this.subscriptions[i.id]=i}return this._subscriptionsCache[e]},s.prototype._reEstablishSubscriptions=function(){var t=this.subscriptions;for(var e in t)t.hasOwnProperty(e)&&(t[e]=new p(t[e]._name,t[e]._params,this))},s}); \ No newline at end of file diff --git a/dist/node.asteroid.js b/dist/node.asteroid.js index 1debffa..a6079fa 100644 --- a/dist/node.asteroid.js +++ b/dist/node.asteroid.js @@ -787,9 +787,10 @@ Asteroid.Set = Set; // Subscription class // //////////////////////// -var Subscription = function (name, params, asteroid) { +var Subscription = function (name, params, hash, asteroid) { this._name = name; this._params = params; + this._hash = hash; this._asteroid = asteroid; // Subscription promises this._ready = Q.defer(); @@ -804,6 +805,7 @@ Subscription.constructor = Subscription; Subscription.prototype.stop = function () { this._asteroid.ddp.unsub(this.id); + delete this._asteroid._subscriptionsCache[this._hash]; }; Subscription.prototype._onReady = function () { @@ -836,7 +838,7 @@ Asteroid.prototype.subscribe = function (name /* , param1, param2, ... */) { if (!this._subscriptionsCache[hash]) { // Collect arguments into array var params = Array.prototype.slice.call(arguments, 1); - var sub = new Subscription(name, params, this); + var sub = new Subscription(name, params, hash, this); this._subscriptionsCache[hash] = sub; this.subscriptions[sub.id] = sub; } diff --git a/package.json b/package.json index c78089f..c3e917f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asteroid", - "version": "0.3.3", + "version": "0.3.4", "description": "Aletrnative Meteor client", "main": "dist/node.asteroid.js", "scripts": {