cujojs / curl

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
https://github.com/cujojs/curl/wiki
Other
1.89k stars 216 forks source link

WIP: WebWorker support #122

Closed alanpearce closed 11 years ago

alanpearce commented 11 years ago

I created a plugin to load a module as a worker, and modified curl.js to work from within a worker.

There isn't any detection for worker support right now, which is the biggest missing feature for me.

There are a few ways I thought of to do this:

Currently I've done the bare minimum to get it to work, suggestions for improvement are certainly welcome.

alanpearce commented 11 years ago

Now curl works from inside a worker. define doesn't, yet. Given the use of the worker plugin I think it would make more sense to use define instead, although it might be difficult, given that curl's define doesn't work outside of the curl context anyway.

alanpearce commented 11 years ago

curl.baseUrl from inside a worker is relative to the worker's directory. This might surprise people when using curl to spawn a worker in another directory.

The workaround I'm using right now is to set baseUrl to '/', since it was previously unset. I'm not sure if something should be done about this, or just have it documented somewhere so that people are aware of it.

unscriptable commented 11 years ago

wow! This is a great start.

I'm thinking that the web-worker support in loadScript could be injected via a shim. Does that make sense? Then again, this is such a small amount of code. Hm.

importScripts is sync? That seems lazy of google. :P

alanpearce commented 11 years ago

I'm not quite sure I understand what you're referring to with the shim idea. Do you mean something like adding a def.worker for loadScript to check for? If that's what you're after, where would you suggest putting it? createResouceDef? fetchResDef?

As for importScripts being synchronous, that did surprise me. From reading the draft spec, it sounds as if browsers will fetch the scripts asynchronously but execute in order. I'm not sure how easy it would be for us to take advantage of this though.

unscriptable commented 11 years ago

I meant like the ssjs shim: https://github.com/cujojs/curl/blob/master/src/curl/shim/ssjs.js#L66

alanpearce commented 11 years ago

Ah, I see. That does look like a better approach. I'll look into it.

alanpearce commented 11 years ago

It wouldn't make sense to put a shim in a different file as curl wouldn't be able to load the shim file without the shim.

However, I could define one of two definitions of loadScript in curl.js based upon a similar predicate. That way, it would only be tested once per page load and once per worker and still be more modular than it is now.

unscriptable commented 11 years ago

I was thinking like this: https://github.com/cujojs/curl/blob/master/dist/curl-for-ssjs/curl.js

The ssjs shim is pre-bundled into that version of curl.js. Does that make sense? What do you think?

alanpearce commented 11 years ago

Aha. Now I understand. I'll give it a try this weekend and add a make rule for it like the ssjs one.

unscriptable commented 11 years ago

cool!

alanpearce commented 11 years ago

The compiled version seems to fail when built with with ADVANCED_OPTIMIZATIONS at the first line of the original loadScripts function. It seems to work fine when built with --NONE, so I'm not sure if I've done anything incorrectly or not.

asilvas commented 11 years ago

@alanpearce Hi Alan, unrelated to curl, I've found ADVANCED_OPTIMIZATIONS will generally be an issue unless you compile all code into a single JS. It heavily renames everything for optimizations, so unless it knows about all references, it cannot safely work.

alanpearce commented 11 years ago

@asilvas Thanks for the info. There error is caused by a reference to window.document, as neither window.document nor window are defined. Workers have a different global scope to scripts running inside a webpage, see the spec.

It wouldn't make sense to compile the code for window.document into curl.

alanpearce commented 11 years ago

@unscriptable Would you like me to rebase this against dev as well? I was planning to rebase it anyway when complete to remove the unnecessary commits.

unscriptable commented 11 years ago

Yes, please rebase on dev. Thx!

alanpearce commented 11 years ago

Thanks for the hints!

alanpearce commented 11 years ago

Closing in favour of #130

alanpearce commented 11 years ago

I think I broke it.