This commit is contained in:
nightwing 2014-01-16 19:35:48 +04:00
commit 9e71e4a003

View file

@ -35,7 +35,7 @@ var oop = require("../lib/oop");
var EventEmitter = require("../lib/event_emitter").EventEmitter; var EventEmitter = require("../lib/event_emitter").EventEmitter;
var config = require("../config"); var config = require("../config");
var WorkerClient = function(topLevelNamespaces, mod, classname) { var WorkerClient = function(topLevelNamespaces, mod, classname, workerUrl) {
this.$sendDeltaQueue = this.$sendDeltaQueue.bind(this); this.$sendDeltaQueue = this.$sendDeltaQueue.bind(this);
this.changeListener = this.changeListener.bind(this); this.changeListener = this.changeListener.bind(this);
this.onMessage = this.onMessage.bind(this); this.onMessage = this.onMessage.bind(this);
@ -44,12 +44,11 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
if (require.nameToUrl && !require.toUrl) if (require.nameToUrl && !require.toUrl)
require.toUrl = require.nameToUrl; require.toUrl = require.nameToUrl;
var workerUrl;
if (config.get("packaged") || !require.toUrl) { if (config.get("packaged") || !require.toUrl) {
workerUrl = config.moduleUrl(mod, "worker"); workerUrl = workerUrl || config.moduleUrl(mod, "worker");
} else { } else {
var normalizePath = this.$normalizePath; var normalizePath = this.$normalizePath;
workerUrl = normalizePath(require.toUrl("ace/worker/worker.js", null, "_")); workerUrl = workerUrl || normalizePath(require.toUrl("ace/worker/worker.js", null, "_"));
var tlns = {}; var tlns = {};
topLevelNamespaces.forEach(function(ns) { topLevelNamespaces.forEach(function(ns) {
@ -59,12 +58,12 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
try { try {
this.$worker = new Worker(workerUrl); this.$worker = new Worker(workerUrl);
} catch ( e ) { } catch(e) {
if ( e instanceof DOMException ) { if (e instanceof window.DOMException) {
// Likely same origin problem. Use importScripts from a shim Worker // Likely same origin problem. Use importScripts from a shim Worker
var blob = this.blobWorker(workerUrl), var blob = this.$workerBlob(workerUrl);
URL = URL || webkitURL, var URL = window.URL || window.webkitURL;
blobURL = URL.createObjectURL(blob); var blobURL = URL.createObjectURL(blob);
this.$worker = new Worker(blobURL); this.$worker = new Worker(blobURL);
URL.revokeObjectURL(blobURL); URL.revokeObjectURL(blobURL);
@ -74,9 +73,9 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
} }
this.$worker.postMessage({ this.$worker.postMessage({
init : true, init : true,
tlns: tlns, tlns : tlns,
module: mod, module : mod,
classname: classname classname : classname
}); });
this.callbackId = 1; this.callbackId = 1;
@ -179,17 +178,13 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
this.emit("change", {data: q}); this.emit("change", {data: q});
}; };
this.blobWorker = function(workerUrl) { this.$workerBlob = function(workerUrl) {
var script = 'importScripts("' + workerUrl + '");', var script = 'importScripts("' + workerUrl + '");';
blob;
try { try {
blob = new Blob( var blob = new Blob([script], {'type': 'application/javascript'});
[script],
{ 'type' : 'application/javascript' }
);
} catch (e) { // Backwards-compatibility } catch (e) { // Backwards-compatibility
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder, var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
blobBuilder = new BlobBuilder(); var blobBuilder = new BlobBuilder();
blobBuilder.append(script); blobBuilder.append(script);
blob = blobBuilder.getBlob('application/javascript'); blob = blobBuilder.getBlob('application/javascript');
} }