Fix for issue #4 -- nested callbacks with same index key

This commit is contained in:
Moos 2014-02-25 00:24:29 -08:00
parent 591b563667
commit 2d8c851810
5 changed files with 32 additions and 6 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
dict
node_modules

View File

@ -1,7 +1,7 @@
wordpos
=======
wordpos is a set of part-of-speech utilities for Node.js using [natural's](http://github.com/NaturalNode/natural) WordNet module.
wordpos is a set of part-of-speech (POS) utilities for Node.js using [natural's](http://github.com/NaturalNode/natural) WordNet module.
## Usage
@ -353,4 +353,4 @@ License
(The MIT License)
Copyright (c) 2012, mooster@42at.com
Copyright (c) 2012, 2014 mooster@42at.com

View File

@ -3,7 +3,7 @@
"author": "Moos <mooster@42at.com>",
"keywords": ["natural", "language", "wordnet", "pos"],
"description": "wordpos is a set of part-of-speech utilities for Node.js using natural's WordNet module.",
"version": "0.1.8",
"version": "0.1.9",
"homepage": "https://github.com/moos/wordpos",
"engines": {
"node": ">=0.6"

View File

@ -182,7 +182,6 @@ describe('options passed to constructor', function() {
});
describe('profile option', function() {
var wp = new WordPOS({profile : true});
it('should return time argument for isX()', function(done){
@ -223,5 +222,23 @@ describe('profile option', function() {
});
describe('nested callbacks on same index key', function() {
var wp = new WordPOS(),
word1 = 'head',
word2 = word1 + 'er';
it('should call inner callback', function(done){
wp.getPOS(word1, function(result) {
expect(result.nouns[0]).toEqual(word1);
// inner call on word2
wp.getPOS(word2, function(result) {
expect(result.nouns[0]).toEqual(word2);
done();
});
});
});
});
function noop(){}

View File

@ -28,7 +28,8 @@ function loadFastIndex(dir, name) {
data = JSON.parse( fs.readFileSync(jsonFile,'utf8') );
//console.log('loaded %d buckets for %s', data.stats.buckets, data.name);
} catch(e) {
console.error('Error with fast index file %s\n ', jsonFile, e);
console.error('Error with fast index file. Try reinstalling from npm!');
throw e;
}
return data;
}
@ -94,7 +95,13 @@ function find(search, callback) {
return line.substring(0,line.indexOf(' '));
});
readCallbacks[key].forEach( test );
// live access callbacks cache in case nested cb's
// add to the array.
while (readCallbacks[key].length) {
test(readCallbacks[key].shift());
}
// now done - delete cb cache
delete readCallbacks[key];
if (--self.refcount == 0) {