(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const Disposable = (target, key) => {
        // property value
        const iVal = target[key];
        const keyName = "_" + key;
        const disposableKey = "_$_disposable" + key;
        target[keyName] = iVal;
        if (iVal) {
            target[disposableKey] = target.registerDisposable(iVal);
        }
        // property getter
        const getter = function () {
            return this[keyName];
        };
        // property setter
        const setter = function (newVal) {
            const oldValue = this[keyName];
            // tslint:disable-next-line:triple-equals
            if (oldValue == newVal) {
                return;
            }
            const oldDisposable = this[disposableKey];
            if (oldDisposable && oldDisposable.dispose) {
                oldDisposable.dispose();
            }
            this[keyName] = newVal;
        };
        // delete property
        if (delete target[key]) {
            // create new property with getter and setter
            Object.defineProperty(target, key, {
                get: getter,
                set: setter,
                enumerable: true,
                configurable: true
            });
        }
    };
    exports.default = Disposable;
});
//# sourceMappingURL=Disposable.js.map