(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", "reflect-metadata", "./AtomMap"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.UMD = exports.DI = exports.ArrayHelper = exports.CancelToken = void 0;
    require("reflect-metadata");
    const AtomMap_1 = require("./AtomMap");
    /**
     *
     *
     * @export
     * @class CancelToken
     */
    class CancelToken {
        constructor(timeout = -1) {
            this.listeners = [];
            this.mCancelled = null;
            this.cancelTimeout = null;
            if (timeout > 0) {
                this.cancelTimeout = setTimeout(() => {
                    this.cancelTimeout = null;
                    this.cancel("timeout");
                }, timeout);
            }
        }
        get cancelled() {
            return this.mCancelled;
        }
        cancel(r = "cancelled") {
            this.mCancelled = r;
            const existing = this.listeners.slice(0);
            this.listeners.length = 0;
            for (const fx of existing) {
                fx(r);
            }
            this.dispose();
        }
        reset() {
            this.mCancelled = null;
            this.dispose();
        }
        dispose() {
            this.listeners.length = 0;
            if (this.cancelTimeout) {
                clearTimeout(this.cancelTimeout);
            }
        }
        registerForCancel(f) {
            if (this.mCancelled) {
                f(this.mCancelled);
                this.cancel();
                return;
            }
            this.listeners.push(f);
        }
    }
    exports.CancelToken = CancelToken;
    class ArrayHelper {
        static remove(a, filter) {
            for (let i = 0; i < a.length; i++) {
                const item = a[i];
                if (filter(item)) {
                    a.splice(i, 1);
                    return true;
                }
            }
            return false;
        }
    }
    exports.ArrayHelper = ArrayHelper;
    // tslint:disable-next-line
    Array.prototype["groupBy"] = function (keySelector) {
        const map = new AtomMap_1.default();
        const groups = [];
        for (const iterator of this) {
            const key = keySelector(iterator);
            let g = map.get(key);
            if (!g) {
                g = [];
                g.key = key;
                groups.push(g);
            }
            g.push(iterator);
        }
        map.clear();
        return groups;
    };
    const globalNS = (typeof window !== "undefined" ? window : global);
    exports.DI = (globalNS).UMD;
    exports.UMD = (globalNS).UMD;
});
//# sourceMappingURL=types.js.map