worker.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. "no use strict";
  2. ;(function(window) {
  3. if (typeof window.window != "undefined" && window.document) {
  4. return;
  5. }
  6. window.console = function() {
  7. var msgs = Array.prototype.slice.call(arguments, 0);
  8. postMessage({type: "log", data: msgs});
  9. };
  10. window.console.error =
  11. window.console.warn =
  12. window.console.log =
  13. window.console.trace = window.console;
  14. window.window = window;
  15. window.ace = window;
  16. window.onerror = function(message, file, line, col, err) {
  17. console.error("Worker " + err.stack);
  18. };
  19. window.normalizeModule = function(parentId, moduleName) {
  20. // normalize plugin requires
  21. if (moduleName.indexOf("!") !== -1) {
  22. var chunks = moduleName.split("!");
  23. return window.normalizeModule(parentId, chunks[0]) + "!" + window.normalizeModule(parentId, chunks[1]);
  24. }
  25. // normalize relative requires
  26. if (moduleName.charAt(0) == ".") {
  27. var base = parentId.split("/").slice(0, -1).join("/");
  28. moduleName = (base ? base + "/" : "") + moduleName;
  29. while(moduleName.indexOf(".") !== -1 && previous != moduleName) {
  30. var previous = moduleName;
  31. moduleName = moduleName.replace(/^\.\//, "").replace(/\/\.\//, "/").replace(/[^\/]+\/\.\.\//, "");
  32. }
  33. }
  34. return moduleName;
  35. };
  36. window.require = function(parentId, id) {
  37. if (!id) {
  38. id = parentId
  39. parentId = null;
  40. }
  41. if (!id.charAt)
  42. throw new Error("worker.js require() accepts only (parentId, id) as arguments");
  43. id = window.normalizeModule(parentId, id);
  44. var module = window.require.modules[id];
  45. if (module) {
  46. if (!module.initialized) {
  47. module.initialized = true;
  48. module.exports = module.factory().exports;
  49. }
  50. return module.exports;
  51. }
  52. var chunks = id.split("/");
  53. if (!window.require.tlns)
  54. return console.log("unable to load " + id);
  55. chunks[0] = window.require.tlns[chunks[0]] || chunks[0];
  56. var path = chunks.join("/") + ".js";
  57. window.require.id = id;
  58. importScripts(path);
  59. return window.require(parentId, id);
  60. };
  61. window.require.modules = {};
  62. window.require.tlns = {};
  63. window.define = function(id, deps, factory) {
  64. if (arguments.length == 2) {
  65. factory = deps;
  66. if (typeof id != "string") {
  67. deps = id;
  68. id = window.require.id;
  69. }
  70. } else if (arguments.length == 1) {
  71. factory = id;
  72. deps = []
  73. id = window.require.id;
  74. }
  75. if (!deps.length)
  76. // If there is no dependencies, we inject 'require', 'exports' and
  77. // 'module' as dependencies, to provide CommonJS compatibility.
  78. deps = ['require', 'exports', 'module']
  79. if (id.indexOf("text!") === 0)
  80. return;
  81. var req = function(childId) {
  82. return window.require(id, childId);
  83. };
  84. window.require.modules[id] = {
  85. exports: {},
  86. factory: function() {
  87. var module = this;
  88. var returnExports = factory.apply(this, deps.map(function(dep) {
  89. switch(dep) {
  90. // Because 'require', 'exports' and 'module' aren't actual
  91. // dependencies, we must handle them seperately.
  92. case 'require': return req
  93. case 'exports': return module.exports
  94. case 'module': return module
  95. // But for all other dependencies, we can just go ahead and
  96. // require them.
  97. default: return req(dep)
  98. }
  99. }));
  100. if (returnExports)
  101. module.exports = returnExports;
  102. return module;
  103. }
  104. };
  105. };
  106. window.define.amd = {}
  107. window.initBaseUrls = function initBaseUrls(topLevelNamespaces) {
  108. require.tlns = topLevelNamespaces;
  109. }
  110. window.initSender = function initSender() {
  111. var EventEmitter = window.require("ace/lib/event_emitter").EventEmitter;
  112. var oop = window.require("ace/lib/oop");
  113. var Sender = function() {};
  114. (function() {
  115. oop.implement(this, EventEmitter);
  116. this.callback = function(data, callbackId) {
  117. postMessage({
  118. type: "call",
  119. id: callbackId,
  120. data: data
  121. });
  122. };
  123. this.emit = function(name, data) {
  124. postMessage({
  125. type: "event",
  126. name: name,
  127. data: data
  128. });
  129. };
  130. }).call(Sender.prototype);
  131. return new Sender();
  132. }
  133. window.main = null;
  134. window.sender = null;
  135. window.onmessage = function(e) {
  136. var msg = e.data;
  137. if (msg.command) {
  138. if (main[msg.command])
  139. main[msg.command].apply(main, msg.args);
  140. else
  141. throw new Error("Unknown command:" + msg.command);
  142. }
  143. else if (msg.init) {
  144. initBaseUrls(msg.tlns);
  145. require("ace/lib/es5-shim");
  146. sender = initSender();
  147. var clazz = require(msg.module)[msg.classname];
  148. main = new clazz(sender);
  149. }
  150. else if (msg.event && sender) {
  151. sender._emit(msg.event, msg.data);
  152. }
  153. };
  154. })(this);