(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 });
    var Disposable = function (target, key) {
        // property value
        var iVal = target[key];
        var keyName = "_" + key;
        var disposableKey = "_$_disposable" + key;
        target[keyName] = iVal;
        if (iVal) {
            target[disposableKey] = target.registerDisposable(iVal);
        }
        // property getter
        var getter = function () {
            return this[keyName];
        };
        // property setter
        var setter = function (newVal) {
            var oldValue = this[keyName];
            // tslint:disable-next-line:triple-equals
            if (oldValue == newVal) {
                return;
            }
            var 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