Source: client/querying-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 {QueryRequest} from "./query-request";
  27. /**
  28. * A client which performs entity state queries.
  29. *
  30. * @abstract
  31. */
  32. export class QueryingClient {
  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 query request.
  42. *
  43. * @param {!Class<Message>} entityType the target entity type
  44. * @param {!Client} client the client which initiated the request
  45. * @return {QueryRequest} a new query request
  46. */
  47. select(entityType, client) {
  48. return new QueryRequest(entityType, client, this._requestFactory);
  49. }
  50. /**
  51. * Executes the given `Query` instance specifying the data to be retrieved from
  52. * Spine server fulfilling a returned promise with an array of received objects.
  53. *
  54. * @param {!spine.client.Query} query a query instance to be executed
  55. * @return {Promise<Message[]>} a promise to be fulfilled with a list of Protobuf
  56. * messages of a given type or with an empty list if no entities matching given query
  57. * were found; rejected with a `SpineError` if error occurs
  58. *
  59. * @template <T> a Protobuf type of entities being the target of a query
  60. */
  61. read(query) {
  62. throw new Error('Not implemented in abstract base.');
  63. }
  64. /**
  65. * Creates a new query factory instance which can be further used for the `Query` creation.
  66. *
  67. * @return {QueryFactory}
  68. */
  69. newQuery() {
  70. return this._requestFactory.query();
  71. }
  72. }