| 1 |
"use strict"; |
| 2 |
|
| 3 |
var callable = require("es5-ext/object/valid-callable") |
| 4 |
, forEach = require("es5-ext/object/for-each") |
| 5 |
, extensions = require("./lib/registered-extensions") |
| 6 |
, configure = require("./lib/configure-map") |
| 7 |
, resolveLength = require("./lib/resolve-length"); |
| 8 |
|
| 9 |
module.exports = function self(fn/*, options */) { |
| 10 |
var options, length, conf; |
| 11 |
|
| 12 |
callable(fn); |
| 13 |
options = Object(arguments[1]); |
| 14 |
|
| 15 |
if (options.async && options.promise) { |
| 16 |
throw new Error("Options 'async' and 'promise' cannot be used together"); |
| 17 |
} |
| 18 |
|
| 19 |
// Do not memoize already memoized function |
| 20 |
if (hasOwnProperty.call(fn, "__memoized__") && !options.force) return fn; |
| 21 |
|
| 22 |
// Resolve length; |
| 23 |
length = resolveLength(options.length, fn.length, options.async && extensions.async); |
| 24 |
|
| 25 |
// Configure cache map |
| 26 |
conf = configure(fn, length, options); |
| 27 |
|
| 28 |
// Bind eventual extensions |
| 29 |
forEach(extensions, function (extFn, name) { |
| 30 |
if (options[name]) extFn(options[name], conf, options); |
| 31 |
}); |
| 32 |
|
| 33 |
if (self.__profiler__) self.__profiler__(conf); |
| 34 |
|
| 35 |
conf.updateEnv(); |
| 36 |
return conf.memoized; |
| 37 |
}; |