Comparing version
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -39,26 +39,40 @@ Object.defineProperty(exports, "__esModule", { | ||
var createSelectorCreator = exports.createSelectorCreator = function createSelectorCreator(memoize, memoizeOptions) { | ||
return function (fns) { | ||
var recomputations = 0; | ||
var resultFn = fns[fns.length - 1]; | ||
var dependencies = fns.slice(0, -1); | ||
function makeDependenciesFn(fns, next) { | ||
fns = Array.isArray(fns) ? fns : [fns]; | ||
return function () { | ||
var _arguments = arguments; | ||
var memoizedResultFn = memoize(function () { | ||
recomputations += 1; | ||
return resultFn.apply(null, arguments); | ||
var params = fns.map(function (fn) { | ||
return fn.apply(null, _arguments); | ||
}); | ||
return next.apply(null, params); | ||
}; | ||
} | ||
var selector = dependencies.reduceRight(function (next, currentFns, index) { | ||
currentFns = Array.isArray(currentFns) ? currentFns : [currentFns]; | ||
function createNestedSelectors(create, fns) { | ||
if (Array.isArray(fns)) { | ||
return fns.map(function (fn) { | ||
return Array.isArray(fn) ? create(fn) : fn; | ||
}); | ||
} | ||
return fns; | ||
} | ||
var createSelectorCreator = exports.createSelectorCreator = function createSelectorCreator(memoize, memoizeOptions) { | ||
function createSelector(fns) { | ||
var initialRecomputations = Array(fns.length).fill(0); | ||
var recomputations = initialRecomputations; | ||
var selector = fns.reduceRight(function (next, currentFns, index) { | ||
currentFns = createNestedSelectors(createSelector, currentFns); | ||
var selectors = next ? makeDependenciesFn(currentFns, next) : currentFns; | ||
return memoize(function () { | ||
var _arguments = arguments; | ||
var params = currentFns.map(function (fn) { | ||
return fn.apply(null, _arguments); | ||
}); | ||
return next.apply(null, params); | ||
recomputations[index] += 1; | ||
return selectors.apply(null, arguments); | ||
}, memoizeOptions); | ||
}, memoizedResultFn); | ||
}, null); | ||
selector.resultFn = resultFn; | ||
selector.recomputations = function () { | ||
@@ -68,6 +82,7 @@ return recomputations; | ||
selector.resetRecomputations = function () { | ||
return recomputations = 0; | ||
return recomputations = initialRecomputations; | ||
}; | ||
return selector; | ||
}; | ||
} | ||
return createSelector; | ||
}; | ||
@@ -77,3 +92,3 @@ | ||
return function (selectors) { | ||
if (typeof selectors === 'function') { | ||
if (Array.isArray(selectors)) { | ||
return createSelector(selectors); | ||
@@ -80,0 +95,0 @@ } |
{ | ||
"name": "re-select", | ||
"version": "0.1.1", | ||
"version": "0.2.0-0", | ||
"description": "Memoized selector library", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -29,30 +29,45 @@ const defaultEqualityCheck = (a, b) => a === b; | ||
export const createSelectorCreator = (memoize, memoizeOptions) => fns => { | ||
let recomputations = 0; | ||
const resultFn = fns[fns.length - 1]; | ||
const dependencies = fns.slice(0, -1); | ||
function makeDependenciesFn(fns, next) { | ||
fns = Array.isArray(fns) ? fns : [fns]; | ||
return function() { | ||
const params = fns.map(fn => fn.apply(null, arguments)); | ||
return next.apply(null, params); | ||
} | ||
} | ||
const memoizedResultFn = memoize( | ||
function() { | ||
recomputations += 1; | ||
return resultFn.apply(null, arguments); | ||
} | ||
) | ||
function createNestedSelectors(create, fns) { | ||
if (Array.isArray(fns)) { | ||
return fns.map(fn => Array.isArray(fn) ? create(fn) : fn) | ||
} | ||
const selector = dependencies.reduceRight(function(next, currentFns, index) { | ||
currentFns = Array.isArray(currentFns) ? currentFns : [currentFns]; | ||
return memoize(function() { | ||
const params = currentFns.map(fn => fn.apply(null, arguments)); | ||
return next.apply(null, params); | ||
}, memoizeOptions); | ||
}, memoizedResultFn) | ||
return fns; | ||
} | ||
selector.resultFn = resultFn; | ||
selector.recomputations = () => recomputations; | ||
selector.resetRecomputations = () => recomputations = 0; | ||
return selector; | ||
export const createSelectorCreator = (memoize, memoizeOptions) => { | ||
function createSelector(fns) { | ||
const initialRecomputations = Array(fns.length).fill(0); | ||
let recomputations = initialRecomputations; | ||
const selector = fns.reduceRight(function(next, currentFns, index) { | ||
currentFns = createNestedSelectors(createSelector, currentFns); | ||
const selectors = next | ||
? makeDependenciesFn(currentFns, next) | ||
: currentFns; | ||
return memoize(function() { | ||
recomputations[index] += 1; | ||
return selectors.apply(null, arguments); | ||
}, memoizeOptions); | ||
}, null) | ||
selector.recomputations = () => recomputations; | ||
selector.resetRecomputations = () => recomputations = initialRecomputations; | ||
return selector; | ||
} | ||
return createSelector; | ||
} | ||
export const createSelectorsCreator = createSelector => selectors => { | ||
if (typeof selectors === 'function') { | ||
if (Array.isArray(selectors)) { | ||
return createSelector(selectors); | ||
@@ -59,0 +74,0 @@ } |
7847
8.77%156
16.42%