From cd07ec841d1facf3f4832e4f11200b7a0970be8d Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 4 Feb 2013 11:41:51 -0800 Subject: [PATCH] Add more window globals and shims in task shell This allows jQuery to be successfully loaded from a web worker since it does not have a document or window object by default. Previously it would log about missing methods and properties. Closes #228 --- spec/fixtures/jquery-task-handler.coffee | 4 ++++ spec/stdlib/task-shell-spec.coffee | 23 +++++++++++++++++++++++ src/stdlib/task-shell.coffee | 20 +++++++++++++++++--- src/stdlib/task.coffee | 5 ++++- 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 spec/fixtures/jquery-task-handler.coffee create mode 100644 spec/stdlib/task-shell-spec.coffee diff --git a/spec/fixtures/jquery-task-handler.coffee b/spec/fixtures/jquery-task-handler.coffee new file mode 100644 index 000000000..dbd91b107 --- /dev/null +++ b/spec/fixtures/jquery-task-handler.coffee @@ -0,0 +1,4 @@ +module.exports = + load: -> + $ = require 'jquery' + callTaskMethod('loaded', $?) diff --git a/spec/stdlib/task-shell-spec.coffee b/spec/stdlib/task-shell-spec.coffee new file mode 100644 index 000000000..d1ccb8fc0 --- /dev/null +++ b/spec/stdlib/task-shell-spec.coffee @@ -0,0 +1,23 @@ +Task = require 'task' + +describe "Task shell", -> + describe "populating the window with fake properties", -> + describe "when jQuery is loaded in a web worker", -> + it "doesn't log to the console", -> + spyOn(console, 'log') + spyOn(console, 'error') + spyOn(console, 'warn') + class JQueryTask extends Task + constructor: -> super('fixtures/jquery-task-handler.coffee') + started: -> @callWorkerMethod('load') + loaded: (@jqueryLoaded) -> + + task = new JQueryTask() + task.start() + waitsFor "web worker to start and jquery to be required", 5000, -> + task.jqueryLoaded + runs -> + expect(task.jqueryLoaded).toBeTruthy() + expect(console.log).not.toHaveBeenCalled() + expect(console.error).not.toHaveBeenCalled() + expect(console.warn).not.toHaveBeenCalled() diff --git a/src/stdlib/task-shell.coffee b/src/stdlib/task-shell.coffee index 39bb58c0e..458235e56 100644 --- a/src/stdlib/task-shell.coffee +++ b/src/stdlib/task-shell.coffee @@ -12,6 +12,19 @@ self.console = log: -> callTaskMethod 'log', arguments... error: -> callTaskMethod 'error', arguments... +window.document = + createElement: -> + setAttribute: -> + getElementsByTagName: -> [] + appendChild: -> + documentElement: + insertBefore: -> + removeChild: -> + getElementById: -> {} + createComment: -> {} + createDocumentFragment: -> {} +self.document = window.document + # `callTaskMethod` can be used to invoke method's on the parent `Task` object # back in the window thread. self.callTaskMethod = (method, args...) -> @@ -19,9 +32,10 @@ self.callTaskMethod = (method, args...) -> # The worker's initial handler replaces itself when `start` is invoked self.handler = - start: ({resourcePath, requirePath, handlerPath}) -> - self.resourcePath = resourcePath - window.resourcePath = resourcePath + start: ({resourcePath, globals, requirePath, handlerPath}) -> + for key, value of globals + self[key] = value + window[key] = value importScripts(requirePath) require 'config' self.handler = require(handlerPath) diff --git a/src/stdlib/task.coffee b/src/stdlib/task.coffee index 463c806bb..4a66a7042 100644 --- a/src/stdlib/task.coffee +++ b/src/stdlib/task.coffee @@ -17,7 +17,10 @@ class Task startWorker: -> @callWorkerMethod 'start' - resourcePath: window.resourcePath + globals: + resourcePath: window.resourcePath + navigator: + userAgent: navigator.userAgent requirePath: require.getPath('require') handlerPath: @path