file-event-handler.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var utils = require("./utils");
  2. /**
  3. * Apply the operators that apply to the 'file:changed' event
  4. * @param {Rx.Observable} subject
  5. * @param options
  6. * @return {Rx.Observable<{type: string, files: Array<any>}>}
  7. */
  8. function fileChanges(subject, options) {
  9. const operators = [
  10. {
  11. option: "reloadThrottle",
  12. fnName: "throttle"
  13. },
  14. {
  15. option: "reloadDelay",
  16. fnName: "delay"
  17. }
  18. ];
  19. const scheduler = options.getIn(["debug", "scheduler"]);
  20. /**
  21. * Create a stream buffered/debounced stream of events
  22. */
  23. const initial = getAggregatedDebouncedStream(subject, options, scheduler);
  24. return applyOperators(operators, initial, options, scheduler).map(function (items) {
  25. const paths = items.map(x => x.path);
  26. if (utils.willCauseReload(paths, options.get("injectFileTypes").toJS())) {
  27. return {
  28. type: "reload",
  29. files: items
  30. };
  31. }
  32. return {
  33. type: "inject",
  34. files: items
  35. };
  36. });
  37. }
  38. module.exports.fileChanges = fileChanges;
  39. /**
  40. * Apply the operators that apply to the 'browser:reload' event
  41. * @param {Rx.Observable} subject
  42. * @param options
  43. * @returns {Rx.Observable}
  44. */
  45. function applyReloadOperators(subject, options) {
  46. var operators = [
  47. {
  48. option: "reloadDebounce",
  49. fnName: "debounce"
  50. },
  51. {
  52. option: "reloadThrottle",
  53. fnName: "throttle"
  54. },
  55. {
  56. option: "reloadDelay",
  57. fnName: "delay"
  58. }
  59. ];
  60. return applyOperators(operators, subject, options, options.getIn(["debug", "scheduler"]));
  61. }
  62. module.exports.applyReloadOperators = applyReloadOperators;
  63. /**
  64. * @param items
  65. * @param subject
  66. * @param options
  67. * @param scheduler
  68. */
  69. function applyOperators(items, subject, options, scheduler) {
  70. return items.reduce(function (subject, item) {
  71. var value = options.get(item.option);
  72. if (value > 0) {
  73. return subject[item.fnName].call(subject, value, scheduler);
  74. }
  75. return subject;
  76. }, subject);
  77. }
  78. /**
  79. * @param subject
  80. * @param options
  81. * @param scheduler
  82. */
  83. function getAggregatedDebouncedStream(subject, options, scheduler) {
  84. return subject
  85. .filter(function (x) {
  86. return options.get("watchEvents").indexOf(x.event) > -1;
  87. })
  88. .buffer(subject.debounce(options.get("reloadDebounce"), scheduler));
  89. }
  90. //# sourceMappingURL=file-event-handler.js.map