ddp.js 3.0 KB

1
  1. app.factory("DDP",["$q",function(e){function t(e){this.VERSIONS=["pre1"],this.wsUri=e,this.sock,this.defs={},this.subs={},this.watchers={},this.collections={}}return t.prototype._Ids=function(){var e=0;return{next:function(){return++e+""}}}(),t.prototype.connect=function(){var t=this,s=new e.defer;return t.sock=new WebSocket(t.wsUri),t.sock.onopen=function(){t.send({msg:"connect",version:t.VERSIONS[0],support:t.VERSIONS})},t.sock.onerror=function(e){s.reject(e)},t.sock.onmessage=function(e){var o=JSON.parse(e.data);switch(o.msg){case"connected":s.resolve(o);break;case"result":t._resolveCall(o);break;case"updated":break;case"changed":t._changeDoc(o);break;case"added":t._addDoc(o);break;case"removed":t._removeDoc(o);break;case"ready":t._resolveSubs(o);break;case"nosub":t._resolveNoSub(o);break;case"addedBefore":t._addDoc(o);break;case"movedBefore":}},s.promise},t.prototype._resolveNoSub=function(e){if(e.error){var t=e.error;this.defs[e.id].reject(t.reason||"Subscription not found")}else this.defs[e.id].resolve()},t.prototype._resolveCall=function(e){e.error?this.defs[e.id].reject(e.error.reason):"undefined"!=typeof e.result&&this.defs[e.id].resolve(e.result)},t.prototype._resolveSubs=function(e){for(var t=e.subs,s=0;s<t.length;s++)this.defs[t[s]].resolve()},t.prototype._changeDoc=function(e){var t=e.collection,s=e.id,o=e.fields,r=e.cleared,i=this.collections[t];if(o)for(var n in o)i[s][n]=o[n];else if(r)for(var c=0;c<r.length;c++){var a=r[c];delete i[s][a]}var d=i[s];this._notifyWatchers(t,d,s,e.msg)},t.prototype._addDoc=function(e){var t=e.collection,s=e.id;this.collections[t]||(this.collections[t]={}),this.collections[t][s]=e.fields;var o=this.collections[t][s];this._notifyWatchers(t,o,s,e.msg)},t.prototype._removeDoc=function(e){var t=e.collection,s=e.id,o=this.collections[t][s],r=JSON.parse(JSON.stringify(o));delete this.collections[t][s],this._notifyWatchers(t,r,s,e.msg)},t.prototype._notifyWatchers=function(e,t,s,o){if(t=JSON.parse(JSON.stringify(t)),t._id=s,this.watchers[e])for(var r=0;r<this.watchers[e].length;r++)this.watchers[e][r](t,o);else this.watchers[e]=[]},t.prototype._deferredSend=function(t,s,o){var r=this._Ids.next();this.defs[r]=new e.defer;var i=o||[],n={msg:t,params:i,id:r};return"method"===t?n.method=s:"sub"===t&&(n.name=s,this.subs[s]=r),this.send(n),this.defs[r].promise},t.prototype.call=function(e,t){return this._deferredSend("method",e,t)},t.prototype.subscribe=function(e,t){return this._deferredSend("sub",e,t)},t.prototype.unsubscribe=function(e){if(this.defs[t]=new $.Deferred,this.subs[e]){var t=this.subs[e],s={msg:"unsub",id:t};this.send(s)}else this.defs[t].reject(e+" was never subscribed");return this.defs[t].promise},t.prototype.watch=function(e,t){this.watchers[e]||(this.watchers[e]=[]),this.watchers[e].push(t)},t.prototype.getCollection=function(e){return this.collections[e]||null},t.prototype.getDocument=function(e,t){return this.collections[e][t]||null},t.prototype.send=function(e){this.sock.send(JSON.stringify(e))},t.prototype.close=function(){this.sock.close()},t}]);