file-event-handler.js 2.5 KB

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