HubConnectionBuilder.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. import { HttpConnection } from "./HttpConnection";
  4. import { HubConnection } from "./HubConnection";
  5. import { JsonHubProtocol } from "./JsonHubProtocol";
  6. import { NullLogger } from "./Loggers";
  7. import { Arg, ConsoleLogger } from "./Utils";
  8. /** A builder for configuring {@link @aspnet/signalr.HubConnection} instances. */
  9. var HubConnectionBuilder = /** @class */ (function () {
  10. function HubConnectionBuilder() {
  11. }
  12. HubConnectionBuilder.prototype.configureLogging = function (logging) {
  13. Arg.isRequired(logging, "logging");
  14. if (isLogger(logging)) {
  15. this.logger = logging;
  16. }
  17. else {
  18. this.logger = new ConsoleLogger(logging);
  19. }
  20. return this;
  21. };
  22. HubConnectionBuilder.prototype.withUrl = function (url, transportTypeOrOptions) {
  23. Arg.isRequired(url, "url");
  24. this.url = url;
  25. // Flow-typing knows where it's at. Since HttpTransportType is a number and IHttpConnectionOptions is guaranteed
  26. // to be an object, we know (as does TypeScript) this comparison is all we need to figure out which overload was called.
  27. if (typeof transportTypeOrOptions === "object") {
  28. this.httpConnectionOptions = transportTypeOrOptions;
  29. }
  30. else {
  31. this.httpConnectionOptions = {
  32. transport: transportTypeOrOptions,
  33. };
  34. }
  35. return this;
  36. };
  37. /** Configures the {@link @aspnet/signalr.HubConnection} to use the specified Hub Protocol.
  38. *
  39. * @param {IHubProtocol} protocol The {@link @aspnet/signalr.IHubProtocol} implementation to use.
  40. */
  41. HubConnectionBuilder.prototype.withHubProtocol = function (protocol) {
  42. Arg.isRequired(protocol, "protocol");
  43. this.protocol = protocol;
  44. return this;
  45. };
  46. /** Creates a {@link @aspnet/signalr.HubConnection} from the configuration options specified in this builder.
  47. *
  48. * @returns {HubConnection} The configured {@link @aspnet/signalr.HubConnection}.
  49. */
  50. HubConnectionBuilder.prototype.build = function () {
  51. // If httpConnectionOptions has a logger, use it. Otherwise, override it with the one
  52. // provided to configureLogger
  53. var httpConnectionOptions = this.httpConnectionOptions || {};
  54. // If it's 'null', the user **explicitly** asked for null, don't mess with it.
  55. if (httpConnectionOptions.logger === undefined) {
  56. // If our logger is undefined or null, that's OK, the HttpConnection constructor will handle it.
  57. httpConnectionOptions.logger = this.logger;
  58. }
  59. // Now create the connection
  60. if (!this.url) {
  61. throw new Error("The 'HubConnectionBuilder.withUrl' method must be called before building the connection.");
  62. }
  63. var connection = new HttpConnection(this.url, httpConnectionOptions);
  64. return HubConnection.create(connection, this.logger || NullLogger.instance, this.protocol || new JsonHubProtocol());
  65. };
  66. return HubConnectionBuilder;
  67. }());
  68. export { HubConnectionBuilder };
  69. function isLogger(logger) {
  70. return logger.log !== undefined;
  71. }