<commit_before>"use strict";
const async = require('async');
const Q     = require('q');
const co    = require('co');

const fifo = async.queue(function (task, callback) {
  task(callback);
}, 1);

module.exports = function AbstractService () {

  /**
   * Adds a promise to a FIFO stack of promises, so the given promise will be executed against a shared FIFO stack.
   * @param p
   * @returns {Q.Promise<T>} A promise wrapping the promise given in the parameter.
   */
  this.pushFIFO = (p) => {
    // Return a promise that will be done after the fifo has executed the given promise
    return Q.Promise((resolve, reject) => {
      // Push the promise on the stack
      fifo.push(function (cb) {
        co(function*(){
          // OK its the turn of given promise, execute it
          try {
            const res = yield p();
            // Finished, we end the function in the FIFO
            cb(null, res);
          } catch (e) {
            // Errored, we end the function with an error
            cb(e);
          }
        });
      }, (err, res) => {
        // An error occured => reject promise
        if (err) return reject(err);
        // Success => we resolve with given promise result
        resolve(res);
      });
    });
  };<commit_msg>[enh] API: provide an accessor to BlockchainService flow control<commit_after>"use strict";
const async = require('async');
const Q     = require('q');
const co    = require('co');

const fifo = async.queue(function (task, callback) {
  task(callback);
}, 1);

module.exports = function AbstractService () {

  /**
   * Gets the queue object for advanced flow control.
   */
  this.getFIFO = () => fifo;

  /**
   * Adds a promise to a FIFO stack of promises, so the given promise will be executed against a shared FIFO stack.
   * @param p
   * @returns {Q.Promise<T>} A promise wrapping the promise given in the parameter.
   */
  this.pushFIFO = (p) => {
    // Return a promise that will be done after the fifo has executed the given promise
    return Q.Promise((resolve, reject) => {
      // Push the promise on the stack
      fifo.push(function (cb) {
        co(function*(){
          // OK its the turn of given promise, execute it
          try {
            const res = yield p();
            // Finished, we end the function in the FIFO
            cb(null, res);
          } catch (e) {
            // Errored, we end the function with an error
            cb(e);
          }
        });
      }, (err, res) => {
        // An error occured => reject promise
        if (err) return reject(err);
        // Success => we resolve with given promise result
        resolve(res);
      });
    });
  };
{'fraction_non_alphanumeric': 0.11898545318910854, 'fraction_numerical': 0.0007459903021260724, 'mean_word_length': 2.0756880733944953, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, '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': '1981546', 'n_tokens_mistral': 761, 'n_tokens_neox': 741, 'n_words': 294}