W3C home > Mailing lists > Public > public-html-commits@w3.org > August 2008

html5/workers Overview.html,1.30,1.31

From: Ian Hickson via cvs-syncmail <cvsmail@w3.org>
Date: Wed, 06 Aug 2008 10:08:57 +0000
To: public-html-commits@w3.org
Message-Id: <E1KQfwv-0006nN-Lt@lionel-hutz.w3.org>

Update of /sources/public/html5/workers
In directory hutz:/tmp/cvs-serv26098

Modified Files:
	Overview.html 
Log Message:
Another example. (whatwg r38)

Index: Overview.html
===================================================================
RCS file: /sources/public/html5/workers/Overview.html,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- Overview.html	6 Aug 2008 09:43:07 -0000	1.30
+++ Overview.html	6 Aug 2008 10:08:55 -0000	1.31
@@ -185,6 +185,9 @@
       <ul class=toc>
        <li><a href="#a-background"><span class=secno>1.1.1 </span>A
         background number-crunching thread</a>
+
+       <li><a href="#a-worker"><span class=secno>1.1.2 </span>A worker for
+        updating a client-side database</a>
       </ul>
 
      <li><a href="#conformance"><span class=secno>1.2 </span>Conformance
@@ -307,6 +310,52 @@
   <p><a href="http://www.whatwg.org/demos/workers/primes/page.html">View this
    example online</a>.
 
+  <h4 id=a-worker><span class=secno>1.1.2 </span>A worker for updating a
+   client-side database</h4>
+
+  <p><em>This section is non-normative.</em>
+
+  <p>In this example, the main document spawns a worker whose only task is to
+   listen for notifications from the server, and, when appropriate, either
+   add or remove data from the client-side database.
+
+  <p>Since no communication occurs between the worker and the main page, the
+   main page can start the worker by just doing:
+
+  <pre>&lt;script>
+ createWorker('worker.js');
+&lt;/script></pre>
+
+  <p>The worker itself is as follows:
+
+  <pre>var server = new WebSocket('ws://whatwg.org/database');
+var database = utils.openDatabase('demobase', '1.0', 'Demo Database', 10240);
+server.onmessage = function (event) {
+  // data is in the format "command key value"
+  var data = event.message.split(' ');
+  switch (data[0]) {
+    case '+':
+     database.transaction(function(tx) {
+       tx.executeSql('INSERT INTO pairs (key, value) VALUES (?, ?)', data[1], data[2]);
+     });
+    case '-':
+     database.transaction(function(tx) {
+       tx.executeSql('DELETE FROM pairs WHERE key=? AND value=?', data[1], data[2]);
+     });
+  }
+};</pre>
+
+  <p>This connects to the server using the <code>WebSocket</code> mechanism
+   and opens the local database (which, we presume, has been created
+   earlier). The worker then just listens for messages from the worker and
+   acts on them as appropriate, forever (or until the main page is closed).
+
+  <p><a
+   href="http://www.whatwg.org/demos/workers/database-updater/page.html">View
+   this example online</a>. (This example will not actually function, since
+   the server does not actually exist and the database is not created by this
+   sample code.)
+
   <h3 id=conformance><span class=secno>1.2 </span>Conformance requirements</h3>
 
   <p>All diagrams, examples, and notes in this specification are
Received on Wednesday, 6 August 2008 10:09:31 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Thursday, 9 October 2008 20:32:58 GMT