Skip to content

Commit

Permalink
Add events to memoize
Browse files Browse the repository at this point in the history
PR-URL: #284
  • Loading branch information
tshemsedinov committed Jan 7, 2018
1 parent 2fc77e9 commit 2a993af
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/memoize.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const memoize = (
return;
}
fn(...args, (err, data) => {
cache.set(key, { err, data });
memoized.add(key, err, data);
memoized.emit('memoize', key, err, data);
callback(err, data);
});
};
Expand All @@ -30,22 +31,33 @@ const memoize = (
size: 0,
maxSize: 0,
maxCount: 0,
events: {
timeout: null,
memoize: null,
overflow: null,
add: null,
del: null,
clear: null
}
};

Object.setPrototypeOf(memoized, Memoized.prototype);
return Object.assign(memoized, fields);
};

Memoized.prototype.clear = function() {
this.emit('clear');
this.cache.clear();
};

Memoized.prototype.add = function(key, err, data) {
this.emit('add', err, data);
this.cache.set(key, { err, data });
return this;
};

Memoized.prototype.del = function(key) {
this.emit('del', key);
this.cache.delete(key);
return this;
};
Expand All @@ -56,6 +68,28 @@ Memoized.prototype.get = function(key, callback) {
return this;
};

Memoized.prototype.on = function(
eventName, // string
listener // function, handler
// on('memoize', function(err, data))
// on('add', function(key, err, data))
// on('del', function(key))
// on('clear', function())
) {
if (eventName in this.events) {
this.events[eventName] = listener;
}
};

Memoized.prototype.emit = function(
// Emit Collector events
eventName, // string
...args // rest arguments
) {
const event = this.events[eventName];
if (event) event(...args);
};

module.exports = {
memoize,
};
24 changes: 24 additions & 0 deletions test/memoize.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ tap.test('memoize', (test) => {

const memoizedGetData = metasync.memoize(getData);

const keys = [];
memoizedGetData.on('memoize', (key) => {
keys.push(key);
});

memoizedGetData('file1', (err, data) => {
test.error(err);
test.strictSame(data, storage.file1);
Expand All @@ -31,6 +36,7 @@ tap.test('memoize', (test) => {
memoizedGetData('file2', (err, data) => {
test.error(err);
test.strictSame(data, storage.file2);
test.strictSame(keys, ['file1', 'file2']);
test.end();
});
});
Expand All @@ -53,6 +59,11 @@ tap.test('memoize clear cache', (test) => {

const memoizedGetData = metasync.memoize(getData);

let onClear = false;
memoizedGetData.on('clear', () => {
onClear = true;
});

memoizedGetData('file1', (err, data) => {
test.error(err);
test.strictSame(data, storage.file1);
Expand All @@ -61,6 +72,7 @@ tap.test('memoize clear cache', (test) => {
memoizedGetData('file1', (err, data) => {
test.error(err);
test.strictSame(data, Buffer.from('changed'));
test.strictSame(onClear, true);
test.end();
});
});
Expand All @@ -81,6 +93,11 @@ tap.test('memoize cache del', (test) => {

const memoizedGetData = metasync.memoize(getData);

let onDel = false;
memoizedGetData.on('del', () => {
onDel = true;
});

memoizedGetData('file1', (err, data) => {
test.error(err);
test.strictSame(data, storage.file1);
Expand All @@ -89,6 +106,7 @@ tap.test('memoize cache del', (test) => {
memoizedGetData('file1', (err, data) => {
test.error(err);
test.strictSame(data, Buffer.from('changed'));
test.strictSame(onDel, true);
test.end();
});
});
Expand All @@ -104,11 +122,17 @@ tap.test('memoize cache add', (test) => {

const memoizedGetData = metasync.memoize(getData);

let onAdd = false;
memoizedGetData.on('add', () => {
onAdd = true;
});

const file1 = Buffer.from('added');
memoizedGetData.add('file1', null, file1);
memoizedGetData('file1', (err, data) => {
test.error(err);
test.strictSame(data, Buffer.from('added'));
test.strictSame(onAdd, true);
test.end();
});
});
Expand Down

0 comments on commit 2a993af

Please sign in to comment.