'use strict';

require('../_base').ext.refCounter = function (ignore, conf) {
	var cache, get, clear;

	cache = {};

	conf.on('init', function (id) { cache[id] = 1; });
	conf.on('hit', function (id) { ++cache[id]; });
	conf.on('purge', function (id) { delete cache[id]; });
	conf.on('purgeall', function () { cache = {}; });

	conf.memoized.clearRef = function () {
		var id = conf.get(arguments);
		if (cache.hasOwnProperty(id)) {
			if (!--cache[id]) {
				conf.clear(id)
				return true;
			}
			return false;
		}
		return null;
	};
};

'use strict';

var ext = require('../_base').ext;

ext.refCounter = function (ignore, conf, options) {
	var cache, get, clear, async;

	cache = {};
	async = options.async && ext.async;

	conf.on('init' + (async ? 'async' : ''), async ? function (id, length) {
		cache[id] = length;
	} : function (id) { cache[id] = 1; });
	conf.on('hit' + (async ? 'async' : ''), function (id) { ++cache[id]; });
	conf.on('purge' + (async ? 'async' : ''), function (id) {
		delete cache[id];
	});
	if (!async) {
		conf.on('purgeall', function () { cache = {}; });
	}

	conf.memoized.clearRef = function () {
		var id = conf.get(arguments);
		if (cache.hasOwnProperty(id)) {
			if (!--cache[id]) {
				conf.clear(id)
				return true;
			}
			return false;
		}
		return null;
	};
};

Support for async mode in refCounter
Support for async mode in refCounter
