summaryrefslogtreecommitdiff
path: root/src/scaladoc/scala/tools/nsc/doc/html/resource/lib/scheduler.js
blob: 4417f5b4385729f6023b05c03bd75c59fe82e596 (plain) (blame)
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
// © 2010 EPFL/LAMP
// code by Gilles Dubochet

function Scheduler() {
    var scheduler = this;
    var resolution = 0;
    this.timeout = undefined;
    this.queues = new Array(0); // an array of work pacakges indexed by index in the labels table.
    this.labels = new Array(0); // an indexed array of labels indexed by priority. This should be short.
    this.label = function(name, priority) {
        this.name = name;
        this.priority = priority;
    }
    this.work = function(fn, self, args) {
        this.fn = fn;
        this.self = self;
        this.args = args;
    }
    this.addLabel = function(name, priority) {
        var idx = 0;
        while (idx < scheduler.queues.length && scheduler.labels[idx].priority <= priority) { idx = idx + 1; }
        scheduler.labels.splice(idx, 0, new scheduler.label(name, priority));
        scheduler.queues.splice(idx, 0, new Array(0));
    }
    this.clearLabel = function(name) {
        var idx = 0;
        while (idx < scheduler.queues.length && scheduler.labels[idx].name != name) { idx = idx + 1; }
        if (idx < scheduler.queues.length && scheduler.labels[i].name == name) {
            scheduler.labels.splice(idx, 1);
            scheduler.queues.splice(idx, 1);
        }
    }
    this.nextWork = function() {
        var fn = undefined;
        var idx = 0;
        while (idx < scheduler.queues.length && scheduler.queues[idx].length == 0) { idx = idx + 1; }
        if (idx < scheduler.queues.length && scheduler.queues[idx].length > 0) {
            var fn = scheduler.queues[idx].shift();
        }
        return fn;
    }
    this.add = function(labelName, fn, self, args) {
        var doWork = function() {
            scheduler.timeout = setTimeout(function() {
                var work = scheduler.nextWork();
                if (work != undefined) {
                    if (work.args == undefined) { work.args = new Array(0); }
                    work.fn.apply(work.self, work.args);
                    doWork();
                }
                else {
                    scheduler.timeout = undefined;
                }
            }, resolution);
        }
        var idx = 0;
        while (idx < scheduler.labels.length && scheduler.labels[idx].name != labelName) { idx = idx + 1; }
        if (idx < scheduler.queues.length && scheduler.labels[idx].name == labelName) {
            scheduler.queues[idx].push(new scheduler.work(fn, self, args));
            if (scheduler.timeout == undefined) doWork();
        }
        else throw("queue for add is non existant");
    }
    this.clear = function(labelName) {
        var idx = 0;
        while (idx < scheduler.labels.length && scheduler.labels[idx].name != labelName) { idx = idx + 1; }
        if (idx < scheduler.queues.length && scheduler.labels[idx].name == labelName) {
            scheduler.queues[idx] = new Array();
        }
    }
};