liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
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
"use strict";
 
var FakeTimers = require("@sinonjs/fake-timers");
var fakeServer = require("./index");
 
// eslint-disable-next-line no-empty-function
function Server() {}
Server.prototype = fakeServer;
 
var fakeServerWithClock = new Server();
 
fakeServerWithClock.addRequest = function addRequest(xhr) {
    if (xhr.async) {
        if (typeof setTimeout.clock === "object") {
            this.clock = setTimeout.clock;
        } else {
            this.clock = FakeTimers.install();
            this.resetClock = true;
        }
 
        if (!this.longestTimeout) {
            var clockSetTimeout = this.clock.setTimeout;
            var clockSetInterval = this.clock.setInterval;
            var server = this;
 
            this.clock.setTimeout = function(fn, timeout) {
                server.longestTimeout = Math.max(
                    timeout,
                    server.longestTimeout || 0
                );
 
                return clockSetTimeout.apply(this, arguments);
            };
 
            this.clock.setInterval = function(fn, timeout) {
                server.longestTimeout = Math.max(
                    timeout,
                    server.longestTimeout || 0
                );
 
                return clockSetInterval.apply(this, arguments);
            };
        }
    }
 
    return fakeServer.addRequest.call(this, xhr);
};
 
fakeServerWithClock.respond = function respond() {
    var returnVal = fakeServer.respond.apply(this, arguments);
 
    if (this.clock) {
        this.clock.tick(this.longestTimeout || 0);
        this.longestTimeout = 0;
 
        if (this.resetClock) {
            this.clock.uninstall();
            this.resetClock = false;
        }
    }
 
    return returnVal;
};
 
fakeServerWithClock.restore = function restore() {
    if (this.clock) {
        this.clock.uninstall();
    }
 
    return fakeServer.restore.apply(this, arguments);
};
 
module.exports = fakeServerWithClock;