live-query.min.js 3.3 KB

12
  1. !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("leancloud-realtime/core")):"function"==typeof define&&define.amd?define("live-query",["exports","leancloud-realtime/core"],t):t((e=e||self).AV=e.AV||{},e.AV)}(this,function(e,o){"use strict";function a(e){return function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}if(!o.Protocals)throw new Error("LeanCloud Realtime SDK not installed");var c=o.Protocals.CommandType,s=o.Protocals.GenericCommand,u=o.Protocals.AckCommand,l=function(e){return console.warn(e.message)},d=function(i){var e,t;function n(e,t,n){var r;return(r=i.call(this)||this)._appId=e,r.id=t,r._connection=n,r._eventemitter=new o.EventEmitter,r._querys=new Set,r}t=i,(e=n).prototype=Object.create(t.prototype),(e.prototype.constructor=e).__proto__=t;var r=n.prototype;return r._send=function(e){for(var t,n=arguments.length,r=new Array(1<n?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];return(t=this._connection).send.apply(t,[Object.assign(e,{appId:this._appId,installationId:this.id,service:1})].concat(r))},r._open=function(){return this._send(new s({cmd:c.login}))},r.close=function(){var e=this._eventemitter;return e.emit("beforeclose"),this._send(new s({cmd:c.logout})).then(function(){return e.emit("close")})},r.register=function(e){this._querys.add(e)},r.deregister=function(e){var t=this;this._querys.delete(e),setTimeout(function(){t._querys.size||t.close().catch(l)},0)},r._dispatchCommand=function(e){return e.cmd!==c.data?(this.emit("unhandledmessage",e),o.Promise.resolve()):this._dispatchDataCommand(e)},r._dispatchDataCommand=function(e){var t=e.dataMessage,n=t.ids,r=t.msg;this.emit("message",r.map(function(e){var t=e.data;return JSON.parse(t)}));var i=new s({cmd:c.ack,ackMessage:new u({ids:n})});return this._send(i,!1).catch(l)},n}(o.EventEmitter),t={name:"leancloud-realtime-plugin-live-query",onRealtimeCreate:function(i){i._liveQueryClients={},i.createLiveQueryClient=function(n){var e;if(void 0!==i._liveQueryClients[n])return o.Promise.resolve(i._liveQueryClients[n]);var t,r=(e=i._open().then(function(e){var t=new d(i._options.appId,n,e);return e.on("reconnect",function(){return t._open().then(function(){return t.emit("reconnect")},function(e){return t.emit("reconnecterror",e)})}),t._eventemitter.on("beforeclose",function(){delete i._liveQueryClients[t.id]},i),t._eventemitter.on("close",function(){i._deregister(t)},i),t._open().then(function(){return i._liveQueryClients[t.id]=t,i._register(t),t})})).then.apply(e,a((t=function(){i._deregisterPending&&i._deregisterPending(r)},[function(e){return t(),e},function(e){throw t(),e}])));return i._liveQueryClients[n]=r,i._registerPending&&i._registerPending(r),r}},beforeCommandDispatch:function(e,t){if(!e.installationId||1!==e.service)return!0;var n=t._liveQueryClients[e.installationId];return n?n._dispatchCommand(e).catch(function(e){return console.warn(e)}):console.warn("Unexpected message received without any live client match: %O",e),!1}};e.LiveQueryPlugin=t,Object.defineProperty(e,"__esModule",{value:!0})});
  2. //# sourceMappingURL=live-query.min.js.map