HubConnectionBuilder.js 3.5 KB

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