|
| 1 | +var utils = require("./utils"); |
| 2 | + |
| 3 | +/** |
| 4 | + * Apply the operators that apply to the 'file:changed' event |
| 5 | + * @param {Rx.Observable} subject |
| 6 | + * @param options |
| 7 | + * @return {Rx.Observable<{type: string, files: Array<any>}>} |
| 8 | + */ |
| 9 | +function fileChanges(subject, options) { |
| 10 | + var operators = [ |
| 11 | + { |
| 12 | + option: "reloadThrottle", |
| 13 | + fnName: "throttle" |
| 14 | + }, |
| 15 | + { |
| 16 | + option: "reloadDelay", |
| 17 | + fnName: "delay" |
| 18 | + } |
| 19 | + ]; |
| 20 | + |
| 21 | + var scheduler = options.getIn(["debug", "scheduler"]); |
| 22 | + |
| 23 | + /** |
| 24 | + * if the 'reloadDebounce' option was provided, create |
| 25 | + * a stream buffered/debounced stream of events |
| 26 | + */ |
| 27 | + var initial = (function() { |
| 28 | + if (options.get("reloadDebounce") > 0) { |
| 29 | + return getAggregatedDebouncedStream(subject, options, scheduler); |
| 30 | + } |
| 31 | + return subject; |
| 32 | + })(); |
| 33 | + |
| 34 | + return applyOperators(operators, initial, options, scheduler) |
| 35 | + .map(function(xs) { |
| 36 | + |
| 37 | + var items = [].concat(xs); |
| 38 | + var paths = items.map(function (x) { return x.path }); |
| 39 | + |
| 40 | + if (utils.willCauseReload(paths, options.get("injectFileTypes").toJS())) { |
| 41 | + return { |
| 42 | + type: "reload", |
| 43 | + files: items |
| 44 | + } |
| 45 | + } |
| 46 | + return { |
| 47 | + type: "inject", |
| 48 | + files: items |
| 49 | + } |
| 50 | + }); |
| 51 | +} |
| 52 | +module.exports.fileChanges = fileChanges; |
| 53 | + |
| 54 | +/** |
| 55 | + * Apply the operators that apply to the 'browser:reload' event |
| 56 | + * @param {Rx.Observable} subject |
| 57 | + * @param options |
| 58 | + * @returns {Rx.Observable} |
| 59 | + */ |
| 60 | +function applyReloadOperators (subject, options) { |
| 61 | + var operators = [ |
| 62 | + { |
| 63 | + option: "reloadDebounce", |
| 64 | + fnName: "debounce" |
| 65 | + }, |
| 66 | + { |
| 67 | + option: "reloadThrottle", |
| 68 | + fnName: "throttle" |
| 69 | + }, |
| 70 | + { |
| 71 | + option: "reloadDelay", |
| 72 | + fnName: "delay" |
| 73 | + } |
| 74 | + ]; |
| 75 | + |
| 76 | + return applyOperators(operators, subject, options, options.getIn(["debug", "scheduler"])); |
| 77 | +} |
| 78 | +module.exports.applyReloadOperators = applyReloadOperators; |
| 79 | + |
| 80 | +/** |
| 81 | + * @param items |
| 82 | + * @param subject |
| 83 | + * @param options |
| 84 | + * @param scheduler |
| 85 | + */ |
| 86 | +function applyOperators (items, subject, options, scheduler) { |
| 87 | + return items.reduce(function(subject, item) { |
| 88 | + var value = options.get(item.option); |
| 89 | + if (value > 0) { |
| 90 | + return subject[item.fnName].call(subject, value, scheduler); |
| 91 | + } |
| 92 | + return subject; |
| 93 | + }, subject); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * @param subject |
| 98 | + * @param options |
| 99 | + * @param scheduler |
| 100 | + */ |
| 101 | +function getAggregatedDebouncedStream (subject, options, scheduler) { |
| 102 | + return subject |
| 103 | + .filter(function(x) { return options.get("watchEvents").indexOf(x.event) > -1 }) |
| 104 | + .buffer(subject.debounce(options.get("reloadDebounce"), scheduler)) |
| 105 | + .map(function(buffered) { |
| 106 | + return buffered.reduce(function (acc, item) { |
| 107 | + if (!acc[item.path]) acc[item.path] = item; |
| 108 | + if (acc[item.path]) acc[item.path] = item; |
| 109 | + return acc; |
| 110 | + }, {}); |
| 111 | + }) |
| 112 | + .map(function(group) { |
| 113 | + return Object |
| 114 | + .keys(group) |
| 115 | + .map(function(key) { |
| 116 | + return group[key]; |
| 117 | + }); |
| 118 | + }) |
| 119 | + .filter(function (x) { return x.length }) |
| 120 | +} |
0 commit comments