Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async iterator interface to cursors #371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Promise = require('bluebird');
var Err = require(__dirname+'/error.js');
var helper = require(__dirname+'/helper.js');
var EventEmitter = require('events').EventEmitter;
var $$asyncIterator = require('iterall').$$asyncIterator;

var MAX_CALL_STACK = 1000;

Expand Down Expand Up @@ -366,6 +367,37 @@ Cursor.prototype._eachCb = function(err, data) {
}
}

Cursor.prototype.asyncIterator = function() {
var self = this;
return {
next: function() {
var iter = this;
return self._next().then(value => {
return {
done: false,
value: value
};
}).error(function(error) {
if ((error.message !== 'You cannot retrieve data from a cursor that is closed.') &&
(error.message.match(/You cannot call `next` on a closed/) === null)) {
return iter.throw(error);
} else {
return iter.return();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mxstbr What other errors might occur? why stop the iterations for only the above errors?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I copied that from the other places where self._next() is called. Not sure why it's that specific, honestly!

Copy link
Contributor

@sagivf sagivf Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it makes sense, since any other errors you might want to ignore where as these don't make sense to continue.

This does raise a question though. With callbacks you get the error and can ignore or handle it.
In this case you wont get it. maybe it should be a part of the return iter.return(err).

This also makes me really wonder what errors are possible... I'll ask around.

}
});
},
return: function() {
return Promise.resolve({ value: undefined, done: true });
},
throw: function(err) {
return Promise.reject(err);
},
[$$asyncIterator]: function() {
return this;
}

}}

var methods = [
'addListener',
'on',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"url": "https://github.com/neumino/rethinkdbdash/issues"
},
"dependencies": {
"bluebird": ">= 3.0.1"
"bluebird": ">= 3.0.1",
"iterall": "^1.2.2"
},
"devDependencies": {
"mocha": ">= 1.20.0",
Expand Down
33 changes: 32 additions & 1 deletion test/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var config = require('./config.js');
var r = require('../lib')(config);
var util = require(__dirname+'/util/common.js');
var assert = require('assert');
var iterall = require('iterall');

var uuid = util.uuid;
var It = util.It
Expand Down Expand Up @@ -841,5 +842,35 @@ It('`eachAync` should return an error if the connection dies', function* (done)
// Kill the TCP connection
connection.connection.end()
})
It('`asyncIterator` should return an async iterator', function* (done) {
try {
var connection = yield r.connect({host: config.host, port: config.port, authKey: config.authKey});
assert(connection);


var feed = yield r.db(dbName).table(tableName).changes().run(connection);
var iterator = feed.asyncIterator();
assert(iterall.isAsyncIterable(iterator))
connection.connection.end()
done();
} catch(err) {
done(err);
}
})
It('`asyncIterator` should have a working `next`method', function* (done) {
try {
feed = yield r.db(dbName).table(tableName2).changes().run();
var value = 1;
setTimeout(function() {
r.db(dbName).table(tableName2).update({foo: value}).run();
}, 100)
assert(feed);
var iterator = feed.asyncIterator();
assert(iterator);
var i=0;
var result = yield iterator.next();
assert(result.value.new_val.foo === value);
done();
} catch(err) {
done(err);
}
})