HubConnectionBuilder.js 4.7 KB

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