Loading ...
Sorry, an error occurred while loading the content.

YUI3 and webworkers

Expand Messages
  • Mark Jarecki
    Hi all, I m looking into implementing an app with web workers and YUI3, however, am wondering how to give the worker access to the YUI3 library. My current
    Message 1 of 5 , Dec 15, 2009
    • 0 Attachment

      Hi all,

      I'm looking into implementing an app with web workers and YUI3, however, am wondering how to give the worker access to the YUI3 library. 

      My current method works, but is obviously missing a means of importing YUI3. I've tried importScripts() to no avail.

      If anyone has a quick example or suggestions on how to do this, that would be greatly appreciated.

      Thanks,

      Mark

      HTML
      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta http-equiv=" content-type" content="text/ html;charset= utf-8" />
        <script type="text/javascri pt" src="path/to/ yui/build/ yui/yui-min. js"></script>
      </head>
      <body>
      <script type="text/javascri pt">
      YUI({

      base: 'path/to/yui/ build/',
      combine: false,
      modules: {
      worker: {fullpath:'path/ to/worker. js'},
      view: {fullpath:'path/ to/js/view. js', requires: ['dd-constrain' ]},
      manager: {fullpath:'path/ to/manager. js', requires: ['base', 'view', 'worker']}
      },
      timeout: 10000

      }).use('manager' , function(Y) {

      Y.on("domready" , function(){ 

      var ManagerPages = Y.Base.build( "managerPages" , Y.Manager, [Y.ManagerPages] );
      var mp = new ManagerPages( );
      }, Y);

      });
      </script>
      </body>
      </html>

      manager.js
      YUI.add("manager" , function(Y) {

          var Manager,
          MANAGER = 'manager',
          $D = Y.DOM,
          $L = Y.Lang,
          $J = Y.JSON,
          isBoolean = $L.isBoolean;

         Manager = function(config) { Manager.superclass. constructor. apply(this, arguments); };

      Y.mix(Manager, {
      NAME: MANAGER,
      ATTRS: {

      someATTR: {
      value: false,
      validator: isBoolean
      }
      }        
      });

      Y.extend(Manager, Y.Base, {
      _worker: null,
      initializer: function(config) {

      //initialise exchange worker
      try {

      this. _worker = new Worker('http: //127.0.0. 1/static/ js/exchangeworke r.js');
      this. _worker.onmessage = function(e) {
      if (e.data == "nay") {
      alert("the worker responded");
      };
      }
      this. _worker.postMessage ("yay");

      } catch(e) {

      alert('Unable to create worker: ' + e.message);
      return;
      }
      }

      });

      Y.Manager = Manager;

      }, "3.0.0");


      worker.js
      onmessage = function(e){
      postMessage( "nay");
      };
    Your message has been successfully submitted and would be delivered to recipients shortly.