<commit_before>"use strict";

var LRU = require("lru-cache");

module.exports = function(options) {
  var locks = LRU(),
      cache = LRU(options);

  return function locked(fn) {
    return function() {
      var args = Array.prototype.slice.call(arguments, 0),
          // extract the eventual callback
          callback = args.pop();

      // replace the callback with a lock function
      args.push(function lock(key, generator) {
        var data;
        if ((data = cache.get(key))) {
          // cache hit!
          return setImmediate(function() {
            return callback.apply(null, [null].concat(data));
          });
        }

        var callbacks;
        if ((callbacks = locks.get(key))) {
          // already cached
          callbacks.push(callback);
          locks.set(key, callbacks);
          return;
        }

        locks.set(key, [callback]);

        return generator(function unlock(err) {
          args = Array.prototype.slice.call(arguments, 0);

          var data = args.slice(1),
              waiting = locks.get(key);

          // clear the lock now that we've got a list of pending callbacks
          locks.del(key);

          if (!err) {
            // store the data as an array
            cache.set(key, data);
          }

          // pass generated data to all waiting callbacks
          waiting.forEach(function(cb) {
            return setImmediate(function() {
              return cb.apply(null, args);
            });
          });
        });
      });

      // call the wrapped function with updated arguments
      return fn.apply(null, args);
    };
  };
};
<commit_msg>Support warming calls w/o callbacks<commit_after>"use strict";

var LRU = require("lru-cache");

module.exports = function(options) {
  var locks = LRU(),
      cache = LRU(options);

  return function locked(fn) {
    return function() {
      var args = Array.prototype.slice.call(arguments, 0);

      // extract the eventual callback, defaulting to a noop if none was
      // provided
      var callback = function() {};

      if (typeof args[args.length - 1] === "function") {
        callback = args.pop();
      }

      // replace the callback with a lock function
      args.push(function lock(key, generator) {
        var data;
        if ((data = cache.get(key))) {
          // cache hit!
          return setImmediate(function() {
            return callback.apply(null, [null].concat(data));
          });
        }

        var callbacks;
        if ((callbacks = locks.get(key))) {
          // already cached
          callbacks.push(callback);
          locks.set(key, callbacks);
          return;
        }

        locks.set(key, [callback]);

        return generator(function unlock(err) {
          args = Array.prototype.slice.call(arguments, 0);

          var data = args.slice(1),
              waiting = locks.get(key);

          // clear the lock now that we've got a list of pending callbacks
          locks.del(key);

          if (!err) {
            // store the data as an array
            cache.set(key, data);
          }

          // pass generated data to all waiting callbacks
          waiting.forEach(function(cb) {
            return setImmediate(function() {
              return cb.apply(null, args);
            });
          });
        });
      });

      // call the wrapped function with updated arguments
      return fn.apply(null, args);
    };
  };
};

{'fraction_non_alphanumeric': 0.12120335551055829, 'fraction_numerical': 0.00202487706103558, 'mean_word_length': 1.7249802994483845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '578311', 'n_tokens_mistral': 947, 'n_tokens_neox': 912, 'n_words': 258}