Source: client/commanding-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 {Status} from "../proto/spine/core/response_pb";
  27. import {CommandRequest} from "./command-request";
  28. import {CommandHandlingError, CommandValidationError, SpineError} from "./errors";
  29. import ObjectToProto from "./object-to-proto";
  30. import {TypedMessage} from "./typed-message";
  31. const _statusType = Status.typeUrl();
  32. /**
  33. * A client which posts commands.
  34. *
  35. * This class has a default implementation but is intended to be overridden as necessary if it's
  36. * required to change the behavior.
  37. */
  38. export class CommandingClient {
  39. constructor(endpoint, requestFactory) {
  40. this._requestFactory = requestFactory;
  41. this._endpoint = endpoint;
  42. }
  43. /**
  44. * Creates a new command request.
  45. *
  46. * @param {!Message} commandMessage the command to send to the server
  47. * @param {!Client} client the client which initiated the request
  48. * @return {CommandRequest} a new command request
  49. */
  50. command(commandMessage, client) {
  51. return new CommandRequest(commandMessage, client, this._requestFactory);
  52. }
  53. /**
  54. * Posts a given command to the Spine server.
  55. *
  56. * @param {!spine.core.Command} command a Command sent to Spine server
  57. * @param {!AckCallback} onAck a command acknowledgement callback
  58. */
  59. post(command, onAck) {
  60. const cmd = TypedMessage.of(command);
  61. this._endpoint.command(cmd)
  62. .then(ack => this._onAck(ack, onAck))
  63. .catch(error => {
  64. onAck.onError(new CommandHandlingError(error.message, error));
  65. });
  66. }
  67. _onAck(ack, onAck) {
  68. const responseStatus = ack.status;
  69. const responseStatusProto = ObjectToProto.convert(responseStatus, _statusType);
  70. const responseStatusCase = responseStatusProto.getStatusCase();
  71. switch (responseStatusCase) {
  72. case Status.StatusCase.OK:
  73. onAck.onOk();
  74. break;
  75. case Status.StatusCase.ERROR:
  76. const error = responseStatusProto.getError();
  77. const message = error.getMessage();
  78. onAck.onError(error.hasValidationError()
  79. ? new CommandValidationError(message, error)
  80. : new CommandHandlingError(message, error));
  81. break;
  82. case Status.StatusCase.REJECTION:
  83. onAck.onImmediateRejection(responseStatusProto.getRejection());
  84. break;
  85. default:
  86. onAck.onError(
  87. new SpineError(`Unknown response status case ${responseStatusCase}`)
  88. );
  89. }
  90. }
  91. }