-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
const util = require('util'); | ||
|
||
function Memoized() { | ||
} | ||
|
||
util.inherits(Memoized, Function); | ||
|
||
const memoize = ( | ||
// Create memoized function | ||
fn // function, sync or async | ||
// Returns: function, memoized | ||
) => { | ||
const cache = new Map(); | ||
|
||
const memoized = function(...args) { | ||
const callback = args.pop(); | ||
const key = args[0]; | ||
const record = cache.get(key); | ||
if (record) { | ||
callback(record.err, record.data); | ||
return; | ||
} | ||
fn(...args, (err, data) => { | ||
cache.set(key, { err, data }); | ||
callback(err, data); | ||
}); | ||
}; | ||
|
||
const fields = { | ||
cache, | ||
timeout: 0, | ||
limit: 0, | ||
size: 0, | ||
maxSize: 0 | ||
}; | ||
|
||
Object.setPrototypeOf(memoized, Memoized.prototype); | ||
return Object.assign(memoized, fields); | ||
}; | ||
|
||
Memoized.prototype.clear = function() { | ||
this.cache.clear(); | ||
}; | ||
|
||
module.exports = { | ||
memoize, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const tap = require('tap'); | ||
const metasync = require('..'); | ||
|
||
tap.test('memoize', (test) => { | ||
const storage = { | ||
file1: Buffer.from('file1'), | ||
file2: Buffer.from('file2'), | ||
}; | ||
|
||
const getData = (file, callback) => { | ||
process.nextTick(() => { | ||
const result = storage[file]; | ||
if (result) callback(null, result); | ||
else callback(new Error('File not found')); | ||
}); | ||
}; | ||
|
||
const memoizedGetData = metasync.memoize(getData); | ||
|
||
memoizedGetData('file1', (err, data) => { | ||
test.error(err); | ||
test.strictSame(data, storage.file1); | ||
memoizedGetData('file2', (err, data) => { | ||
test.error(err); | ||
test.strictSame(data, storage.file2); | ||
memoizedGetData('file1', (err, data) => { | ||
test.error(err); | ||
test.strictSame(data, storage.file1); | ||
memoizedGetData('file2', (err, data) => { | ||
test.error(err); | ||
test.strictSame(data, storage.file2); | ||
test.end(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |