watch-proxy.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. @license
  3. Rollup.js v3.18.0
  4. Wed, 01 Mar 2023 18:45:12 GMT - commit 25bdc129d21685b69a00ee55397d42ac6eff6449
  5. https://github.com/rollup/rollup
  6. Released under the MIT License.
  7. */
  8. 'use strict';
  9. const rollup = require('./rollup.js');
  10. const fseventsImporter = require('./fsevents-importer.js');
  11. class WatchEmitter {
  12. constructor() {
  13. this.currentHandlers = Object.create(null);
  14. this.persistentHandlers = Object.create(null);
  15. }
  16. // Will be overwritten by Rollup
  17. async close() { }
  18. emit(event, ...parameters) {
  19. return Promise.all([...this.getCurrentHandlers(event), ...this.getPersistentHandlers(event)].map(handler => handler(...parameters)));
  20. }
  21. off(event, listener) {
  22. const listeners = this.persistentHandlers[event];
  23. if (listeners) {
  24. // A hack stolen from "mitt": ">>> 0" does not change numbers >= 0, but -1
  25. // (which would remove the last array element if used unchanged) is turned
  26. // into max_int, which is outside the array and does not change anything.
  27. listeners.splice(listeners.indexOf(listener) >>> 0, 1);
  28. }
  29. return this;
  30. }
  31. on(event, listener) {
  32. this.getPersistentHandlers(event).push(listener);
  33. return this;
  34. }
  35. onCurrentRun(event, listener) {
  36. this.getCurrentHandlers(event).push(listener);
  37. return this;
  38. }
  39. once(event, listener) {
  40. const selfRemovingListener = (...parameters) => {
  41. this.off(event, selfRemovingListener);
  42. return listener(...parameters);
  43. };
  44. this.on(event, selfRemovingListener);
  45. return this;
  46. }
  47. removeAllListeners() {
  48. this.removeListenersForCurrentRun();
  49. this.persistentHandlers = Object.create(null);
  50. return this;
  51. }
  52. removeListenersForCurrentRun() {
  53. this.currentHandlers = Object.create(null);
  54. return this;
  55. }
  56. getCurrentHandlers(event) {
  57. return this.currentHandlers[event] || (this.currentHandlers[event] = []);
  58. }
  59. getPersistentHandlers(event) {
  60. return this.persistentHandlers[event] || (this.persistentHandlers[event] = []);
  61. }
  62. }
  63. function watch(configs) {
  64. const emitter = new WatchEmitter();
  65. watchInternal(configs, emitter).catch(error => {
  66. rollup.handleError(error);
  67. });
  68. return emitter;
  69. }
  70. async function watchInternal(configs, emitter) {
  71. const optionsList = await Promise.all(rollup.ensureArray(configs).map(config => rollup.mergeOptions(config)));
  72. const watchOptionsList = optionsList.filter(config => config.watch !== false);
  73. if (watchOptionsList.length === 0) {
  74. return rollup.error(rollup.errorInvalidOption('watch', rollup.URL_WATCH, 'there must be at least one config where "watch" is not set to "false"'));
  75. }
  76. await fseventsImporter.loadFsEvents();
  77. const { Watcher } = await Promise.resolve().then(() => require('./watch.js'));
  78. new Watcher(watchOptionsList, emitter);
  79. }
  80. exports.watch = watch;
  81. //# sourceMappingURL=watch-proxy.js.map