live-query.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('leancloud-realtime/core')) :
  3. typeof define === 'function' && define.amd ? define('live-query', ['exports', 'leancloud-realtime/core'], factory) :
  4. (global = global || self, factory(global.AV = global.AV || {}, global.AV));
  5. }(this, function (exports, core) { 'use strict';
  6. function _inheritsLoose(subClass, superClass) {
  7. subClass.prototype = Object.create(superClass.prototype);
  8. subClass.prototype.constructor = subClass;
  9. subClass.__proto__ = superClass;
  10. }
  11. function _toConsumableArray(arr) {
  12. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
  13. }
  14. function _arrayWithoutHoles(arr) {
  15. if (Array.isArray(arr)) {
  16. for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
  17. return arr2;
  18. }
  19. }
  20. function _iterableToArray(iter) {
  21. if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
  22. }
  23. function _nonIterableSpread() {
  24. throw new TypeError("Invalid attempt to spread non-iterable instance");
  25. }
  26. /* eslint-disable import/no-unresolved */
  27. if (!core.Protocals) {
  28. throw new Error('LeanCloud Realtime SDK not installed');
  29. }
  30. var CommandType = core.Protocals.CommandType,
  31. GenericCommand = core.Protocals.GenericCommand,
  32. AckCommand = core.Protocals.AckCommand;
  33. var warn = function warn(error) {
  34. return console.warn(error.message);
  35. };
  36. var LiveQueryClient =
  37. /*#__PURE__*/
  38. function (_EventEmitter) {
  39. _inheritsLoose(LiveQueryClient, _EventEmitter);
  40. function LiveQueryClient(appId, subscriptionId, connection) {
  41. var _this;
  42. _this = _EventEmitter.call(this) || this;
  43. _this._appId = appId;
  44. _this.id = subscriptionId;
  45. _this._connection = connection;
  46. _this._eventemitter = new core.EventEmitter();
  47. _this._querys = new Set();
  48. return _this;
  49. }
  50. var _proto = LiveQueryClient.prototype;
  51. _proto._send = function _send(cmd) {
  52. var _this$_connection;
  53. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  54. args[_key - 1] = arguments[_key];
  55. }
  56. return (_this$_connection = this._connection).send.apply(_this$_connection, [Object.assign(cmd, {
  57. appId: this._appId,
  58. installationId: this.id,
  59. service: 1
  60. })].concat(args));
  61. };
  62. _proto._open = function _open() {
  63. return this._send(new GenericCommand({
  64. cmd: CommandType.login
  65. }));
  66. };
  67. _proto.close = function close() {
  68. var _ee = this._eventemitter;
  69. _ee.emit('beforeclose');
  70. return this._send(new GenericCommand({
  71. cmd: CommandType.logout
  72. })).then(function () {
  73. return _ee.emit('close');
  74. });
  75. };
  76. _proto.register = function register(liveQuery) {
  77. this._querys.add(liveQuery);
  78. };
  79. _proto.deregister = function deregister(liveQuery) {
  80. var _this2 = this;
  81. this._querys.delete(liveQuery);
  82. setTimeout(function () {
  83. if (!_this2._querys.size) _this2.close().catch(warn);
  84. }, 0);
  85. };
  86. _proto._dispatchCommand = function _dispatchCommand(command) {
  87. if (command.cmd !== CommandType.data) {
  88. this.emit('unhandledmessage', command);
  89. return core.Promise.resolve();
  90. }
  91. return this._dispatchDataCommand(command);
  92. };
  93. _proto._dispatchDataCommand = function _dispatchDataCommand(_ref) {
  94. var _ref$dataMessage = _ref.dataMessage,
  95. ids = _ref$dataMessage.ids,
  96. msg = _ref$dataMessage.msg;
  97. this.emit('message', msg.map(function (_ref2) {
  98. var data = _ref2.data;
  99. return JSON.parse(data);
  100. })); // send ack
  101. var command = new GenericCommand({
  102. cmd: CommandType.ack,
  103. ackMessage: new AckCommand({
  104. ids: ids
  105. })
  106. });
  107. return this._send(command, false).catch(warn);
  108. };
  109. return LiveQueryClient;
  110. }(core.EventEmitter);
  111. var finalize = function finalize(callback) {
  112. return [// eslint-disable-next-line no-sequences
  113. function (value) {
  114. return callback(), value;
  115. }, function (error) {
  116. callback();
  117. throw error;
  118. }];
  119. };
  120. var onRealtimeCreate = function onRealtimeCreate(realtime) {
  121. /* eslint-disable no-param-reassign */
  122. realtime._liveQueryClients = {};
  123. realtime.createLiveQueryClient = function (subscriptionId) {
  124. var _realtime$_open$then;
  125. if (realtime._liveQueryClients[subscriptionId] !== undefined) {
  126. return core.Promise.resolve(realtime._liveQueryClients[subscriptionId]);
  127. }
  128. var promise = (_realtime$_open$then = realtime._open().then(function (connection) {
  129. var client = new LiveQueryClient(realtime._options.appId, subscriptionId, connection);
  130. connection.on('reconnect', function () {
  131. return client._open().then(function () {
  132. return client.emit('reconnect');
  133. }, function (error) {
  134. return client.emit('reconnecterror', error);
  135. });
  136. });
  137. client._eventemitter.on('beforeclose', function () {
  138. delete realtime._liveQueryClients[client.id];
  139. }, realtime);
  140. client._eventemitter.on('close', function () {
  141. realtime._deregister(client);
  142. }, realtime);
  143. return client._open().then(function () {
  144. realtime._liveQueryClients[client.id] = client;
  145. realtime._register(client);
  146. return client;
  147. });
  148. })).then.apply(_realtime$_open$then, _toConsumableArray(finalize(function () {
  149. if (realtime._deregisterPending) realtime._deregisterPending(promise);
  150. })));
  151. realtime._liveQueryClients[subscriptionId] = promise;
  152. if (realtime._registerPending) realtime._registerPending(promise);
  153. return promise;
  154. };
  155. /* eslint-enable no-param-reassign */
  156. };
  157. var beforeCommandDispatch = function beforeCommandDispatch(command, realtime) {
  158. var isLiveQueryCommand = command.installationId && command.service === 1;
  159. if (!isLiveQueryCommand) return true;
  160. var targetClient = realtime._liveQueryClients[command.installationId];
  161. if (targetClient) {
  162. targetClient._dispatchCommand(command).catch(function (error) {
  163. return console.warn(error);
  164. });
  165. } else {
  166. console.warn('Unexpected message received without any live client match: %O', command);
  167. }
  168. return false;
  169. }; // eslint-disable-next-line import/prefer-default-export
  170. var LiveQueryPlugin = {
  171. name: 'leancloud-realtime-plugin-live-query',
  172. onRealtimeCreate: onRealtimeCreate,
  173. beforeCommandDispatch: beforeCommandDispatch
  174. };
  175. exports.LiveQueryPlugin = LiveQueryPlugin;
  176. Object.defineProperty(exports, '__esModule', { value: true });
  177. }));
  178. //# sourceMappingURL=live-query.js.map