Permalink
Browse files

added and documented on_pause/on_resume overridable methods

  • Loading branch information...
1 parent 7291d1d commit 2c4772c30f6aed9927a16d793d3f062046c1d01d @deoxxa committed Oct 21, 2012
Showing with 32 additions and 0 deletions.
  1. +20 −0 README.md
  2. +12 −0 index.js
View
@@ -25,6 +25,8 @@ Features
* Transparent `data` event queueing (by hijacking `emit()`) ([more](#buffering))
* Accurate `pause()`, `resume()`, `end()`, `destroySoon()` and `destroy()`
implementations out of the box
+* Ability to override certain behaviours on a per-implementation basis
+ ([more](#overridable-methods))
* Multi-target stream synchronisation (more on this [below](#multi-target-pipe))
Buffering
@@ -40,6 +42,24 @@ event.
paused, there's a chance you'll blow out your memory usage as events will be
indefinitely buffered.
+Overridable Methods
+-------------------
+
+Steez has a few methods you can implement to alter its behaviour; they are as
+follows:
+
+`can_destroy`: return a truthy value if the stream has no pending tasks and can
+be destroyed safely. This is useful if you perform any asynchronous tasks in
+your stream, as they will not count towards the stream's internal queue.
+
+`on_pause`: this is called (if it exists) when Steez goes from being not paused
+to paused. Note that this is not as simple as just being called every time
+`pause()` is called, due to the multi-target stream synchronisation.
+
+`on_resume`: this is called (if it exists) when Steez goes from being paused to
+not paused. This is the same as `on_pause` in that it's not just called whenever
+`resume()` is called, but rather when all streams have `resume()`ed the stream.
+
Multi-Target `pipe()`
---------------------
View
@@ -38,8 +38,14 @@ Steez.prototype.pause = function pause() {
};
Steez.prototype._pause = function _pause(pindex) {
+ var was_paused = this.paused !== 0;
+
this.paused |= pindex;
+ if (!was_paused && this.paused !== 0 && typeof this.on_pause === "function") {
+ this.on_pause();
+ }
+
return this;
};
@@ -48,6 +54,8 @@ Steez.prototype.resume = function resume() {
};
Steez.prototype._resume = function _resume(pindex) {
+ var was_paused = this.paused !== 0;
+
if (this.paused & pindex) {
this.paused ^= pindex;
}
@@ -56,6 +64,10 @@ Steez.prototype._resume = function _resume(pindex) {
return this;
}
+ if (was_paused && this.paused === 0 && typeof this.on_resume === "function") {
+ this.on_resume();
+ }
+
return this.flush();
};

0 comments on commit 2c4772c

Please sign in to comment.