internal-events.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. var utils = require("./utils");
  3. var fileUtils = require("./file-utils");
  4. var Rx = require("rx");
  5. var fromEvent = Rx.Observable.fromEvent;
  6. var fileHandler = require("./file-event-handler");
  7. module.exports = function (bs) {
  8. var events = {
  9. /**
  10. * File reloads
  11. * @param data
  12. */
  13. "file:reload": function (data) {
  14. bs.io.sockets.emit("file:reload", data);
  15. },
  16. /**
  17. * Browser Reloads
  18. */
  19. "browser:reload": function () {
  20. bs.io.sockets.emit("browser:reload");
  21. },
  22. /**
  23. * Browser Notify
  24. * @param data
  25. */
  26. "browser:notify": function (data) {
  27. bs.io.sockets.emit("browser:notify", data);
  28. },
  29. /**
  30. * Things that happened after the service is running
  31. * @param data
  32. */
  33. "service:running": function (data) {
  34. var mode = bs.options.get("mode");
  35. var open = bs.options.get("open");
  36. if (mode === "proxy" ||
  37. mode === "server" ||
  38. open === "ui" ||
  39. open === "ui-external") {
  40. utils.openBrowser(data.url, bs.options, bs);
  41. }
  42. // log about any file watching
  43. if (bs.watchers) {
  44. bs.events.emit("file:watching", bs.watchers);
  45. }
  46. },
  47. /**
  48. * Option setting
  49. * @param data
  50. */
  51. "options:set": function (data) {
  52. if (bs.io) {
  53. bs.io.sockets.emit("options:set", data);
  54. }
  55. },
  56. /**
  57. * Plugin configuration setting
  58. * @param data
  59. */
  60. "plugins:configure": function (data) {
  61. if (data.active) {
  62. bs.pluginManager.enablePlugin(data.name);
  63. }
  64. else {
  65. bs.pluginManager.disablePlugin(data.name);
  66. }
  67. bs.setOption("userPlugins", bs.getUserPlugins());
  68. },
  69. "plugins:opts": function (data) {
  70. if (bs.pluginManager.pluginOptions[data.name]) {
  71. bs.pluginManager.pluginOptions[data.name] = data.opts;
  72. bs.setOption("userPlugins", bs.getUserPlugins());
  73. }
  74. }
  75. };
  76. Object.keys(events).forEach(function (event) {
  77. bs.events.on(event, events[event]);
  78. });
  79. var reloader = fileHandler
  80. .applyReloadOperators(fromEvent(bs.events, "_browser:reload"), bs.options)
  81. .subscribe(function () {
  82. bs.events.emit("browser:reload");
  83. });
  84. var coreNamespacedWatchers = fromEvent(bs.events, "file:changed")
  85. .filter(function () {
  86. return bs.options.get("codeSync");
  87. })
  88. .filter(function (x) {
  89. return x.namespace === "core";
  90. });
  91. var handler = fileHandler
  92. .fileChanges(coreNamespacedWatchers, bs.options)
  93. .subscribe(function (x) {
  94. if (x.type === "reload") {
  95. bs.events.emit("browser:reload", x);
  96. }
  97. if (x.type === "inject") {
  98. x.files.forEach(function (data) {
  99. if (!bs.paused && data.namespace === "core") {
  100. bs.events.emit("file:reload", fileUtils.getFileInfo(data, bs.options));
  101. }
  102. });
  103. }
  104. });
  105. bs.registerCleanupTask(function () {
  106. handler.dispose();
  107. reloader.dispose();
  108. });
  109. };
  110. //# sourceMappingURL=internal-events.js.map