# all files / TypeScript.NET/source/System/Tasks/ deferImmediate.js

54.88% Statements 45/82
40% Branches 14/35
43.75% Functions 7/16
55% Lines 44/80

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129

    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    19×
    19×
    3023×
    3023×
    3023×

    3023×

    19×
    19×

    19×

    1×
    1×
    1×
    3023×
    3023×

    3023×

    1×
    3023×
    19×
    19×

    1×
    3023×
    3023×

    3023×
    3023×
    3023×

    1×
    1×

    1×
    1×

    1×
    1×
    19×

    1×
    1×

     /*!
     * @author electricessence / https://github.com/electricessence/
     * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
     * Based on code from: https://github.com/kriskowal/q
     */
    (function (factory) {
        Eif (typeof module === 'object' && typeof module.exports === 'object') {
            var v = factory(require, exports); Iif (v !== undefined) module.exports = v;
        }
        else if (typeof define === 'function' && define.amd) {
            define(["require", "exports", "../Types", "../Collections/LinkedNodeList", "../Collections/Queue"], factory);
        }
    })(function (require, exports) {
        "use strict";
        var Types_1 = require("../Types");
        var LinkedNodeList_1 = require("../Collections/LinkedNodeList");
        var Queue_1 = require("../Collections/Queue");
        var requestTick;
        var isNodeJS = false;
        var flushing = false;
        function flush() {
            var entry;
            while (entry = immediateQueue.first) {
                var task_1 = entry.task, domain = entry.domain;
                immediateQueue.removeNode(entry);
                Iif (domain)
                    domain.enter();
                runSingle(task_1, domain);
            }
            var task;
            while (task = laterQueue.dequeue()) {
                runSingle(task);
            }
            flushing = false;
        }
        var immediateQueue = new LinkedNodeList_1.LinkedNodeList();
        var laterQueue = new Queue_1.Queue();
        function runSingle(task, domain) {
            try {
                task();
            }
            catch (e) {
                if (isNodeJS) {
                    if (domain) {
                        domain.exit();
                    }
                    setTimeout(flush, 0);
                    if (domain) {
                        domain.enter();
                    }
                    throw e;
                }
                else {
                    setTimeout(function () {
                        throw e;
                    }, 0);
                }
            }
            Iif (domain) {
                domain.exit();
            }
        }
        function requestFlush() {
            if (!flushing) {
                flushing = true;
                requestTick();
            }
        }
        function deferImmediate(task) {
            var _this = this;
            var entry = {
                task: task,
                domain: isNodeJS && process['domain']
            };
            immediateQueue.addNode(entry);
            requestFlush();
            return {
                cancel: function () { return !!immediateQueue.removeNode(entry); },
                dispose: function () { _this.cancel(); }
            };
        }
        exports.deferImmediate = deferImmediate;
        function runAfterDeferred(task) {
            laterQueue.enqueue(task);
            requestFlush();
        }
        exports.runAfterDeferred = runAfterDeferred;
        Eif (Types_1.Type.isObject(process)
            && process.toString() === "[object process]"
            && process.nextTick) {
            isNodeJS = true;
            requestTick = function () {
                process.nextTick(flush);
            };
        }
        else if (typeof setImmediate === "function") {
            if (typeof window !== "undefined") {
                requestTick = setImmediate.bind(window, flush);
            }
            else {
                requestTick = function () {
                    setImmediate(flush);
                };
            }
        }
        else if (typeof MessageChannel !== "undefined") {
            var channel = new MessageChannel();
            channel.port1.onmessage = function () {
                requestTick = requestPortTick;
                channel.port1.onmessage = flush;
                flush();
            };
            var requestPortTick = function () {
                channel.port2.postMessage(0);
            };
            requestTick = function () {
                setTimeout(flush, 0);
                requestPortTick();
            };
        }
        else {
            requestTick = function () {
                setTimeout(flush, 0);
            };
        }
        Object.defineProperty(exports, "__esModule", { value: true });
        exports.default = deferImmediate;
    });
    //# sourceMappingURL=deferImmediate.js.map