yzt
2023-05-26 2f70f6727314edd84d8ec2bfe3ce832803f1ea77
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
define(['../Evented', '../_base/lang', './util'], function(Evented, lang, util){
    // module:
    //      dojo/request/notify
    // summary:
    //      Global notification API for dojo/request. Notifications will
    //      only be emitted if this module is required.
    //
    //      | require('dojo/request', 'dojo/request/notify',
    //      |     function(request, notify){
    //      |         notify('load', function(response){
    //      |             if(response.url === 'someUrl.html'){
    //      |                 console.log('Loaded!');
    //      |             }
    //      |         });
    //      |         request.get('someUrl.html');
    //      |     }
    //      | );
 
    var pubCount = 0,
        slice = [].slice;
 
    var hub = lang.mixin(new Evented, {
        onsend: function(data){
            if(!pubCount){
                this.emit('start');
            }
            pubCount++;
        },
        _onload: function(data){
            this.emit('done', data);
        },
        _onerror: function(data){
            this.emit('done', data);
        },
        _ondone: function(data){
            if(--pubCount <= 0){
                pubCount = 0;
                this.emit('stop');
            }
        },
        emit: function(type, event){
            var result = Evented.prototype.emit.apply(this, arguments);
 
            // After all event handlers have run, run _on* handler
            if(this['_on' + type]){
                this['_on' + type].apply(this, slice.call(arguments, 1));
            }
            return result;
        }
    });
 
    function notify(type, listener){
        // summary:
        //      Register a listener to be notified when an event
        //      in dojo/request happens.
        // type: String?
        //      The event to listen for. Events emitted: "start", "send",
        //      "load", "error", "done", "stop".
        // listener: Function?
        //      A callback to be run when an event happens.
        // returns:
        //      A signal object that can be used to cancel the listener.
        //      If remove() is called on this signal object, it will
        //      stop the listener from being executed.
        return hub.on(type, listener);
    }
    notify.emit = function(type, event, cancel){
        return hub.emit(type, event, cancel);
    };
 
    // Attach notify to dojo/request/util to avoid
    // try{ require('./notify'); }catch(e){}
    return util.notify = notify;
});