Skip to content

Commit

Permalink
edited FAQ and fixed some obsolete comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Jan 16, 2014
1 parent 1b3a3bc commit f712e39
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
16 changes: 10 additions & 6 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,26 @@ And read just below about dealing with events!

### The underscore trick is designed for callbacks but not events. How do I deal with events?

If you are dealing with stream events, you should try streamline's stream API. It wraps node streams with a simple callback oriented API and it takes care of the low level event handling for you (`pause/resume` on readable streams, `drain` on writable streams). For example:
If you are dealing with stream events, you should try the ez-streams package. It wraps node streams with a simple callback oriented API and it takes care of the low level event handling for you (`pause/resume` on readable streams, `drain` on writable streams). For example:

``` javascript
var streams = require('streamline/lib/streams');
var ez = require('ez-streams');

var inStream = new streams.ReadableStream(nodeInStream);
var inStream = ez.devices.node.reader(nodeInStream);
var head = inStream.read(_, 128); // read the first 128 bytes
var chunk;
while (chunk = inStream.read(_)) {
// do something with chunk
}

var outStream = new streams.WritableStream(nodeOutStream);
var outStream = ez.devices.node.writer(nodeOutStream);
outStream.write(_, result);
```

This module also contains wrappers around node's `Http` and `Net` objects, both client and server.
See the `streams` documentation for details.
See the [ez-streams](https://github.com/Sage/ez-streams) documentation for details.

If you are not dealing with stream events, you can take a look at the implementation of the streams module for ideas. Any event API can be turned into a callback API (with a `getEvent(_)` call that you would call in a loop) but this can be totally counterprodutive (events will be serialized).
If you are not dealing with stream events, you can take a look at the implementation of the streamline-streams module (ez-streams' low level implementation module) for ideas. Any event API can be turned into a callback API (with a `getEvent(_)` call that you would call in a loop) but this can be totally counterprodutive (events will be serialized).

If the events are loosely correlated, it is better to let them be dispatched as events. But in this case, you may want to use streamline to handle the logic of each event you subscribed to. This is not too difficult: just put a small anonymous function wrapper inside your event handlers:

Expand All @@ -242,6 +242,10 @@ server.on('eventB', function(arg) {
});
```

### How can I deal with node.js streams

The easiest way is to use the [ez-streams](https://github.com/Sage/ez-streams) companion package.

### Are there limitations? Am I limited to a subset of Javascript?

Hardly any.
Expand Down
8 changes: 4 additions & 4 deletions examples/streams/googleClient._js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
*/
"use strict";

// require the streamline streams wrapper
var streams = require('streamline/lib/streams/server/streams');
// require the ez-streams package (you must install it with npm install ez-streams)
var ez = require('ez-streams');

function google(str, _) {
// Create the request.
// The options are the same as for node's http.request call.
// But the call also accepts a simple URL for the GET case
// But streams.requestRequest does not take any callback parameter.
// But ez.devices.http.client does not take any callback parameter.
// Instead, the callback is passed to the response(_) method (a few lines below).
var req = streams.httpRequest({
var req = ez.devices.http.client({
url: 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' + str,
proxy: process.env.http_proxy
});
Expand Down
2 changes: 2 additions & 0 deletions lib/streams/readers._js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var streams = require("./streams");
/// Readers module
/// The `readers` module contains higher level readers built on top of pull-mode streams.
///
/// This module is deprecated. Use x[ez-streams](https://github.com/Sage/ez-streams) instead.
///
exports.Reader = function(stream, boundary, options) {
options = options || {};
options.defaultSize = options.defaultSize || 512;
Expand Down

0 comments on commit f712e39

Please sign in to comment.