HubConnectionBuilder.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. // Licensed to the .NET Foundation under one or more agreements.
  3. // The .NET Foundation licenses this file to you under the MIT license.
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. exports.HubConnectionBuilder = void 0;
  6. const DefaultReconnectPolicy_1 = require("./DefaultReconnectPolicy");
  7. const HttpConnection_1 = require("./HttpConnection");
  8. const HubConnection_1 = require("./HubConnection");
  9. const ILogger_1 = require("./ILogger");
  10. const JsonHubProtocol_1 = require("./JsonHubProtocol");
  11. const Loggers_1 = require("./Loggers");
  12. const Utils_1 = require("./Utils");
  13. const LogLevelNameMapping = {
  14. trace: ILogger_1.LogLevel.Trace,
  15. debug: ILogger_1.LogLevel.Debug,
  16. info: ILogger_1.LogLevel.Information,
  17. information: ILogger_1.LogLevel.Information,
  18. warn: ILogger_1.LogLevel.Warning,
  19. warning: ILogger_1.LogLevel.Warning,
  20. error: ILogger_1.LogLevel.Error,
  21. critical: ILogger_1.LogLevel.Critical,
  22. none: ILogger_1.LogLevel.None,
  23. };
  24. function parseLogLevel(name) {
  25. // Case-insensitive matching via lower-casing
  26. // Yes, I know case-folding is a complicated problem in Unicode, but we only support
  27. // the ASCII strings defined in LogLevelNameMapping anyway, so it's fine -anurse.
  28. const mapping = LogLevelNameMapping[name.toLowerCase()];
  29. if (typeof mapping !== "undefined") {
  30. return mapping;
  31. }
  32. else {
  33. throw new Error(`Unknown log level: ${name}`);
  34. }
  35. }
  36. /** A builder for configuring {@link @microsoft/signalr.HubConnection} instances. */
  37. class HubConnectionBuilder {
  38. configureLogging(logging) {
  39. Utils_1.Arg.isRequired(logging, "logging");
  40. if (isLogger(logging)) {
  41. this.logger = logging;
  42. }
  43. else if (typeof logging === "string") {
  44. const logLevel = parseLogLevel(logging);
  45. this.logger = new Utils_1.ConsoleLogger(logLevel);
  46. }
  47. else {
  48. this.logger = new Utils_1.ConsoleLogger(logging);
  49. }
  50. return this;
  51. }
  52. withUrl(url, transportTypeOrOptions) {
  53. Utils_1.Arg.isRequired(url, "url");
  54. Utils_1.Arg.isNotEmpty(url, "url");
  55. this.url = url;
  56. // Flow-typing knows where it's at. Since HttpTransportType is a number and IHttpConnectionOptions is guaranteed
  57. // to be an object, we know (as does TypeScript) this comparison is all we need to figure out which overload was called.
  58. if (typeof transportTypeOrOptions === "object") {
  59. this.httpConnectionOptions = { ...this.httpConnectionOptions, ...transportTypeOrOptions };
  60. }
  61. else {
  62. this.httpConnectionOptions = {
  63. ...this.httpConnectionOptions,
  64. transport: transportTypeOrOptions,
  65. };
  66. }
  67. return this;
  68. }
  69. /** Configures the {@link @microsoft/signalr.HubConnection} to use the specified Hub Protocol.
  70. *
  71. * @param {IHubProtocol} protocol The {@link @microsoft/signalr.IHubProtocol} implementation to use.
  72. */
  73. withHubProtocol(protocol) {
  74. Utils_1.Arg.isRequired(protocol, "protocol");
  75. this.protocol = protocol;
  76. return this;
  77. }
  78. withAutomaticReconnect(retryDelaysOrReconnectPolicy) {
  79. if (this.reconnectPolicy) {
  80. throw new Error("A reconnectPolicy has already been set.");
  81. }
  82. if (!retryDelaysOrReconnectPolicy) {
  83. this.reconnectPolicy = new DefaultReconnectPolicy_1.DefaultReconnectPolicy();
  84. }
  85. else if (Array.isArray(retryDelaysOrReconnectPolicy)) {
  86. this.reconnectPolicy = new DefaultReconnectPolicy_1.DefaultReconnectPolicy(retryDelaysOrReconnectPolicy);
  87. }
  88. else {
  89. this.reconnectPolicy = retryDelaysOrReconnectPolicy;
  90. }
  91. return this;
  92. }
  93. /** Creates a {@link @microsoft/signalr.HubConnection} from the configuration options specified in this builder.
  94. *
  95. * @returns {HubConnection} The configured {@link @microsoft/signalr.HubConnection}.
  96. */
  97. build() {
  98. // If httpConnectionOptions has a logger, use it. Otherwise, override it with the one
  99. // provided to configureLogger
  100. const httpConnectionOptions = this.httpConnectionOptions || {};
  101. // If it's 'null', the user **explicitly** asked for null, don't mess with it.
  102. if (httpConnectionOptions.logger === undefined) {
  103. // If our logger is undefined or null, that's OK, the HttpConnection constructor will handle it.
  104. httpConnectionOptions.logger = this.logger;
  105. }
  106. // Now create the connection
  107. if (!this.url) {
  108. throw new Error("The 'HubConnectionBuilder.withUrl' method must be called before building the connection.");
  109. }
  110. const connection = new HttpConnection_1.HttpConnection(this.url, httpConnectionOptions);
  111. return HubConnection_1.HubConnection.create(connection, this.logger || Loggers_1.NullLogger.instance, this.protocol || new JsonHubProtocol_1.JsonHubProtocol(), this.reconnectPolicy);
  112. }
  113. }
  114. exports.HubConnectionBuilder = HubConnectionBuilder;
  115. function isLogger(logger) {
  116. return logger.log !== undefined;
  117. }
  118. //# sourceMappingURL=HubConnectionBuilder.js.map