LiveQuerySubscription.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. import EventEmitter from './EventEmitter';
  11. import CoreManager from './CoreManager';
  12. /**
  13. * Creates a new LiveQuery Subscription.
  14. * Extends events.EventEmitter
  15. * <a href="https://nodejs.org/api/events.html#events_class_eventemitter">cloud functions</a>.
  16. *
  17. *
  18. * <p>Open Event - When you call query.subscribe(), we send a subscribe request to
  19. * the LiveQuery server, when we get the confirmation from the LiveQuery server,
  20. * this event will be emitted. When the client loses WebSocket connection to the
  21. * LiveQuery server, we will try to auto reconnect the LiveQuery server. If we
  22. * reconnect the LiveQuery server and successfully resubscribe the ParseQuery,
  23. * you'll also get this event.
  24. *
  25. * <pre>
  26. * subscription.on('open', () => {
  27. *
  28. * });</pre></p>
  29. *
  30. * <p>Create Event - When a new ParseObject is created and it fulfills the ParseQuery you subscribe,
  31. * you'll get this event. The object is the ParseObject which is created.
  32. *
  33. * <pre>
  34. * subscription.on('create', (object) => {
  35. *
  36. * });</pre></p>
  37. *
  38. * <p>Update Event - When an existing ParseObject (original) which fulfills the ParseQuery you subscribe
  39. * is updated (The ParseObject fulfills the ParseQuery before and after changes),
  40. * you'll get this event. The object is the ParseObject which is updated.
  41. * Its content is the latest value of the ParseObject.
  42. *
  43. * Parse-Server 3.1.3+ Required for original object parameter
  44. *
  45. * <pre>
  46. * subscription.on('update', (object, original) => {
  47. *
  48. * });</pre></p>
  49. *
  50. * <p>Enter Event - When an existing ParseObject's (original) old value doesn't fulfill the ParseQuery
  51. * but its new value fulfills the ParseQuery, you'll get this event. The object is the
  52. * ParseObject which enters the ParseQuery. Its content is the latest value of the ParseObject.
  53. *
  54. * Parse-Server 3.1.3+ Required for original object parameter
  55. *
  56. * <pre>
  57. * subscription.on('enter', (object, original) => {
  58. *
  59. * });</pre></p>
  60. *
  61. *
  62. * <p>Update Event - When an existing ParseObject's old value fulfills the ParseQuery but its new value
  63. * doesn't fulfill the ParseQuery, you'll get this event. The object is the ParseObject
  64. * which leaves the ParseQuery. Its content is the latest value of the ParseObject.
  65. *
  66. * <pre>
  67. * subscription.on('leave', (object) => {
  68. *
  69. * });</pre></p>
  70. *
  71. *
  72. * <p>Delete Event - When an existing ParseObject which fulfills the ParseQuery is deleted, you'll
  73. * get this event. The object is the ParseObject which is deleted.
  74. *
  75. * <pre>
  76. * subscription.on('delete', (object) => {
  77. *
  78. * });</pre></p>
  79. *
  80. *
  81. * <p>Close Event - When the client loses the WebSocket connection to the LiveQuery
  82. * server and we stop receiving events, you'll get this event.
  83. *
  84. * <pre>
  85. * subscription.on('close', () => {
  86. *
  87. * });</pre></p>
  88. *
  89. * @alias Parse.LiveQuerySubscription
  90. */
  91. class Subscription extends EventEmitter {
  92. /*
  93. * @param {string} id - subscription id
  94. * @param {string} query - query to subscribe to
  95. * @param {string} sessionToken - optional session token
  96. */
  97. constructor(id, query, sessionToken) {
  98. super();
  99. this.id = id;
  100. this.query = query;
  101. this.sessionToken = sessionToken; // adding listener so process does not crash
  102. // best practice is for developer to register their own listener
  103. this.on('error', () => {});
  104. }
  105. /**
  106. * Close the subscription
  107. */
  108. unsubscribe()
  109. /*: Promise*/
  110. {
  111. return CoreManager.getLiveQueryController().getDefaultLiveQueryClient().then(liveQueryClient => {
  112. liveQueryClient.unsubscribe(this);
  113. this.emit('close');
  114. });
  115. }
  116. }
  117. export default Subscription;