"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.withObservation = exports.currentObserver = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const observerStack = [];
function setObserver(newObserver) {
    observerStack.push(newObserver);
    exports.currentObserver = newObserver;
}
function clearObserver() {
    (0, internal_1.debugAssert)(() => observerStack.length > 0 || "Empty Observer stack on clear");
    const popped = observerStack.pop();
    (0, internal_1.debugAssert)(() => popped === exports.currentObserver || "Mismatched observer stack");
    exports.currentObserver = observerStack[observerStack.length - 1];
}
/**
 * For the duration of `f`, pushes `newObserver` onto the observer stack, making it the {@link currentObserver}.
 */
function withObservation(newObserver, f) {
    setObserver(newObserver);
    try {
        return f();
    }
    finally {
        clearObserver();
    }
}
exports.withObservation = withObservation;
//# sourceMappingURL=observer.js.map