alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (scheduler.js)

This example Scala source code file (scheduler.js) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

array, scheduler, this

The scheduler.js Scala example source code

// © 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();
        }
    }
};

Other Scala source code examples

Here is a short list of links related to this Scala scheduler.js source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.