From 13ddec12897e59cc1f53a375ab7a583fbdec7e9e Mon Sep 17 00:00:00 2001 From: Paolo Scanferla Date: Thu, 8 May 2014 14:49:24 +0200 Subject: [PATCH] Changed getCollection method and updated version --- README.md | 2 +- bower.json | 2 +- dist/asteroid.js | 10 ++++++++-- dist/asteroid.min.js | 2 +- package.json | 2 +- src/asteroid.js | 10 ++++++++-- 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7f2a8ee..ed1c6f0 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ var ceres = new Asteroid("localhost:3000"); // Use real-time collections ceres.subscribe("tasks"); -var tasks = ceres.getCollection("tasks"); +var tasks = ceres.createCollection("tasks"); tasks.insert({ description: "Do the laundry" }); diff --git a/bower.json b/bower.json index 12e0a74..883a71a 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "asteroid", - "version": "0.2.1", + "version": "0.2.2", "homepage": "https://github.com/mondora/asteroid", "authors": [ "Paolo Scanferla " diff --git a/dist/asteroid.js b/dist/asteroid.js index 6f4f64b..929ab1d 100644 --- a/dist/asteroid.js +++ b/dist/asteroid.js @@ -291,7 +291,7 @@ Asteroid.prototype.call = function (method /* , param1, param2, ... */) { }; Asteroid.prototype.apply = function (method, params) { - // Assert name must be a string + // Assert method must be a string must.beString(method); // Create the result and updated promises var resultDeferred = Q.defer(); @@ -326,7 +326,13 @@ Asteroid.prototype.apply = function (method, params) { // Syntactic sugar // ///////////////////// -Asteroid.prototype.getCollection = function (name) { +Asteroid.prototype.createCollection = function (name) { + // Assert on arguments type + must.beString(name); + // Only create the collection if it doesn't exist + if (!this.collections[name]) { + this.collections[name] = new Asteroid._Collection(name, this); + } return this.collections[name]; }; diff --git a/dist/asteroid.min.js b/dist/asteroid.min.js index d005589..9379501 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}var i=function(){};i.prototype={constructor:i,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.beObject=function(t){var e=this._toString(t);if("Object"!==e)throw new Error("Assertion failed: expected Object, instead got "+e)};var r=function(t,e,o){n.beString(t),this._host=(e?"https://":"http://")+t,this._ddpOptions={endpoint:(e?"wss://":"ws://")+t+(window.SockJS?"/sockjs":"/websocket"),SocketConstructor:window.SockJS||window.WebSocket,debug:o},this.collections={},this.subscriptions={},this._init()};r.prototype=Object.create(i.prototype),r.prototype.constructor=r,r.prototype._init=function(){var t=this;t.ddp=new DDP(this._ddpOptions),t.ddp.on("connected",function(){t._tryResumeLogin(),t.ddp.sub("meteor.loginServiceConfiguration"),t._emit("connected")}),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)})},r.prototype._onAdded=function(t){var e=t.collection;this.collections[e]||(this.collections[e]=new r._Collection(e,this));var o=t.fields||{};o._id=t.id,this.collections[e]._remoteToLocalInsert(o)},r.prototype._onRemoved=function(t){this.collections[t.collection]&&this.collections[t.collection]._remoteToLocalRemove(t.id)},r.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))},r.prototype.subscribe=function(t){if(n.beString(t),this.subscriptions[t])return this.subscriptions[t];var e=Q.defer();this.subscriptions[t]=e.promise;var o=Array.prototype.slice.call(arguments,1);return this.ddp.sub(t,o,function(t,o){t?e.reject(t,o):e.resolve(o)}),this.subscriptions[t]},r.prototype.unsubscribe=function(t){this.ddp.unsub(t)},r.prototype.call=function(t){n.beString(t);var e=Array.prototype.slice.call(arguments,1);return this.apply(t,e)},r.prototype.apply=function(t,e){n.beString(t);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}},r.prototype.getCollection=function(t){return this.collections[t]};var s="__del__",c="__upd__",u=function(t){var e=s.length,o=c.length,i=t.slice(-1*e),n=t.slice(-1*o);return i===s||n===c},l=function(t,e){this.name=t,this.asteroid=e,this._set=new p};l.prototype.constructor=l,l.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)},l.prototype._remoteToLocalInsert=function(t){this._set.put(t._id,t)},l.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},l.prototype.insert=function(t){return t._id||(t._id=o()),{local:this._localToLocalInsert(t),remote:this._localToRemoteInsert(t)}},l.prototype._localToLocalRemove=function(t){var e=this._set.get(t);return e&&(this._set.put(t+s,e),this._set.del(t)),Q(t)},l.prototype._remoteToLocalRemove=function(t){this._set.del(t)},l.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 n=e._set.get(t+s);n&&(e._set.put(t,n),e._set.del(t+s)),o.reject(i)}else e._set.del(t+s),o.resolve(t)}),o.promise},l.prototype.remove=function(t){return{local:this._localToLocalRemove(t),remote:this._localToRemoteRemove(t)}},l.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+c,o);for(var i in e)o[i]=e[i];return this._set.put(t,o),Q(t)},l.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)},l.prototype._localToRemoteUpdate=function(t,e){var o=this,i=Q.defer(),n="/"+o.name+"/update",r={_id:t},s={$set:e};return o.asteroid.ddp.method(n,[r,s],function(e){if(e){var n=o._set.get(t+c);o._set.put(t,n),o._set.del(t+c),i.reject(e)}else o._set.del(t+c),i.resolve(t)}),i.promise},l.prototype.update=function(t,e){return{local:this._localToLocalUpdate(t,e),remote:this._localToRemoteUpdate(t,e)}};var a=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)})};a.prototype=Object.create(i.prototype),a.constructor=a,a.prototype._getResult=function(){this.result=this._set.toArray()};var d=function(t){return function(e,o){if(u(e))return!1;var i=function(t,e){return e.split(".").reduce(function(t,e){return t?t=t[e]:t},t)};for(var n in t){var r=i(o,n);if(r!==t[n])return!1}return!0}};l.prototype.reactiveQuery=function(t){var e;e="function"==typeof t?t:d(t);var o=this._set.filter(e);return new a(o)},r._Collection=l;var h=function(){this.itemsHash={},this.itemsArray=[]};h.prototype.constructor=h,h.prototype.set=function(e,o){if(o=t(o),this.itemsHash[e]){var i=this.itemsArray.indexOf(this.itemsHash[e]);this.itemsArray[i]=o}else this.itemsArray.push(o);this.itemsHash[e]=o},h.prototype.get=function(e){return t(this.itemsHash[e])},h.prototype.find=function(e){var o=function(t,e){return e.split(".").reduce(function(t,e){return t=t[e]},t)},i=Object.keys(e),n=[];return this.itemsArray.forEach(function(r){for(var s=0;se;e++)t+=Math.floor(65536*(1+Math.random())).toString(16).substring(1);return t}var i=function(){};i.prototype={constructor:i,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.beObject=function(t){var e=this._toString(t);if("Object"!==e)throw new Error("Assertion failed: expected Object, instead got "+e)};var r=function(t,e,o){n.beString(t),this._host=(e?"https://":"http://")+t,this._ddpOptions={endpoint:(e?"wss://":"ws://")+t+(window.SockJS?"/sockjs":"/websocket"),SocketConstructor:window.SockJS||window.WebSocket,debug:o},this.collections={},this.subscriptions={},this._init()};r.prototype=Object.create(i.prototype),r.prototype.constructor=r,r.prototype._init=function(){var t=this;t.ddp=new DDP(this._ddpOptions),t.ddp.on("connected",function(){t._tryResumeLogin(),t.ddp.sub("meteor.loginServiceConfiguration"),t._emit("connected")}),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)})},r.prototype._onAdded=function(t){var e=t.collection;this.collections[e]||(this.collections[e]=new r._Collection(e,this));var o=t.fields||{};o._id=t.id,this.collections[e]._remoteToLocalInsert(o)},r.prototype._onRemoved=function(t){this.collections[t.collection]&&this.collections[t.collection]._remoteToLocalRemove(t.id)},r.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))},r.prototype.subscribe=function(t){if(n.beString(t),this.subscriptions[t])return this.subscriptions[t];var e=Q.defer();this.subscriptions[t]=e.promise;var o=Array.prototype.slice.call(arguments,1);return this.ddp.sub(t,o,function(t,o){t?e.reject(t,o):e.resolve(o)}),this.subscriptions[t]},r.prototype.unsubscribe=function(t){this.ddp.unsub(t)},r.prototype.call=function(t){n.beString(t);var e=Array.prototype.slice.call(arguments,1);return this.apply(t,e)},r.prototype.apply=function(t,e){n.beString(t);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}},r.prototype.createCollection=function(t){return n.beString(t),this.collections[t]||(this.collections[t]=new r._Collection(t,this)),this.collections[t]};var s="__del__",c="__upd__",u=function(t){var e=s.length,o=c.length,i=t.slice(-1*e),n=t.slice(-1*o);return i===s||n===c},l=function(t,e){this.name=t,this.asteroid=e,this._set=new p};l.prototype.constructor=l,l.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)},l.prototype._remoteToLocalInsert=function(t){this._set.put(t._id,t)},l.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},l.prototype.insert=function(t){return t._id||(t._id=o()),{local:this._localToLocalInsert(t),remote:this._localToRemoteInsert(t)}},l.prototype._localToLocalRemove=function(t){var e=this._set.get(t);return e&&(this._set.put(t+s,e),this._set.del(t)),Q(t)},l.prototype._remoteToLocalRemove=function(t){this._set.del(t)},l.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 n=e._set.get(t+s);n&&(e._set.put(t,n),e._set.del(t+s)),o.reject(i)}else e._set.del(t+s),o.resolve(t)}),o.promise},l.prototype.remove=function(t){return{local:this._localToLocalRemove(t),remote:this._localToRemoteRemove(t)}},l.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+c,o);for(var i in e)o[i]=e[i];return this._set.put(t,o),Q(t)},l.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)},l.prototype._localToRemoteUpdate=function(t,e){var o=this,i=Q.defer(),n="/"+o.name+"/update",r={_id:t},s={$set:e};return o.asteroid.ddp.method(n,[r,s],function(e){if(e){var n=o._set.get(t+c);o._set.put(t,n),o._set.del(t+c),i.reject(e)}else o._set.del(t+c),i.resolve(t)}),i.promise},l.prototype.update=function(t,e){return{local:this._localToLocalUpdate(t,e),remote:this._localToRemoteUpdate(t,e)}};var a=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)})};a.prototype=Object.create(i.prototype),a.constructor=a,a.prototype._getResult=function(){this.result=this._set.toArray()};var d=function(t){return function(e,o){if(u(e))return!1;var i=function(t,e){return e.split(".").reduce(function(t,e){return t?t=t[e]:t},t)};for(var n in t){var r=i(o,n);if(r!==t[n])return!1}return!0}};l.prototype.reactiveQuery=function(t){var e;e="function"==typeof t?t:d(t);var o=this._set.filter(e);return new a(o)},r._Collection=l;var h=function(){this.itemsHash={},this.itemsArray=[]};h.prototype.constructor=h,h.prototype.set=function(e,o){if(o=t(o),this.itemsHash[e]){var i=this.itemsArray.indexOf(this.itemsHash[e]);this.itemsArray[i]=o}else this.itemsArray.push(o);this.itemsHash[e]=o},h.prototype.get=function(e){return t(this.itemsHash[e])},h.prototype.find=function(e){var o=function(t,e){return e.split(".").reduce(function(t,e){return t=t[e]},t)},i=Object.keys(e),n=[];return this.itemsArray.forEach(function(r){for(var s=0;s