Skip to content

Commit

Permalink
Merge pull request #44 from simon-o-matic/master
Browse files Browse the repository at this point in the history
Added the ability to chain an .on('load') on the constructor.
  • Loading branch information
felixge committed Jan 29, 2014
2 parents 6894f24 + f52d798 commit ac7b803
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -107,6 +107,12 @@ records before this event fires. Writing records however should be fine.
`length` is the amount of records the database is holding. This only counts each
key once, even if it had been overwritten.

You can chain the on load to the contructor as follows:

```javascript
var db = dirty(file).on('load', function() { ... });
```

### dirty event: 'drain' ()

Emitted whenever all records have been written to disk.
Expand Down
1 change: 1 addition & 0 deletions lib/dirty/dirty.js
Expand Up @@ -25,6 +25,7 @@ var Dirty = exports.Dirty = function(path) {
this._fdWrite = null;

this._load();
return this;
};

util.inherits(Dirty, EventEmitter);
Expand Down
19 changes: 19 additions & 0 deletions test/test-system.js
Expand Up @@ -97,3 +97,22 @@ describe('test-size', function() {
assert.equal(db.size(), 3);
});
});

describe('test-chaining-of-constructor', function() {
var file = config.TMP_PATH + '/chain.dirty';
fs.existsSync(file) && fs.unlinkSync(file);

it('should allow .on load to chain to constructor', function() {
var db = dirty(file);
db.on('load', function() {
db.set("x", "y");
db.set("p", "q");
db.close();

db = dirty(file).on('load', function(size) {
assert.strictEqual(db.size(), 2);
assert.strictEqual(size, 2);
});
});
});
});

0 comments on commit ac7b803

Please sign in to comment.