source: Dev/branches/play-2.0.1/documentation/api/scala/lib/scheduler.js @ 322

Last change on this file since 322 was 322, checked in by hendrikvanantwerpen, 13 years ago

Added Play! framework and application with Jena dependency. Working on
the basic things now (login/register), after that start implementing
our data model.

File size: 2.8 KB
Line 
1// © 2010 EPFL/LAMP
2// code by Gilles Dubochet
3
4function Scheduler() {
5    var scheduler = this;
6    var resolution = 0;
7    this.timeout = undefined;
8    this.queues = new Array(0); // an array of work pacakges indexed by index in the labels table.
9    this.labels = new Array(0); // an indexed array of labels indexed by priority. This should be short.
10    this.label = function(name, priority) {
11        this.name = name;
12        this.priority = priority;
13    }
14    this.work = function(fn, self, args) {
15        this.fn = fn;
16        this.self = self;
17        this.args = args;
18    }
19    this.addLabel = function(name, priority) {
20        var idx = 0;
21        while (idx < scheduler.queues.length && scheduler.labels[idx].priority <= priority) { idx = idx + 1; }
22        scheduler.labels.splice(idx, 0, new scheduler.label(name, priority));
23        scheduler.queues.splice(idx, 0, new Array(0));
24    }
25    this.clearLabel = function(name) {
26        var idx = 0;
27        while (idx < scheduler.queues.length && scheduler.labels[idx].name != name) { idx = idx + 1; }
28        if (idx < scheduler.queues.length && scheduler.labels[i].name == name) {
29            scheduler.labels.splice(idx, 1);
30            scheduler.queues.splice(idx, 1);
31        }
32    }
33    this.nextWork = function() {
34        var fn = undefined;
35        var idx = 0;
36        while (idx < scheduler.queues.length && scheduler.queues[idx].length == 0) { idx = idx + 1; }
37        if (idx < scheduler.queues.length && scheduler.queues[idx].length > 0) {
38            var fn = scheduler.queues[idx].shift();
39        }
40        return fn;
41    }
42    this.add = function(labelName, fn, self, args) {
43        var doWork = function() {
44            scheduler.timeout = setTimeout(function() {
45                var work = scheduler.nextWork();
46                if (work != undefined) {
47                    if (work.args == undefined) { work.args = new Array(0); }
48                    work.fn.apply(work.self, work.args);
49                    doWork();
50                }
51                else {
52                    scheduler.timeout = undefined;
53                }
54            }, resolution);
55        }
56        var idx = 0;
57        while (idx < scheduler.labels.length && scheduler.labels[idx].name != labelName) { idx = idx + 1; }
58        if (idx < scheduler.queues.length && scheduler.labels[idx].name == labelName) {
59            scheduler.queues[idx].push(new scheduler.work(fn, self, args));
60            if (scheduler.timeout == undefined) doWork();
61        }
62        else throw("queue for add is non existant");
63    }
64    this.clear = function(labelName) {
65        var idx = 0;
66        while (idx < scheduler.labels.length && scheduler.labels[idx].name != labelName) { idx = idx + 1; }
67        if (idx < scheduler.queues.length && scheduler.labels[idx].name == labelName) {
68            scheduler.queues[idx] = new Array();
69        }
70    }
71};
Note: See TracBrowser for help on using the repository browser.