Files
memoizee/ext/ref-counter.js
Mariusz Nowak eb72d16bf6 Major reorganization and partial refactoring
- 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
2014-04-27 12:11:06 +02:00

40 lines
1.1 KiB
JavaScript

// Reference counter, useful for garbage collector like functionality
'use strict';
var d = require('d')
, extensions = require('../lib/registered-extensions')
, create = Object.create, defineProperties = Object.defineProperties;
extensions.refCounter = function (ignore, conf, options) {
var cache, postfix;
cache = create(null);
postfix = (options.async && extensions.async) ? 'async' : '';
conf.on('set' + postfix, function (id, length) { cache[id] = length || 1; });
conf.on('get' + postfix, function (id) { ++cache[id]; });
conf.on('delete' + postfix, function (id) { delete cache[id]; });
conf.on('clear' + postfix, function () { cache = {}; });
defineProperties(conf.memoized, {
deleteRef: d(function () {
var id = conf.get(arguments);
if (id === null) return null;
if (!cache[id]) return null;
if (!--cache[id]) {
conf.delete(id);
return true;
}
return false;
}),
getRefCount: d(function () {
var id = conf.get(arguments);
if (id === null) return 0;
if (!cache[id]) return 0;
return cache[id];
})
});
};