Source: client/known-types.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. "use strict";
  27. /**
  28. * The map of all Protobuf types known to the application.
  29. *
  30. * <p>It is intended to be a static variable, but ES6 doesn't provide an easy way to do it.
  31. *
  32. * @type {Map<String, Class>}
  33. * @private
  34. */
  35. const types = new Map();
  36. /**
  37. * All Protobuf types known to the application.
  38. *
  39. * <p>This class serves as the registry of types generated by Protobuf complier.
  40. */
  41. export default class KnownTypes {
  42. constructor() {
  43. throw new Error('KnownTypes is not supposed to be instantiated.');
  44. }
  45. /**
  46. * Obtains the type URL for the Protobuf type.
  47. *
  48. * @param {!Class} messageClass the class of a Protobuf message or enum
  49. * @return {!string} the type URL
  50. * @public
  51. */
  52. static typeUrlFor(messageClass) {
  53. return messageClass.typeUrl();
  54. }
  55. /**
  56. * Obtains JS class for the given Protobuf type URL.
  57. *
  58. * @param {!string} typeUrl the type URL
  59. * @return {!Class} class of this Protobuf type
  60. * @public
  61. */
  62. static classFor(typeUrl) {
  63. const cls = types.get(typeUrl);
  64. if (cls === null || cls === undefined) {
  65. throw new Error(`Class for type URL '${typeUrl}' is not found.`);
  66. }
  67. return cls;
  68. }
  69. /**
  70. * Registers the type as a known type.
  71. *
  72. * @param {!Class} type the class of a Protobuf message or enum
  73. * @param {!string} typeUrl the URL of the type
  74. */
  75. static register(type, typeUrl) {
  76. if (!KnownTypes.hasType(typeUrl)) {
  77. types.set(typeUrl, type);
  78. }
  79. }
  80. /**
  81. * Tells whether the specified type URL is present among known types.
  82. *
  83. * @param {!string} typeUrl the type URL to check
  84. */
  85. static hasType(typeUrl) {
  86. return types.has(typeUrl);
  87. }
  88. /**
  89. * Removes all the types.
  90. *
  91. * The method is purposed for the testing.
  92. */
  93. static clear() {
  94. types.clear();
  95. }
  96. }