-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stream): Implement dedicated
.stream()
method for better handl…
…ing streams & to pave the way for new stream-related features
- Loading branch information
1 parent
f5f3d65
commit 2581e7a
Showing
6 changed files
with
118 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"use strict"; | ||
|
||
var path = require("path"); | ||
|
||
/** | ||
* @param emitter | ||
* @returns {Function} | ||
*/ | ||
module.exports = function (emitter) { | ||
|
||
function emitReload(path, log) { | ||
emitter.emit("file:changed", { | ||
path: path, | ||
log: log, | ||
namespace: "core" | ||
}); | ||
} | ||
|
||
function emitBrowserReload() { | ||
emitter.emit("browser:reload"); | ||
} | ||
|
||
function emitInfo(changed) { | ||
emitter.emit("stream:changed", {changed: changed}); | ||
} | ||
|
||
return function (opts) { | ||
|
||
opts = opts || {}; | ||
var emitted = false; | ||
var Transform = require("stream").Transform; | ||
var reload = new Transform({objectMode: true}); | ||
var changed = []; | ||
|
||
reload._transform = function (file, encoding, next) { | ||
|
||
if (opts.once === true && !emitted) { | ||
|
||
emitBrowserReload(); | ||
|
||
emitted = true; | ||
|
||
} else { // handle multiple | ||
|
||
if (opts.once === true && emitted) { | ||
|
||
} else { | ||
|
||
if (file.path) { | ||
|
||
emitted = true; | ||
emitReload(file.path, false); | ||
changed.push(path.basename(file.path)); | ||
} | ||
} | ||
} | ||
|
||
this.push(file); // always send the file down-stream | ||
|
||
next(); | ||
}; | ||
|
||
reload._flush = function (next) { | ||
|
||
if (changed.length) { | ||
emitInfo(changed); | ||
} | ||
|
||
next(); | ||
}; | ||
|
||
return reload; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters