Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- /**
- * @example
- *
- * var Q = Q(function(o, done) {
- *
- * }, max_queue_item)
- *
- * q.enqueue(o, function(r) {
- *
- * }, ctx)
- *
- *
- */
- function Q(fn, _max_size, when_empty) {
- const ctx = this;
- const max_size = _max_size ? _max_size : Infinity;
- let e = null;
- let count = 0;
- const ret = {
- enqueue,
- };
- function QItem(o, fn, ctx) {
- return {
- fn,
- ctx,
- data: o,
- next: null,
- };
- }
- function enqueue(o, fn, ctx) {
- if (count >= max_size) {
- throw new Error('queue fulled');
- }
- count++;
- const item = QItem(o, fn, ctx);
- if (e == null) {
- e = item;
- process.nextTick(next.bind(ctx, item));
- } else {
- e.next = item;
- e = e.next;
- }
- return ret;
- }
- function next(h) {
- if (h == null) {
- e = null;
- if (when_empty) when_empty();
- return;
- }
- count--;
- fn(h.data, (...args) => {
- h.fn.apply(h.ctx || ctx, args);
- }).then(next.bind(ctx, h.next), next.bind(ctx, h.next));
- }
- return ret;
- }
- module.exports = Q;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement