UNPKG

@sentry/core

Version:
127 lines (123 loc) 3.4 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const defaultScopes = require('../defaultScopes.js'); const scope = require('../scope.js'); const chainAndCopyPromiselike = require('../utils/chain-and-copy-promiselike.js'); const is = require('../utils/is.js'); const carrier = require('../carrier.js'); class AsyncContextStack { constructor(scope$1, isolationScope) { let assignedScope; if (!scope$1) { assignedScope = new scope.Scope(); } else { assignedScope = scope$1; } let assignedIsolationScope; if (!isolationScope) { assignedIsolationScope = new scope.Scope(); } else { assignedIsolationScope = isolationScope; } this._stack = [{ scope: assignedScope }]; this._isolationScope = assignedIsolationScope; } /** * Fork a scope for the stack. */ withScope(callback) { const scope = this._pushScope(); let maybePromiseResult; try { maybePromiseResult = callback(scope); } catch (e) { this._popScope(); throw e; } if (is.isThenable(maybePromiseResult)) { return chainAndCopyPromiselike.chainAndCopyPromiseLike( maybePromiseResult, () => this._popScope(), () => this._popScope() ); } this._popScope(); return maybePromiseResult; } /** * Get the client of the stack. */ getClient() { return this.getStackTop().client; } /** * Returns the scope of the top stack. */ getScope() { return this.getStackTop().scope; } /** * Get the isolation scope for the stack. */ getIsolationScope() { return this._isolationScope; } /** * Returns the topmost scope layer in the order domain > local > process. */ getStackTop() { return this._stack[this._stack.length - 1]; } /** * Push a scope to the stack. */ _pushScope() { const scope = this.getScope().clone(); this._stack.push({ client: this.getClient(), scope }); return scope; } /** * Pop a scope from the stack. */ _popScope() { if (this._stack.length <= 1) return false; return !!this._stack.pop(); } } function getAsyncContextStack() { const registry = carrier.getMainCarrier(); const sentry = carrier.getSentryCarrier(registry); return sentry.stack = sentry.stack || new AsyncContextStack(defaultScopes.getDefaultCurrentScope(), defaultScopes.getDefaultIsolationScope()); } function withScope(callback) { return getAsyncContextStack().withScope(callback); } function withSetScope(scope, callback) { const stack = getAsyncContextStack(); return stack.withScope(() => { stack.getStackTop().scope = scope; return callback(scope); }); } function withIsolationScope(callback) { return getAsyncContextStack().withScope(() => { return callback(getAsyncContextStack().getIsolationScope()); }); } function getStackAsyncContextStrategy() { return { withIsolationScope, withScope, withSetScope, withSetIsolationScope: (_isolationScope, callback) => { return withIsolationScope(callback); }, getCurrentScope: () => getAsyncContextStack().getScope(), getIsolationScope: () => getAsyncContextStack().getIsolationScope() }; } exports.AsyncContextStack = AsyncContextStack; exports.getStackAsyncContextStrategy = getStackAsyncContextStrategy; //# sourceMappingURL=stackStrategy.js.map