Add web worker support #1279

Merged
merged 7 commits into from Jul 29, 2014

Projects

None yet

4 participants

@akfish
Contributor
akfish commented Jul 28, 2014

Description

This PL made some necessary changes that allows developers to import sea.js from a web work and then use it to load CMD modules.
Since web workers have no access to DOM object, implementation before this PL won't be able to run in a web worker context.

Change List

  • Determine and set environment flags isBrowser and isWebWorker
  • Wrap browser specific operations inside if (isBrowser) block
  • Load scripts with importScripts when running in a web worker
  • Add test spec/web-worker

Status

  • Passed all original tests (browser runner)
  • Passed spec/web-worker
  • Failed module/public-api/seajs.request (node runner)
  • Manually tested in Chrome 36, FireFox 31 and IE 11

Use Cases

Common modules can be required from both browser and worker:

// Common modules
// adder.js
define(function(require, exports, module) {
  module.exports = function (a, b) { return a + b; }
});

Load sea.js once at the beginning of a worker script:

// Worker script
// worker.js
// Import sea.js
importScripts("path/to/sea.js");

seajs.use('task');

Use sea.js to load other modules in worker task:

// task.js
define(function(require, exports, module) {
  // Set up message listener
  // ..
  // On some message
  // require in worker
  adder = require('./adder')
  console.log(adder(1, 2));
  // Send result to browser script
  // ...
});
<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <script>
    var worker = new Worker('worker.js');
    // Set up message listener
    // ...
    // Send tasks
    // ...
    // Receive results
  </script>
</body>
</html>
@akfish
Contributor
akfish commented Jul 28, 2014

Travis CI build failed in node environment on test module/public-api/seajs.request.

I notice that node.js support is implemented with plugin mechanism and doesn't depend on request method at all. I thought that plugins should only communicate with core sea.js by events and seajs.request should not be considered as public API.

While we are on this topic, I notice that some plugins overrides seajs.request directly(seajs-css for example), instead of using seajs.on("request", handler). This kind of implementation defeats the purpose of plugin mechanism.

@army8735
Member

是专门为webwork做的hack?

@akfish
Contributor
akfish commented Jul 29, 2014

@army8735 应该算是适配,让sea.js直接能用在web worker里。sea.js直接用到了DOM对象,不做修改跑worker里会出错,所以worker的支持不能用插件实现,只有放在核心里。
RequireJS的web worker支持也是类似实现:
http://requirejs.org/docs/api.html#webworker
https://github.com/jrburke/requirejs/blob/master/require.js#L1916

@army8735
Member

util-cs.js怎么反过来被合并到一起了,提炼出来没包含进runtime版本的?

@akfish
Contributor
akfish commented Jul 29, 2014

util-cs.js里用到了util-request.js里的currentlyAddingScriptinteractiveScript变量,需要封到同一个if block里。

@army8735 army8735 merged commit 44622b3 into seajs:master Jul 29, 2014

1 check failed

continuous-integration/travis-ci The Travis CI build failed
Details
@army8735
Member

先合并慢慢看

@army8735
Member

es5里没有{}作用域,在if里var和外边一样的,提出utli-cs是为了减少runtime版本的体积

@akfish
Contributor
akfish commented Jul 29, 2014

看了下seatools的build task,了解了。
的确不用合并,worker环境下的执行路径也不会调用到getCurrentScript

@army8735
Member

可以if(webwork)else走普通流程

@akfish akfish added a commit to akfish/seajs that referenced this pull request Jul 29, 2014
@akfish akfish Revert util-cs.js #1279 fece5af
@army8735
Member

revert我已经master

@akfish akfish referenced this pull request Jul 29, 2014
Closed

Revert util-cs.js #1279 #1280

@akfish
Contributor
akfish commented Jul 29, 2014

OK.

@army8735
Member

???

@army8735
Member

typeof importScripts !== 'undefined'
应该是判断isFunction更好吧

@akfish
Contributor
akfish commented Jul 29, 2014

@hotoo 我用的就是git-flow,建的feature branch。@army8735建议考虑采用这种分支模型,不用每次PL都merge进master再review再改。

@army8735
Member

因为主要就我一个改,所以分不分都差不多……

@akfish
Contributor
akfish commented Jul 29, 2014

@army8735 git flow也适合单人用,新增功能,发布版本都很好用。
master分支原则上应该是可用于生产的稳定版本,开发者有时会直接fork master来用,而不会去下release。试验性的、未完成功能都应该隔离到单独的分支。

@popomore
Member

如果需要 review 的话建议分支开发,尤其是对于 seajs 这种比较重要的项目—
Sent from Mailbox for iPhone

On Tue, Jul 29, 2014 at 10:21 PM, AKFish notifications@github.com wrote:

@army8735 git flow也适合单人用,新增功能,发布版本都很好用。

master分支原则上应该是可用于生产的稳定版本,开发者有时会直接fork master来用,而不会去下release。试验性的、未完成功能都应该隔离到单独的分支。

Reply to this email directly or view it on GitHub:
#1279 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment