Source: client/subscribing-client.js

  1. /*
  2. * Copyright 2023, TeamDev. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Redistribution and use in source and/or binary forms, with or without
  11. * modification, must retain the above copyright notice and the following
  12. * disclaimer.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. import {EventSubscriptionRequest, SubscriptionRequest} from "./subscribing-request";
  27. /**
  28. * A client which manages entity state and event subscriptions.
  29. *
  30. * @abstract
  31. */
  32. export class SubscribingClient {
  33. /**
  34. * @param {!ActorRequestFactory} actorRequestFactory
  35. * a request factory to build requests to Spine server
  36. */
  37. constructor(actorRequestFactory) {
  38. this._requestFactory = actorRequestFactory;
  39. }
  40. /**
  41. * Creates a new subscription request.
  42. *
  43. * @param {!Class<Message>} type the target entity type
  44. * @param {!Client} client the client that initiated the request
  45. * @return {SubscriptionRequest} a new subscription request
  46. */
  47. subscribeTo(type, client) {
  48. return new SubscriptionRequest(type, client, this._requestFactory);
  49. }
  50. /**
  51. * Subscribes to a given topic which targets an entity type.
  52. *
  53. * @param {!spine.client.Topic} topic a topic to subscribe to
  54. * @return {Promise<EntitySubscriptionObject<Message>>} a subscription object
  55. *
  56. * @template <T> a Protobuf type of entities being the target of a subscription
  57. */
  58. subscribe(topic) {
  59. throw new Error('Not implemented in abstract base.');
  60. }
  61. /**
  62. * Creates a new event subscription request.
  63. *
  64. * @param {!Class<Message>} type the target event type
  65. * @param {!Client} client the client that initiated the request
  66. * @return {EventSubscriptionRequest} a new event subscription request
  67. */
  68. subscribeToEvent(type, client) {
  69. return new EventSubscriptionRequest(type, client, this._requestFactory);
  70. }
  71. /**
  72. * Subscribes to the given topic which targets an event type.
  73. *
  74. * @param {!spine.client.Topic} topic a topic to subscribe to
  75. * @return {Promise<EventSubscriptionObject>} a subscription object
  76. */
  77. subscribeToEvents(topic) {
  78. throw new Error('Not implemented in abstract base.');
  79. }
  80. /**
  81. * Cancels all subscriptions, which were created through this instance of subscribing client.
  82. */
  83. cancelAllSubscriptions() {
  84. throw new Error('Not implemented in abstract base.');
  85. }
  86. /**
  87. * Returns a new topic factory instance which can be further used for the `Topic` creation.
  88. *
  89. * @return {TopicFactory}
  90. */
  91. newTopic() {
  92. return this._requestFactory.topic();
  93. }
  94. }
  95. const SUBSCRIPTIONS_NOT_SUPPORTED = 'Subscriptions are not supported.';
  96. /**
  97. * A {@link SubscribingClient} which does not create subscriptions.
  98. */
  99. export class NoOpSubscribingClient extends SubscribingClient {
  100. constructor(actorRequestFactory) {
  101. super(actorRequestFactory)
  102. }
  103. /**
  104. * Always throws an error.
  105. *
  106. * @override
  107. */
  108. subscribe(topic) {
  109. throw new Error(SUBSCRIPTIONS_NOT_SUPPORTED);
  110. }
  111. /**
  112. * Always throws an error.
  113. *
  114. * @override
  115. */
  116. subscribeToEvents(topic) {
  117. throw new Error(SUBSCRIPTIONS_NOT_SUPPORTED);
  118. }
  119. /**
  120. * Always throws an error.
  121. *
  122. * @override
  123. */
  124. cancelAllSubscriptions() {
  125. throw new Error(SUBSCRIPTIONS_NOT_SUPPORTED);
  126. }
  127. }