RequireJS和HTML5 WebWorkers

时间:2015-12-17 00:55:49

标签: javascript html5 reactjs web-worker

我有这个代码,令人惊讶的是,CDN链接与importScripts一起使用。但是,我更愿意在主UI线程上重用已由RequireJS加载的脚本。我可以使用importScripts导入RequireJS库,但我相信这将是RequireJS的一个新实例..无论如何都要以某种方式共享同一个实例?

     //I don't want to have to reimport these scripts:

      /*

      if(typeof importScripts === 'function'){
         importScripts('https://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react.js');
         importScripts('https://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react-dom.js');
      }

     */

       //...I would rather just use the existing RequireJS scripts

      if(typeof importScripts === 'function'){
         importScripts('/js/vendor/require.js');
       }

    //require is now defined, but React and ReactDOM aren't loaded

    require(['react','reactDOM'],function(React,ReactDOM){

        onmessage = function(msg) {

            var ContactItem = React.createFactory(React.createClass({

                propTypes: {
                    name: React.PropTypes.string.isRequired
                },

                render: function () {
                    return (
                        React.createElement('li', {className: 'Contact'},
                            React.createElement('h2', {className: 'Contact-name'}, this.props.name)
                        )
                    )
                }
            }));

            var str = React.renderToString(ContactItem({name:'charlie'}));
            postMessage(str);

        };

    });

总结以上内容 - 我可以将RequireJS导入到WebWorker中,但它不会与主UI线程上的RequireJS实例相同,所以我不能使用加载了RequireJS实例的脚本 - 那么是什么假设我的评估是正确的,这是最好的做法吗?或者是吗?

0 个答案:

没有答案