virtual-websocket-client.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. import set from 'lodash.set';
  2. import unset from 'lodash.unset';
  3. import cloneDeep from 'lodash.clonedeep';
  4. import { genRequestId } from './message';
  5. import { CloudSDKError, isTimeoutError, CancelledError, isCancelledError } from '../utils/error';
  6. import { ERR_CODE } from '../config/error.config';
  7. import { sleep } from '../utils/utils';
  8. import { RealtimeListener } from './listener';
  9. import { Snapshot } from './snapshot';
  10. import { isRealtimeErrorMessageError } from './error';
  11. var WATCH_STATUS;
  12. (function (WATCH_STATUS) {
  13. WATCH_STATUS["LOGGINGIN"] = "LOGGINGIN";
  14. WATCH_STATUS["INITING"] = "INITING";
  15. WATCH_STATUS["REBUILDING"] = "REBUILDING";
  16. WATCH_STATUS["ACTIVE"] = "ACTIVE";
  17. WATCH_STATUS["ERRORED"] = "ERRORED";
  18. WATCH_STATUS["CLOSING"] = "CLOSING";
  19. WATCH_STATUS["CLOSED"] = "CLOSED";
  20. WATCH_STATUS["PAUSED"] = "PAUSED";
  21. WATCH_STATUS["RESUMING"] = "RESUMING";
  22. })(WATCH_STATUS || (WATCH_STATUS = {}));
  23. const DEFAULT_WAIT_TIME_ON_UNKNOWN_ERROR = 100;
  24. const DEFAULT_MAX_AUTO_RETRY_ON_ERROR = 2;
  25. const DEFAULT_MAX_SEND_ACK_AUTO_RETRY_ON_ERROR = 2;
  26. const DEFAULT_SEND_ACK_DEBOUNCE_TIMEOUT = 10 * 1000;
  27. const DEFAULT_INIT_WATCH_TIMEOUT = 10 * 1000;
  28. const DEFAULT_REBUILD_WATCH_TIMEOUT = 10 * 1000;
  29. export class VirtualWebSocketClient {
  30. constructor(options) {
  31. this.watchStatus = WATCH_STATUS.INITING;
  32. this._login = async (envId, refresh) => {
  33. this.watchStatus = WATCH_STATUS.LOGGINGIN;
  34. const loginResult = await this.login(envId, refresh);
  35. if (!this.envId) {
  36. this.envId = loginResult.envId;
  37. }
  38. return loginResult;
  39. };
  40. this.initWatch = async (forceRefreshLogin) => {
  41. if (this._initWatchPromise) {
  42. return this._initWatchPromise;
  43. }
  44. this._initWatchPromise = new Promise(async (resolve, reject) => {
  45. try {
  46. if (this.watchStatus === WATCH_STATUS.PAUSED) {
  47. console.log('[realtime] initWatch cancelled on pause');
  48. return resolve();
  49. }
  50. const { envId } = await this._login(this.envId, forceRefreshLogin);
  51. if (this.watchStatus === WATCH_STATUS.PAUSED) {
  52. console.log('[realtime] initWatch cancelled on pause');
  53. return resolve();
  54. }
  55. this.watchStatus = WATCH_STATUS.INITING;
  56. const initWatchMsg = {
  57. watchId: this.watchId,
  58. requestId: genRequestId(),
  59. msgType: 'INIT_WATCH',
  60. msgData: {
  61. envId,
  62. collName: this.collectionName,
  63. query: this.query,
  64. limit: this.limit,
  65. orderBy: this.orderBy
  66. }
  67. };
  68. const initEventMsg = await this.send({
  69. msg: initWatchMsg,
  70. waitResponse: true,
  71. skipOnMessage: true,
  72. timeout: DEFAULT_INIT_WATCH_TIMEOUT
  73. });
  74. const { events, currEvent } = initEventMsg.msgData;
  75. this.sessionInfo = {
  76. queryID: initEventMsg.msgData.queryID,
  77. currentEventId: currEvent - 1,
  78. currentDocs: []
  79. };
  80. if (events.length > 0) {
  81. for (const e of events) {
  82. e.ID = currEvent;
  83. }
  84. this.handleServerEvents(initEventMsg);
  85. }
  86. else {
  87. this.sessionInfo.currentEventId = currEvent;
  88. const snapshot = new Snapshot({
  89. id: currEvent,
  90. docChanges: [],
  91. docs: [],
  92. type: 'init'
  93. });
  94. this.listener.onChange(snapshot);
  95. this.scheduleSendACK();
  96. }
  97. this.onWatchStart(this, this.sessionInfo.queryID);
  98. this.watchStatus = WATCH_STATUS.ACTIVE;
  99. this._availableRetries.INIT_WATCH = DEFAULT_MAX_AUTO_RETRY_ON_ERROR;
  100. resolve();
  101. }
  102. catch (e) {
  103. this.handleWatchEstablishmentError(e, {
  104. operationName: 'INIT_WATCH',
  105. resolve,
  106. reject
  107. });
  108. }
  109. });
  110. let success = false;
  111. try {
  112. await this._initWatchPromise;
  113. success = true;
  114. }
  115. finally {
  116. this._initWatchPromise = undefined;
  117. }
  118. console.log(`[realtime] initWatch ${success ? 'success' : 'fail'}`);
  119. };
  120. this.rebuildWatch = async (forceRefreshLogin) => {
  121. if (this._rebuildWatchPromise) {
  122. return this._rebuildWatchPromise;
  123. }
  124. this._rebuildWatchPromise = new Promise(async (resolve, reject) => {
  125. try {
  126. if (this.watchStatus === WATCH_STATUS.PAUSED) {
  127. console.log('[realtime] rebuildWatch cancelled on pause');
  128. return resolve();
  129. }
  130. const { envId } = await this._login(this.envId, forceRefreshLogin);
  131. if (!this.sessionInfo) {
  132. throw new Error('can not rebuildWatch without a successful initWatch (lack of sessionInfo)');
  133. }
  134. if (this.watchStatus === WATCH_STATUS.PAUSED) {
  135. console.log('[realtime] rebuildWatch cancelled on pause');
  136. return resolve();
  137. }
  138. this.watchStatus = WATCH_STATUS.REBUILDING;
  139. const rebuildWatchMsg = {
  140. watchId: this.watchId,
  141. requestId: genRequestId(),
  142. msgType: 'REBUILD_WATCH',
  143. msgData: {
  144. envId,
  145. collName: this.collectionName,
  146. queryID: this.sessionInfo.queryID,
  147. eventID: this.sessionInfo.currentEventId
  148. }
  149. };
  150. const nextEventMsg = await this.send({
  151. msg: rebuildWatchMsg,
  152. waitResponse: true,
  153. skipOnMessage: false,
  154. timeout: DEFAULT_REBUILD_WATCH_TIMEOUT
  155. });
  156. this.handleServerEvents(nextEventMsg);
  157. this.watchStatus = WATCH_STATUS.ACTIVE;
  158. this._availableRetries.REBUILD_WATCH = DEFAULT_MAX_AUTO_RETRY_ON_ERROR;
  159. resolve();
  160. }
  161. catch (e) {
  162. this.handleWatchEstablishmentError(e, {
  163. operationName: 'REBUILD_WATCH',
  164. resolve,
  165. reject
  166. });
  167. }
  168. });
  169. let success = false;
  170. try {
  171. await this._rebuildWatchPromise;
  172. success = true;
  173. }
  174. finally {
  175. this._rebuildWatchPromise = undefined;
  176. }
  177. console.log(`[realtime] rebuildWatch ${success ? 'success' : 'fail'}`);
  178. };
  179. this.handleWatchEstablishmentError = async (e, options) => {
  180. const isInitWatch = options.operationName === 'INIT_WATCH';
  181. const abortWatch = () => {
  182. this.closeWithError(new CloudSDKError({
  183. errCode: isInitWatch
  184. ? ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_INIT_WATCH_FAIL
  185. : ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_REBUILD_WATCH_FAIL,
  186. errMsg: e
  187. }));
  188. options.reject(e);
  189. };
  190. const retry = (refreshLogin) => {
  191. if (this.useRetryTicket(options.operationName)) {
  192. if (isInitWatch) {
  193. this._initWatchPromise = undefined;
  194. options.resolve(this.initWatch(refreshLogin));
  195. }
  196. else {
  197. this._rebuildWatchPromise = undefined;
  198. options.resolve(this.rebuildWatch(refreshLogin));
  199. }
  200. }
  201. else {
  202. abortWatch();
  203. }
  204. };
  205. this.handleCommonError(e, {
  206. onSignError: () => retry(true),
  207. onTimeoutError: () => retry(false),
  208. onNotRetryableError: abortWatch,
  209. onCancelledError: options.reject,
  210. onUnknownError: async () => {
  211. try {
  212. const onWSDisconnected = async () => {
  213. this.pause();
  214. await this.onceWSConnected();
  215. retry(true);
  216. };
  217. if (!this.isWSConnected()) {
  218. await onWSDisconnected();
  219. }
  220. else {
  221. await sleep(DEFAULT_WAIT_TIME_ON_UNKNOWN_ERROR);
  222. if (this.watchStatus === WATCH_STATUS.PAUSED) {
  223. options.reject(new CancelledError(`${options.operationName} cancelled due to pause after unknownError`));
  224. }
  225. else if (!this.isWSConnected()) {
  226. await onWSDisconnected();
  227. }
  228. else {
  229. retry(false);
  230. }
  231. }
  232. }
  233. catch (e) {
  234. retry(true);
  235. }
  236. }
  237. });
  238. };
  239. this.closeWatch = async () => {
  240. const queryId = this.sessionInfo ? this.sessionInfo.queryID : '';
  241. if (this.watchStatus !== WATCH_STATUS.ACTIVE) {
  242. this.watchStatus = WATCH_STATUS.CLOSED;
  243. this.onWatchClose(this, queryId);
  244. return;
  245. }
  246. try {
  247. this.watchStatus = WATCH_STATUS.CLOSING;
  248. const closeWatchMsg = {
  249. watchId: this.watchId,
  250. requestId: genRequestId(),
  251. msgType: 'CLOSE_WATCH',
  252. msgData: null
  253. };
  254. await this.send({
  255. msg: closeWatchMsg
  256. });
  257. this.sessionInfo = undefined;
  258. this.watchStatus = WATCH_STATUS.CLOSED;
  259. }
  260. catch (e) {
  261. this.closeWithError(new CloudSDKError({
  262. errCode: ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_CLOSE_WATCH_FAIL,
  263. errMsg: e
  264. }));
  265. }
  266. finally {
  267. this.onWatchClose(this, queryId);
  268. }
  269. };
  270. this.scheduleSendACK = () => {
  271. this.clearACKSchedule();
  272. this._ackTimeoutId = setTimeout(() => {
  273. if (this._waitExpectedTimeoutId) {
  274. this.scheduleSendACK();
  275. }
  276. else {
  277. this.sendACK();
  278. }
  279. }, DEFAULT_SEND_ACK_DEBOUNCE_TIMEOUT);
  280. };
  281. this.clearACKSchedule = () => {
  282. if (this._ackTimeoutId) {
  283. clearTimeout(this._ackTimeoutId);
  284. }
  285. };
  286. this.sendACK = async () => {
  287. try {
  288. if (this.watchStatus !== WATCH_STATUS.ACTIVE) {
  289. this.scheduleSendACK();
  290. return;
  291. }
  292. if (!this.sessionInfo) {
  293. console.warn('[realtime listener] can not send ack without a successful initWatch (lack of sessionInfo)');
  294. return;
  295. }
  296. const ackMsg = {
  297. watchId: this.watchId,
  298. requestId: genRequestId(),
  299. msgType: 'CHECK_LAST',
  300. msgData: {
  301. queryID: this.sessionInfo.queryID,
  302. eventID: this.sessionInfo.currentEventId
  303. }
  304. };
  305. await this.send({
  306. msg: ackMsg
  307. });
  308. this.scheduleSendACK();
  309. }
  310. catch (e) {
  311. if (isRealtimeErrorMessageError(e)) {
  312. const msg = e.payload;
  313. switch (msg.msgData.code) {
  314. case 'CHECK_LOGIN_FAILED':
  315. case 'SIGN_EXPIRED_ERROR':
  316. case 'SIGN_INVALID_ERROR':
  317. case 'SIGN_PARAM_INVALID': {
  318. this.rebuildWatch();
  319. return;
  320. }
  321. case 'QUERYID_INVALID_ERROR':
  322. case 'SYS_ERR':
  323. case 'INVALIID_ENV':
  324. case 'COLLECTION_PERMISSION_DENIED': {
  325. this.closeWithError(new CloudSDKError({
  326. errCode: ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_CHECK_LAST_FAIL,
  327. errMsg: msg.msgData.code
  328. }));
  329. return;
  330. }
  331. default: {
  332. break;
  333. }
  334. }
  335. }
  336. if (this._availableRetries.CHECK_LAST &&
  337. this._availableRetries.CHECK_LAST > 0) {
  338. this._availableRetries.CHECK_LAST--;
  339. this.scheduleSendACK();
  340. }
  341. else {
  342. this.closeWithError(new CloudSDKError({
  343. errCode: ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_CHECK_LAST_FAIL,
  344. errMsg: e
  345. }));
  346. }
  347. }
  348. };
  349. this.handleCommonError = (e, options) => {
  350. if (isRealtimeErrorMessageError(e)) {
  351. const msg = e.payload;
  352. switch (msg.msgData.code) {
  353. case 'CHECK_LOGIN_FAILED':
  354. case 'SIGN_EXPIRED_ERROR':
  355. case 'SIGN_INVALID_ERROR':
  356. case 'SIGN_PARAM_INVALID': {
  357. options.onSignError(e);
  358. return;
  359. }
  360. case 'QUERYID_INVALID_ERROR':
  361. case 'SYS_ERR':
  362. case 'INVALIID_ENV':
  363. case 'COLLECTION_PERMISSION_DENIED': {
  364. options.onNotRetryableError(e);
  365. return;
  366. }
  367. default: {
  368. options.onNotRetryableError(e);
  369. return;
  370. }
  371. }
  372. }
  373. else if (isTimeoutError(e)) {
  374. options.onTimeoutError(e);
  375. return;
  376. }
  377. else if (isCancelledError(e)) {
  378. options.onCancelledError(e);
  379. return;
  380. }
  381. options.onUnknownError(e);
  382. };
  383. this.watchId = `watchid_${+new Date()}_${Math.random()}`;
  384. this.envId = options.envId;
  385. this.collectionName = options.collectionName;
  386. this.query = options.query;
  387. this.limit = options.limit;
  388. this.orderBy = options.orderBy;
  389. this.send = options.send;
  390. this.login = options.login;
  391. this.isWSConnected = options.isWSConnected;
  392. this.onceWSConnected = options.onceWSConnected;
  393. this.getWaitExpectedTimeoutLength = options.getWaitExpectedTimeoutLength;
  394. this.onWatchStart = options.onWatchStart;
  395. this.onWatchClose = options.onWatchClose;
  396. this.debug = options.debug;
  397. this._availableRetries = {
  398. INIT_WATCH: DEFAULT_MAX_AUTO_RETRY_ON_ERROR,
  399. REBUILD_WATCH: DEFAULT_MAX_AUTO_RETRY_ON_ERROR,
  400. CHECK_LAST: DEFAULT_MAX_SEND_ACK_AUTO_RETRY_ON_ERROR
  401. };
  402. this.listener = new RealtimeListener({
  403. close: this.closeWatch,
  404. onChange: options.onChange,
  405. onError: options.onError,
  406. debug: this.debug,
  407. virtualClient: this
  408. });
  409. this.initWatch();
  410. }
  411. useRetryTicket(operationName) {
  412. if (this._availableRetries[operationName] &&
  413. this._availableRetries[operationName] > 0) {
  414. this._availableRetries[operationName]--;
  415. console.log(`[realtime] ${operationName} use a retry ticket, now only ${this._availableRetries[operationName]} retry left`);
  416. return true;
  417. }
  418. return false;
  419. }
  420. async handleServerEvents(msg) {
  421. try {
  422. this.scheduleSendACK();
  423. await this._handleServerEvents(msg);
  424. this._postHandleServerEventsValidityCheck(msg);
  425. }
  426. catch (e) {
  427. console.error('[realtime listener] internal non-fatal error: handle server events failed with error: ', e);
  428. throw e;
  429. }
  430. }
  431. async _handleServerEvents(msg) {
  432. const { requestId } = msg;
  433. const { events } = msg.msgData;
  434. const { msgType } = msg;
  435. if (!events.length || !this.sessionInfo) {
  436. return;
  437. }
  438. const sessionInfo = this.sessionInfo;
  439. let allChangeEvents;
  440. try {
  441. allChangeEvents = events.map(getPublicEvent);
  442. }
  443. catch (e) {
  444. this.closeWithError(new CloudSDKError({
  445. errCode: ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_RECEIVE_INVALID_SERVER_DATA,
  446. errMsg: e
  447. }));
  448. return;
  449. }
  450. let docs = [...sessionInfo.currentDocs];
  451. let initEncountered = false;
  452. for (let i = 0, len = allChangeEvents.length; i < len; i++) {
  453. const change = allChangeEvents[i];
  454. if (sessionInfo.currentEventId >= change.id) {
  455. if (!allChangeEvents[i - 1] || change.id > allChangeEvents[i - 1].id) {
  456. console.warn(`[realtime] duplicate event received, cur ${sessionInfo.currentEventId} but got ${change.id}`);
  457. }
  458. else {
  459. console.error(`[realtime listener] server non-fatal error: events out of order (the latter event's id is smaller than that of the former) (requestId ${requestId})`);
  460. }
  461. continue;
  462. }
  463. else if (sessionInfo.currentEventId === change.id - 1) {
  464. switch (change.dataType) {
  465. case 'update': {
  466. if (!change.doc) {
  467. switch (change.queueType) {
  468. case 'update':
  469. case 'dequeue': {
  470. const localDoc = docs.find(doc => doc._id === change.docId);
  471. if (localDoc) {
  472. const doc = cloneDeep(localDoc);
  473. if (change.updatedFields) {
  474. for (const fieldPath in change.updatedFields) {
  475. set(doc, fieldPath, change.updatedFields[fieldPath]);
  476. }
  477. }
  478. if (change.removedFields) {
  479. for (const fieldPath of change.removedFields) {
  480. unset(doc, fieldPath);
  481. }
  482. }
  483. change.doc = doc;
  484. }
  485. else {
  486. console.error('[realtime listener] internal non-fatal server error: unexpected update dataType event where no doc is associated.');
  487. }
  488. break;
  489. }
  490. case 'enqueue': {
  491. const err = new CloudSDKError({
  492. errCode: ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_UNEXPECTED_FATAL_ERROR,
  493. errMsg: `HandleServerEvents: full doc is not provided with dataType="update" and queueType="enqueue" (requestId ${msg.requestId})`
  494. });
  495. this.closeWithError(err);
  496. throw err;
  497. }
  498. default: {
  499. break;
  500. }
  501. }
  502. }
  503. break;
  504. }
  505. case 'replace': {
  506. if (!change.doc) {
  507. const err = new CloudSDKError({
  508. errCode: ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_UNEXPECTED_FATAL_ERROR,
  509. errMsg: `HandleServerEvents: full doc is not provided with dataType="replace" (requestId ${msg.requestId})`
  510. });
  511. this.closeWithError(err);
  512. throw err;
  513. }
  514. break;
  515. }
  516. case 'remove': {
  517. const doc = docs.find(doc => doc._id === change.docId);
  518. if (doc) {
  519. change.doc = doc;
  520. }
  521. else {
  522. console.error('[realtime listener] internal non-fatal server error: unexpected remove event where no doc is associated.');
  523. }
  524. break;
  525. }
  526. case 'limit': {
  527. if (!change.doc) {
  528. switch (change.queueType) {
  529. case 'dequeue': {
  530. const doc = docs.find(doc => doc._id === change.docId);
  531. if (doc) {
  532. change.doc = doc;
  533. }
  534. else {
  535. console.error('[realtime listener] internal non-fatal server error: unexpected limit dataType event where no doc is associated.');
  536. }
  537. break;
  538. }
  539. case 'enqueue': {
  540. const err = new CloudSDKError({
  541. errCode: ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_UNEXPECTED_FATAL_ERROR,
  542. errMsg: `HandleServerEvents: full doc is not provided with dataType="limit" and queueType="enqueue" (requestId ${msg.requestId})`
  543. });
  544. this.closeWithError(err);
  545. throw err;
  546. }
  547. default: {
  548. break;
  549. }
  550. }
  551. }
  552. break;
  553. }
  554. }
  555. switch (change.queueType) {
  556. case 'init': {
  557. if (!initEncountered) {
  558. initEncountered = true;
  559. docs = [change.doc];
  560. }
  561. else {
  562. docs.push(change.doc);
  563. }
  564. break;
  565. }
  566. case 'enqueue': {
  567. docs.push(change.doc);
  568. break;
  569. }
  570. case 'dequeue': {
  571. const ind = docs.findIndex(doc => doc._id === change.docId);
  572. if (ind > -1) {
  573. docs.splice(ind, 1);
  574. }
  575. else {
  576. console.error('[realtime listener] internal non-fatal server error: unexpected dequeue event where no doc is associated.');
  577. }
  578. break;
  579. }
  580. case 'update': {
  581. const ind = docs.findIndex(doc => doc._id === change.docId);
  582. if (ind > -1) {
  583. docs[ind] = change.doc;
  584. }
  585. else {
  586. console.error('[realtime listener] internal non-fatal server error: unexpected queueType update event where no doc is associated.');
  587. }
  588. break;
  589. }
  590. }
  591. if (i === len - 1 ||
  592. (allChangeEvents[i + 1] && allChangeEvents[i + 1].id !== change.id)) {
  593. const docsSnapshot = [...docs];
  594. const docChanges = allChangeEvents
  595. .slice(0, i + 1)
  596. .filter(c => c.id === change.id);
  597. this.sessionInfo.currentEventId = change.id;
  598. this.sessionInfo.currentDocs = docs;
  599. const snapshot = new Snapshot({
  600. id: change.id,
  601. docChanges,
  602. docs: docsSnapshot,
  603. msgType
  604. });
  605. this.listener.onChange(snapshot);
  606. }
  607. }
  608. else {
  609. console.warn(`[realtime listener] event received is out of order, cur ${this.sessionInfo.currentEventId} but got ${change.id}`);
  610. await this.rebuildWatch();
  611. return;
  612. }
  613. }
  614. }
  615. _postHandleServerEventsValidityCheck(msg) {
  616. if (!this.sessionInfo) {
  617. console.error('[realtime listener] internal non-fatal error: sessionInfo lost after server event handling, this should never occur');
  618. return;
  619. }
  620. if (this.sessionInfo.expectEventId &&
  621. this.sessionInfo.currentEventId >= this.sessionInfo.expectEventId) {
  622. this.clearWaitExpectedEvent();
  623. }
  624. if (this.sessionInfo.currentEventId < msg.msgData.currEvent) {
  625. console.warn('[realtime listener] internal non-fatal error: client eventId does not match with server event id after server event handling');
  626. return;
  627. }
  628. }
  629. clearWaitExpectedEvent() {
  630. if (this._waitExpectedTimeoutId) {
  631. clearTimeout(this._waitExpectedTimeoutId);
  632. this._waitExpectedTimeoutId = undefined;
  633. }
  634. }
  635. onMessage(msg) {
  636. switch (this.watchStatus) {
  637. case WATCH_STATUS.PAUSED: {
  638. if (msg.msgType !== 'ERROR') {
  639. return;
  640. }
  641. break;
  642. }
  643. case WATCH_STATUS.LOGGINGIN:
  644. case WATCH_STATUS.INITING:
  645. case WATCH_STATUS.REBUILDING: {
  646. console.warn(`[realtime listener] internal non-fatal error: unexpected message received while ${this.watchStatus}`);
  647. return;
  648. }
  649. case WATCH_STATUS.CLOSED: {
  650. console.warn('[realtime listener] internal non-fatal error: unexpected message received when the watch has closed');
  651. return;
  652. }
  653. case WATCH_STATUS.ERRORED: {
  654. console.warn('[realtime listener] internal non-fatal error: unexpected message received when the watch has ended with error');
  655. return;
  656. }
  657. }
  658. if (!this.sessionInfo) {
  659. console.warn('[realtime listener] internal non-fatal error: sessionInfo not found while message is received.');
  660. return;
  661. }
  662. this.scheduleSendACK();
  663. switch (msg.msgType) {
  664. case 'NEXT_EVENT': {
  665. console.warn(`nextevent ${msg.msgData.currEvent} ignored`, msg);
  666. this.handleServerEvents(msg);
  667. break;
  668. }
  669. case 'CHECK_EVENT': {
  670. if (this.sessionInfo.currentEventId < msg.msgData.currEvent) {
  671. this.sessionInfo.expectEventId = msg.msgData.currEvent;
  672. this.clearWaitExpectedEvent();
  673. this._waitExpectedTimeoutId = setTimeout(() => {
  674. this.rebuildWatch();
  675. }, this.getWaitExpectedTimeoutLength());
  676. console.log(`[realtime] waitExpectedTimeoutLength ${this.getWaitExpectedTimeoutLength()}`);
  677. }
  678. break;
  679. }
  680. case 'ERROR': {
  681. this.closeWithError(new CloudSDKError({
  682. errCode: ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_SERVER_ERROR_MSG,
  683. errMsg: `${msg.msgData.code} - ${msg.msgData.message}`
  684. }));
  685. break;
  686. }
  687. default: {
  688. console.warn(`[realtime listener] virtual client receive unexpected msg ${msg.msgType}: `, msg);
  689. break;
  690. }
  691. }
  692. }
  693. closeWithError(error) {
  694. this.watchStatus = WATCH_STATUS.ERRORED;
  695. this.clearACKSchedule();
  696. this.listener.onError(error);
  697. this.onWatchClose(this, (this.sessionInfo && this.sessionInfo.queryID) || '');
  698. console.log(`[realtime] client closed (${this.collectionName} ${this.query}) (watchId ${this.watchId})`);
  699. }
  700. pause() {
  701. this.watchStatus = WATCH_STATUS.PAUSED;
  702. console.log(`[realtime] client paused (${this.collectionName} ${this.query}) (watchId ${this.watchId})`);
  703. }
  704. async resume() {
  705. this.watchStatus = WATCH_STATUS.RESUMING;
  706. console.log(`[realtime] client resuming with ${this.sessionInfo ? 'REBUILD_WATCH' : 'INIT_WATCH'} (${this.collectionName} ${this.query}) (${this.watchId})`);
  707. try {
  708. await (this.sessionInfo ? this.rebuildWatch() : this.initWatch());
  709. console.log(`[realtime] client successfully resumed (${this.collectionName} ${this.query}) (${this.watchId})`);
  710. }
  711. catch (e) {
  712. console.error(`[realtime] client resume failed (${this.collectionName} ${this.query}) (${this.watchId})`, e);
  713. }
  714. }
  715. }
  716. function getPublicEvent(event) {
  717. const e = {
  718. id: event.ID,
  719. dataType: event.DataType,
  720. queueType: event.QueueType,
  721. docId: event.DocID,
  722. doc: event.Doc && event.Doc !== '{}' ? JSON.parse(event.Doc) : undefined
  723. };
  724. if (event.DataType === 'update') {
  725. if (event.UpdatedFields) {
  726. e.updatedFields = JSON.parse(event.UpdatedFields);
  727. }
  728. if (event.removedFields || event.RemovedFields) {
  729. e.removedFields = JSON.parse(event.removedFields);
  730. }
  731. }
  732. return e;
  733. }