"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runAllDisposers = exports.addDisposer = exports.getDisposersArray = void 0;
const objectsMap = new WeakMap();
function getDisposersArray(obj, topic) {
    let mapForGivenObject = objectsMap.get(obj);
    let disposersArrayForGivenTopic = mapForGivenObject ? mapForGivenObject.get(topic) : undefined;
    if (!mapForGivenObject) {
        mapForGivenObject = new Map();
        objectsMap.set(obj, mapForGivenObject);
    }
    if (!disposersArrayForGivenTopic) {
        disposersArrayForGivenTopic = [];
        mapForGivenObject.set(topic, disposersArrayForGivenTopic);
    }
    return disposersArrayForGivenTopic;
}
exports.getDisposersArray = getDisposersArray;
function addDisposer(obj, topic, disposer) {
    getDisposersArray(obj, topic).push(disposer);
}
exports.addDisposer = addDisposer;
function runAllDisposers(obj, topic) {
    const allDisposers = getDisposersArray(obj, topic);
    let disposer = allDisposers.pop();
    while (disposer !== undefined) {
        disposer();
        disposer = allDisposers.pop();
    }
}
exports.runAllDisposers = runAllDisposers;
