billysometimes / rethinkdb

dart driver for rethinkDB
MIT License
37 stars 19 forks source link

Let Cursor implement Stream #17

Closed cgarciae closed 9 years ago

cgarciae commented 9 years ago

According to the docs

Streams are lists like arrays, but they’re loaded in a lazy fashion. Operations that return streams return a cursor.

they they tell you

JavaScript has no native iterator, but ReQL implements an each command similar to jQuery’s.

And your implementation a abides perfectly. But Dart is not JavaScript, we DO have streams so why not implement cursor as such? Instead it implements IterableBase (which seems resonable) but whenever I put it in a for loop like this

Cursor users = await r.table('usuarios').run(conn);

  for (Response user in users)
  {
    //What can I do with Response???
  }

I get a Response object that is useless (no exposed fields or methods). I take that methods like map, where, etc will also receive this type.

With Dart 1.9.1 and an implementing cursor as a Stream<Map> (for example), using async/await we could do things like

var users = await r.table('usuarios').run(conn);
await for (Map user in users)
{
  //Do something as each user is retrieved async 
}

or even more sophisticated stuff using async *.

billysometimes commented 9 years ago

I actually really like this idea, it is the dart way to do things and would really simplify the cursor class.

for example, instead of the toArray() method they toList() method is built right into streams, and really every other cursor method as well.

billysometimes commented 9 years ago

https://github.com/billysometimes/rethinkdb/tree/cursor-stream/