<commit_before>Impromptu = require '../impromptu'

# An abstract class that manages how a method is cached.
class Cache
  constructor: (@impromptu, @name, @options) ->
    # Build our own `run` instance-method to accurately reflect the fact that
    # `run` is always asynchronous and requires an argument to declare itself
    # as such to our API. This feels a bit hacky, but using CoffeeScript's `=>`
    # in `Cache.prototype` creates a zero argument function when compiled.
    protoRun = @run
    @run = (done) =>
      protoRun.apply @, arguments


  # The main method.
  #
  # Accepts a `fn` with a signature of `err, results`, where `results` is the
  # cached value. Optionally updates the cache.
  #
  # This method is bound to the instance so the method can be passed around
  # without the instance.
  run: (fn) -> throw Impromptu.AbstractError


  # Gets the cached value.
  #
  # Accepts a `fn` with a signature of `err, results`, where `results` is the
  # cached value. Does not update the cache.
  get: (fn) -> throw Impromptu.AbstractError


  # Updates the cached value.
  #
  # Accepts a `fn` with a signature of `err, success`, where `success` is a
  # boolean indicating whether the cached value was updated.
  set: (fn) -> throw Impromptu.AbstractError


  # Clears the cached value.
  #
  # Accepts a `fn` with a signature of `err, success`, where `success` is a
  # boolean indicating whether the value was removed from the cache.
  unset: (fn) -> throw Impromptu.AbstractError


  # Private. A low-level method to properly call the cache's update method.
  _update: (done) =>
    done ||= (err, results) ->

    # If the method accepts an argument, it is asynchronous.
    return @options.update.call @options.context, done if @options.update.length

    try
      results = @options.update.call @options.context
    catch err
    finally
      # Process the results if method is synchronous.
      done err, results


# Expose `Cache`.
exports = module.exports = Cache
<commit_msg>Allow the cache's run method to be overloaded.<commit_after>Impromptu = require '../impromptu'

# An abstract class that manages how a method is cached.
class Cache
  constructor: (@impromptu, @name, @options) ->
    # Build our own `run` instance-method to accurately reflect the fact that
    # `run` is always asynchronous and requires an argument to declare itself
    # as such to our API. This feels a bit hacky, but using CoffeeScript's `=>`
    # in `Cache.prototype` creates a zero argument function when compiled.
    protoRun = @run
    @run = (done) =>
      if @options.run
        # If a `run` option is defined, use that instead.
        @options.run.apply @, arguments
      else
        # Otherwise, use the default `run` method.
        protoRun.apply @, arguments


  # The main method: `run` acts as a deterministic way to fetch and potentially
  # update the cache.
  #
  # Accepts a `fn` with a signature of `err, results`, where `results` is the
  # cached value.
  #
  # This method is bound to the instance so the method can be passed around
  # without the instance. Can be overloaded using `options.run`.
  run: (fn) -> throw Impromptu.AbstractError


  # Gets the cached value.
  #
  # Accepts a `fn` with a signature of `err, results`, where `results` is the
  # cached value. Does not update the cache.
  get: (fn) -> throw Impromptu.AbstractError


  # Updates the cached value.
  #
  # Accepts a `fn` with a signature of `err, success`, where `success` is a
  # boolean indicating whether the cached value was updated.
  set: (fn) -> throw Impromptu.AbstractError


  # Clears the cached value.
  #
  # Accepts a `fn` with a signature of `err, success`, where `success` is a
  # boolean indicating whether the value was removed from the cache.
  unset: (fn) -> throw Impromptu.AbstractError


  # Private. A low-level method to properly call the cache's update method.
  _update: (done) =>
    done ||= (err, results) ->

    # If the method accepts an argument, it is asynchronous.
    return @options.update.call @options.context, done if @options.update.length

    try
      results = @options.update.call @options.context
    catch err
    finally
      # Process the results if method is synchronous.
      done err, results


# Expose `Cache`.
exports = module.exports = Cache

{'fraction_non_alphanumeric': 0.09109826589595375, 'fraction_numerical': 0.0, 'mean_word_length': 3.0927152317880795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '5286416', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1224, 'n_words': 547}