"use strict";

var NodeEventEmitter = require("events").EventEmitter,
    Event = require("../shared/Event.class.js"),
    Class = require("alamid-class");

var Base = Class(NodeEventEmitter).extend("Base", {
    _disposed: false,
    constructor: function () {
        this._super();
        this.dispose = createDisposeGuard(this.dispose);
    },
    isDisposed: function () {
        return this._disposed === true;
    },
    dispose: function () {
        NodeEventEmitter.prototype.removeAllListeners.call(this);
    }
});

/**
 * Returns a function which ensures that dispose() is only called once.
 */
function createDisposeGuard(dispose) { /* jshint validthis: true */
    return function disposeGuard() {
        if (this._disposed) {
            return;
        }
        // Emit the dispose event as soon as possible because when there is a listener,
        // it is likely that the listener needs to run code before all references have been cleared.
        this._disposed = true;
        this.emit("dispose", new DisposeEvent(this));
        dispose.call(this);
    };
}

/**
 * @class DisposeEvent
 * @extends Event
 */
var DisposeEvent = Event.extend("DisposeEvent", {
    name: "DisposeEvent"
});

module.exports = Base;
@@ -415,24 +415,436 @@
 rue;%0A    %7D,%0A
+    /**%0A     * Returns an async function, that does only run when the instance has not been disposed yet.%0A     *%0A     * @param %7BFunction%7D fn%0A     * @return %7BFunction%7D%0A     */%0A    async: function (fn) %7B%0A        var self = this;%0A%0A        return function checkIsDisposed() %7B%0A            if (self.isDisposed()) %7B%0A                return;%0A            %7D%0A%0A            return fn.apply(this, arguments);%0A        %7D;%0A    %7D,%0A
     dispose:

Add async()-method to Base.class