@microsoft/signalr
Version:
ASP.NET Core SignalR Client
126 lines • 5.93 kB
JavaScript
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var DefaultReconnectPolicy_1 = require("./DefaultReconnectPolicy");
var HttpConnection_1 = require("./HttpConnection");
var HubConnection_1 = require("./HubConnection");
var ILogger_1 = require("./ILogger");
var JsonHubProtocol_1 = require("./JsonHubProtocol");
var Loggers_1 = require("./Loggers");
var Utils_1 = require("./Utils");
// tslint:disable:object-literal-sort-keys
var LogLevelNameMapping = {
trace: ILogger_1.LogLevel.Trace,
debug: ILogger_1.LogLevel.Debug,
info: ILogger_1.LogLevel.Information,
information: ILogger_1.LogLevel.Information,
warn: ILogger_1.LogLevel.Warning,
warning: ILogger_1.LogLevel.Warning,
error: ILogger_1.LogLevel.Error,
critical: ILogger_1.LogLevel.Critical,
none: ILogger_1.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.
var 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. */
var HubConnectionBuilder = /** @class */ (function () {
function HubConnectionBuilder() {
}
HubConnectionBuilder.prototype.configureLogging = function (logging) {
Utils_1.Arg.isRequired(logging, "logging");
if (isLogger(logging)) {
this.logger = logging;
}
else if (typeof logging === "string") {
var logLevel = parseLogLevel(logging);
this.logger = new Utils_1.ConsoleLogger(logLevel);
}
else {
this.logger = new Utils_1.ConsoleLogger(logging);
}
return this;
};
HubConnectionBuilder.prototype.withUrl = function (url, transportTypeOrOptions) {
Utils_1.Arg.isRequired(url, "url");
Utils_1.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 = __assign({}, this.httpConnectionOptions, transportTypeOrOptions);
}
else {
this.httpConnectionOptions = __assign({}, 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.
*/
HubConnectionBuilder.prototype.withHubProtocol = function (protocol) {
Utils_1.Arg.isRequired(protocol, "protocol");
this.protocol = protocol;
return this;
};
HubConnectionBuilder.prototype.withAutomaticReconnect = function (retryDelaysOrReconnectPolicy) {
if (this.reconnectPolicy) {
throw new Error("A reconnectPolicy has already been set.");
}
if (!retryDelaysOrReconnectPolicy) {
this.reconnectPolicy = new DefaultReconnectPolicy_1.DefaultReconnectPolicy();
}
else if (Array.isArray(retryDelaysOrReconnectPolicy)) {
this.reconnectPolicy = new DefaultReconnectPolicy_1.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}.
*/
HubConnectionBuilder.prototype.build = function () {
// If httpConnectionOptions has a logger, use it. Otherwise, override it with the one
// provided to configureLogger
var 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.");
}
var connection = new HttpConnection_1.HttpConnection(this.url, httpConnectionOptions);
return HubConnection_1.HubConnection.create(connection, this.logger || Loggers_1.NullLogger.instance, this.protocol || new JsonHubProtocol_1.JsonHubProtocol(), this.reconnectPolicy);
};
return HubConnectionBuilder;
}());
exports.HubConnectionBuilder = HubConnectionBuilder;
function isLogger(logger) {
return logger.log !== undefined;
}
//# sourceMappingURL=HubConnectionBuilder.js.map
;