<commit_before>{Transform} = require 'readable-stream'
_      = require 'underscore'
async  = require 'async'
debug  = require('debug') 'us:queue'
util   = require 'util'

class Queue extends Transform
  constructor: (@stream_opts, @options) ->
    super @stream_opts
    @options = { fn: @options } if _(@options).isFunction()
    _(@options).defaults concurrency: 1000
    @q = async.queue (payload, cb) =>
      @options.fn payload, (err, out) =>
        return cb err if err?
        @push out
        cb()
    , @options.concurrency
  _transform: (chunk, encoding, cb) =>
    async.whilst(
      => @q.length() >= @options.concurrency
      (cb_w) => setImmediate cb_w
      =>
        @q.push chunk
        cb()
    )
  _flush: (cb) =>
    @q.drain = cb
    @q.drain() if @q.tasks.length is 0

module.exports = (Understream) ->
  Understream.mixin Queue, 'queue'<commit_msg>queue: support node 0.8.25<commit_after>{Transform} = require 'readable-stream'
_      = require 'underscore'
async  = require 'async'
debug  = require('debug') 'us:queue'
util   = require 'util'
timers = require 'timers'

nextTick = timers?.setImmediate or process.nextTick

class Queue extends Transform
  constructor: (@stream_opts, @options) ->
    super @stream_opts
    @options = { fn: @options } if _(@options).isFunction()
    _(@options).defaults concurrency: 1000
    @q = async.queue (payload, cb) =>
      @options.fn payload, (err, out) =>
        return cb err if err?
        @push out
        cb()
    , @options.concurrency
  _transform: (chunk, encoding, cb) =>
    async.whilst(
      => @q.length() >= @options.concurrency
      (cb_w) => nextTick cb_w
      =>
        @q.push chunk
        cb()
    )
  _flush: (cb) =>
    @q.drain = cb
    @q.drain() if @q.tasks.length is 0

module.exports = (Understream) ->
  Understream.mixin Queue, 'queue'
{'fraction_non_alphanumeric': 0.1461038961038961, 'fraction_numerical': 0.007575757575757576, 'mean_word_length': 2.7889344262295084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1654380', 'n_tokens_mistral': 629, 'n_tokens_neox': 610, 'n_words': 159}