# Code coverage report for src/async/Task.js

# all files / src/async/ Task.js

23.26% Statements 10/43
0% Branches 0/12
0% Functions 0/12
23.26% Lines 10/43

    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

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     import notifier from '../core/mixin/notifier';
    import request from '../core/request';
    import util  from '../core/util';

    /**
     * @constructor
     * @alias clay.async.Task
     * @mixes clay.core.mixin.notifier
     */
    var Task = function() {
        this._fullfilled = false;
        this._rejected = false;
    };
    /**
     * Task successed
     * @param {} data
     */
    Task.prototype.resolve = function(data) {
        this._fullfilled = true;
        this._rejected = false;
        this.trigger('success', data);
    };
    /**
     * Task failed
     * @param {} err
     */
    Task.prototype.reject = function(err) {
        this._rejected = true;
        this._fullfilled = false;
        this.trigger('error', err);
    };
    /**
     * If task successed
     * @return {boolean}
     */
    Task.prototype.isFullfilled = function() {
        return this._fullfilled;
    };
    /**
     * If task failed
     * @return {boolean}
     */
    Task.prototype.isRejected = function() {
        return this._rejected;
    };
    /**
     * If task finished, either successed or failed
     * @return {boolean}
     */
    Task.prototype.isSettled = function() {
        return this._fullfilled || this._rejected;
    };

    util.extend(Task.prototype, notifier);

    function makeRequestTask(url, responseType) {
        var task = new Task();
        request.get({
            url: url,
            responseType: responseType,
            onload: function(res) {
                task.resolve(res);
            },
            onerror: function(error) {
                task.reject(error);
            }
        });
        return task;
    }
    /**
     * Make a request task
     * @param  {string|object|object[]|string[]} url
     * @param  {string} [responseType]
     * @example
     *     var task = Task.makeRequestTask('./a.json');
     *     var task = Task.makeRequestTask({
     *         url: 'b.bin',
     *         responseType: 'arraybuffer'
     *     });
     *     var tasks = Task.makeRequestTask(['./a.json', './b.json']);
     *     var tasks = Task.makeRequestTask([
     *         {url: 'a.json'},
     *         {url: 'b.bin', responseType: 'arraybuffer'}
     *     ]);
     * @return {clay.async.Task|clay.async.Task[]}
     */
    Task.makeRequestTask = function(url, responseType) {
        if (typeof url === 'string') {
            return makeRequestTask(url, responseType);
        } else if (url.url) {   //  Configure object
            var obj = url;
            return makeRequestTask(obj.url, obj.responseType);
        } else if (Array.isArray(url)) {  // Url list
            var urlList = url;
            var tasks = [];
            urlList.forEach(function(obj) {
                var url, responseType;
                if (typeof obj === 'string') {
                    url = obj;
                } else if (Object(obj) === obj) {
                    url = obj.url;
                    responseType = obj.responseType;
                }
                tasks.push(makeRequestTask(url, responseType));
            });
            return tasks;
        }
    };
    /**
     * @return {clay.async.Task}
     */
    Task.makeTask = function() {
        return new Task();
    };

    util.extend(Task.prototype, notifier);

    export default Task;