mirror of
https://github.com/BreizhHardware/memoizee.git
synced 2026-01-18 16:37:21 +01:00
- Move out main modules from `lib` folder - Introduce `normalizer` based configurations, and convert primitive and regular handlers into thos normalizers (can be found in lib/normalizers folder). Custom normalizers can be provided at run time via `normaizer` option - Provide `plain` module which does not load any extensions or normalizers. Any extensions that have to be used should be required upfront and normalizers should be provided directly - Remove `method` option, instead `methods` and `methods-plan` modules are provided which generate descriptors for lazy created memoized methods - `profile` is no longer extension. To be used it should be required directly - Improve logic for `async` handling - Take out `max` extensionLRU logic into external `lru-queue` package - Remove `context` option - Remove possibility to access original arguments when resolvers are used - Assure expected length of memoized functions
90 lines
2.0 KiB
JavaScript
90 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
var indexOf = require('es5-ext/array/#/e-index-of')
|
|
, create = Object.create;
|
|
|
|
module.exports = function () {
|
|
var lastId = 0, map = [], cache = create(null);
|
|
return {
|
|
get: function (args) {
|
|
var index = 0, set = map, i, length = args.length;
|
|
if (length === 0) return set[length] || null;
|
|
if ((set = set[length])) {
|
|
while (index < (length - 1)) {
|
|
i = indexOf.call(set[0], args[index]);
|
|
if (i === -1) return null;
|
|
set = set[1][i];
|
|
++index;
|
|
}
|
|
i = indexOf.call(set[0], args[index]);
|
|
if (i === -1) return null;
|
|
return set[1][i] || null;
|
|
}
|
|
return null;
|
|
},
|
|
set: function (args) {
|
|
var index = 0, set = map, i, length = args.length;
|
|
if (length === 0) {
|
|
set[length] = ++lastId;
|
|
} else {
|
|
if (!set[length]) {
|
|
set[length] = [[], []];
|
|
}
|
|
set = set[length];
|
|
while (index < (length - 1)) {
|
|
i = indexOf.call(set[0], args[index]);
|
|
if (i === -1) {
|
|
i = set[0].push(args[index]) - 1;
|
|
set[1].push([[], []]);
|
|
}
|
|
set = set[1][i];
|
|
++index;
|
|
}
|
|
i = indexOf.call(set[0], args[index]);
|
|
if (i === -1) {
|
|
i = set[0].push(args[index]) - 1;
|
|
}
|
|
set[1][i] = ++lastId;
|
|
}
|
|
cache[lastId] = args;
|
|
return lastId;
|
|
},
|
|
delete: function (id) {
|
|
var index = 0, set = map, i, args = cache[id], length = args.length
|
|
, path = [];
|
|
if (length === 0) {
|
|
delete set[length];
|
|
} else if ((set = set[length])) {
|
|
while (index < (length - 1)) {
|
|
i = indexOf.call(set[0], args[index]);
|
|
if (i === -1) {
|
|
return;
|
|
}
|
|
path.push(set, i);
|
|
set = set[1][i];
|
|
++index;
|
|
}
|
|
i = indexOf.call(set[0], args[index]);
|
|
if (i === -1) {
|
|
return;
|
|
}
|
|
id = set[1][i];
|
|
set[0].splice(i, 1);
|
|
set[1].splice(i, 1);
|
|
while (!set[0].length && path.length) {
|
|
i = path.pop();
|
|
set = path.pop();
|
|
set[0].splice(i, 1);
|
|
set[1].splice(i, 1);
|
|
}
|
|
}
|
|
delete cache[id];
|
|
},
|
|
clear: function () {
|
|
map = [];
|
|
cache = create(null);
|
|
lastId = 0;
|
|
}
|
|
};
|
|
};
|