Skip to content

Commit

Permalink
perf: support flexible buffers for file-watchers
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Dec 22, 2017
1 parent 756c49e commit fd3d074
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/default-config.js
Expand Up @@ -367,7 +367,7 @@ module.exports = {
* @default 0
* @since 2.6.0
*/
reloadDebounce: 0,
reloadDebounce: 500,

/**
* Emit only the first event during sequential time windows
Expand Down
59 changes: 17 additions & 42 deletions lib/file-event-handler.js
Expand Up @@ -7,7 +7,7 @@ var utils = require("./utils");
* @return {Rx.Observable<{type: string, files: Array<any>}>}
*/
function fileChanges(subject, options) {
var operators = [
const operators = [
{
option: "reloadThrottle",
fnName: "throttle"
Expand All @@ -18,40 +18,30 @@ function fileChanges(subject, options) {
}
];

var scheduler = options.getIn(["debug", "scheduler"]);
const scheduler = options.getIn(["debug", "scheduler"]);

/**
* if the 'reloadDebounce' option was provided, create
* a stream buffered/debounced stream of events
* Create a stream buffered/debounced stream of events
*/
var initial = (function() {
if (options.get("reloadDebounce") > 0) {
return getAggregatedDebouncedStream(subject, options, scheduler);
}
return subject;
})();
const initial = getAggregatedDebouncedStream(subject, options, scheduler);

return applyOperators(operators, initial, options, scheduler).map(function(
xs
) {
var items = [].concat(xs);
var paths = items.map(function(x) {
return x.path;
});
return applyOperators(operators, initial, options, scheduler)
.map(function(items) {
const paths = items.map(x => x.path);

if (
utils.willCauseReload(paths, options.get("injectFileTypes").toJS())
) {
if (
utils.willCauseReload(paths, options.get("injectFileTypes").toJS())
) {
return {
type: "reload",
files: items
};
}
return {
type: "reload",
type: "inject",
files: items
};
}
return {
type: "inject",
files: items
};
});
})
}
module.exports.fileChanges = fileChanges;

Expand Down Expand Up @@ -113,19 +103,4 @@ function getAggregatedDebouncedStream(subject, options, scheduler) {
return options.get("watchEvents").indexOf(x.event) > -1;
})
.buffer(subject.debounce(options.get("reloadDebounce"), scheduler))
.map(function(buffered) {
return buffered.reduce(function(acc, item) {
if (!acc[item.path]) acc[item.path] = item;
if (acc[item.path]) acc[item.path] = item;
return acc;
}, {});
})
.map(function(group) {
return Object.keys(group).map(function(key) {
return group[key];
});
})
.filter(function(x) {
return x.length;
});
}

0 comments on commit fd3d074

Please sign in to comment.