123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- import { DefaultReconnectPolicy } from "./DefaultReconnectPolicy";
- import { HttpConnection } from "./HttpConnection";
- import { HubConnection } from "./HubConnection";
- import { LogLevel } from "./ILogger";
- import { JsonHubProtocol } from "./JsonHubProtocol";
- import { NullLogger } from "./Loggers";
- import { Arg, ConsoleLogger } from "./Utils";
- const LogLevelNameMapping = {
- trace: LogLevel.Trace,
- debug: LogLevel.Debug,
- info: LogLevel.Information,
- information: LogLevel.Information,
- warn: LogLevel.Warning,
- warning: LogLevel.Warning,
- error: LogLevel.Error,
- critical: LogLevel.Critical,
- none: LogLevel.None,
- };
- function parseLogLevel(name) {
- // Case-insensitive matching via lower-casing
- // Yes, I know case-folding is a complicated problem in Unicode, but we only support
- // the ASCII strings defined in LogLevelNameMapping anyway, so it's fine -anurse.
- const mapping = LogLevelNameMapping[name.toLowerCase()];
- if (typeof mapping !== "undefined") {
- return mapping;
- }
- else {
- throw new Error(`Unknown log level: ${name}`);
- }
- }
- /** A builder for configuring {@link @microsoft/signalr.HubConnection} instances. */
- export class HubConnectionBuilder {
- configureLogging(logging) {
- Arg.isRequired(logging, "logging");
- if (isLogger(logging)) {
- this.logger = logging;
- }
- else if (typeof logging === "string") {
- const logLevel = parseLogLevel(logging);
- this.logger = new ConsoleLogger(logLevel);
- }
- else {
- this.logger = new ConsoleLogger(logging);
- }
- return this;
- }
- withUrl(url, transportTypeOrOptions) {
- Arg.isRequired(url, "url");
- Arg.isNotEmpty(url, "url");
- this.url = url;
- // Flow-typing knows where it's at. Since HttpTransportType is a number and IHttpConnectionOptions is guaranteed
- // to be an object, we know (as does TypeScript) this comparison is all we need to figure out which overload was called.
- if (typeof transportTypeOrOptions === "object") {
- this.httpConnectionOptions = { ...this.httpConnectionOptions, ...transportTypeOrOptions };
- }
- else {
- this.httpConnectionOptions = {
- ...this.httpConnectionOptions,
- transport: transportTypeOrOptions,
- };
- }
- return this;
- }
- /** Configures the {@link @microsoft/signalr.HubConnection} to use the specified Hub Protocol.
- *
- * @param {IHubProtocol} protocol The {@link @microsoft/signalr.IHubProtocol} implementation to use.
- */
- withHubProtocol(protocol) {
- Arg.isRequired(protocol, "protocol");
- this.protocol = protocol;
- return this;
- }
- withAutomaticReconnect(retryDelaysOrReconnectPolicy) {
- if (this.reconnectPolicy) {
- throw new Error("A reconnectPolicy has already been set.");
- }
- if (!retryDelaysOrReconnectPolicy) {
- this.reconnectPolicy = new DefaultReconnectPolicy();
- }
- else if (Array.isArray(retryDelaysOrReconnectPolicy)) {
- this.reconnectPolicy = new DefaultReconnectPolicy(retryDelaysOrReconnectPolicy);
- }
- else {
- this.reconnectPolicy = retryDelaysOrReconnectPolicy;
- }
- return this;
- }
- /** Creates a {@link @microsoft/signalr.HubConnection} from the configuration options specified in this builder.
- *
- * @returns {HubConnection} The configured {@link @microsoft/signalr.HubConnection}.
- */
- build() {
- // If httpConnectionOptions has a logger, use it. Otherwise, override it with the one
- // provided to configureLogger
- const httpConnectionOptions = this.httpConnectionOptions || {};
- // If it's 'null', the user **explicitly** asked for null, don't mess with it.
- if (httpConnectionOptions.logger === undefined) {
- // If our logger is undefined or null, that's OK, the HttpConnection constructor will handle it.
- httpConnectionOptions.logger = this.logger;
- }
- // Now create the connection
- if (!this.url) {
- throw new Error("The 'HubConnectionBuilder.withUrl' method must be called before building the connection.");
- }
- const connection = new HttpConnection(this.url, httpConnectionOptions);
- return HubConnection.create(connection, this.logger || NullLogger.instance, this.protocol || new JsonHubProtocol(), this.reconnectPolicy);
- }
- }
- function isLogger(logger) {
- return logger.log !== undefined;
- }
- //# sourceMappingURL=HubConnectionBuilder.js.map
|