"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var domain_1 = require("./domain");
var global_1 = require("./global");
* API compatibility version of this shim.
*
* WARNING: This number should only be incresed when the global interface
* changes a and new methods are introduced.
*/
exports.API_VERSION = 1;
* Internal class used to make sure we always have the latest internal functions
* working in case we have a version conflict.
*/
var Shim = (function () {
function Shim(version) {
if (version === void 0) { version = exports.API_VERSION; }
this.version = version;
var stack = global_1.getGlobalStack();
if (stack.length === 0) {
stack.push({ scope: {}, type: 'process' });
}
}
* Checks if this shim's version is older than the given version.
*
* @param version A version number to compare to.
* @return True if the given version is newer; otherwise false.
*/
Shim.prototype.isOlderThan = function (version) {
return this.version < version;
};
* Creates a new 'local' ScopeLayer with the given client.
* @param client Optional client, defaults to the current client.
*/
Shim.prototype.pushScope = function (client) {
var usedClient = client || this.getCurrentClient();
this.getStack().push({
client: usedClient,
scope: this.getInitialScope(usedClient),
type: 'local',
});
};
Shim.prototype.popScope = function () {
return this.getStack().pop() !== undefined;
};
* Convenience method for pushScope and popScope.
*
* @param arg1 Either the client or callback.
* @param arg2 Either the client or callback.
*/
Shim.prototype.withScope = function (arg1, arg2) {
var callback = arg1;
var client = arg2;
if (!!(arg1 && arg1.constructor && arg1.call && arg1.apply)) {
callback = arg1;
client = arg2;
}
if (!!(arg2 && arg2.constructor && arg2.call && arg2.apply)) {
callback = arg2;
client = arg1;
}
this.pushScope(client);
try {
callback();
}
finally {
this.popScope();
}
};
Shim.prototype.clearScope = function () {
var top = this.getStackTop();
top.scope = this.getInitialScope(top.client);
};
Shim.prototype.getCurrentClient = function () {
return this.getStackTop().client;
};
Shim.prototype.getStack = function () {
return domain_1.getDomainStack() || global_1.getGlobalStack();
};
Shim.prototype.getStackTop = function () {
return this.getDomainStackTop() || this.getGlobalStackTop();
};
Shim.prototype.getGlobalStackTop = function () {
var stack = global_1.getGlobalStack();
return stack[stack.length - 1];
};
Shim.prototype.getDomainStackTop = function () {
var stack = domain_1.getDomainStack();
if (!stack) {
return undefined;
}
if (stack.length === 0) {
var client = this.getCurrentClient();
stack.push({
client: client,
scope: this.getInitialScope(client),
type: 'domain',
});
}
return stack[stack.length - 1];
};
* Obtains a new scope instance from the client.
*
* @param client An SDK client that implements `getInitialScope`.
* @returns The scope instance or an empty object on error.
*/
Shim.prototype.getInitialScope = function (client) {
try {
return client && client.getInitialScope();
}
catch (_a) {
return {};
}
};
return Shim;
}());
exports.Shim = Shim;