<commit_before><commit_msg>A janky queue class<commit_after>// jankyqueue.js
//
// A janky in-process queue
//
// Copyright 2012, StatusNet Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// I searched npm for in-process queues; got too many results; so I added
// to the problem.

var Queue = function(max) {
    this.max = max;
    this.waiting = [];
    this.running = 0;
};

Queue.prototype.enqueue = function(fn, args, callback) {
    var q = this;

    if (q.running < q.max) {
        q.run(fn, args, callback);
    } else {
        q.add(fn, args, callback);
    }
};

Queue.prototype.add = function(fn, args, callback) {
    var q = this;
    q.waiting.push([fn, args, callback]);
};

Queue.prototype.run = function(fn, args, callback) {
    var q = this,
        wrapped = function() {
            var results = arguments,
                next;
            q.running--;
            if ((q.running < q.max) && q.waiting.length > 0) {
                next = q.waiting.shift();
                q.run(next[0], next[1], next[2]);
            }
            callback.apply(null, results);
        },
        all = (args) ? args.concat([wrapped]) : [wrapped];

    q.running++;

    process.nextTick(function() {
        fn.apply(null, all);
    });
};

exports.Queue = Queue;
{'fraction_non_alphanumeric': 0.13489409141583056, 'fraction_numerical': 0.007246376811594203, 'mean_word_length': 2.305709023941068, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4394054', 'n_tokens_mistral': 549, 'n_tokens_neox': 535, 'n_words': 178}