- From: poot <cvsmail@w3.org>
- Date: Wed, 6 Aug 2008 20:03:35 +0900 (JST)
- To: public-html-diffs@w3.org
New example. (whatwg r40)
1.1.2 A worker for updating a client-side database
http://people.w3.org/mike/diffs/html5/workers/Overview.1.33.html#a-worker
success steps
http://people.w3.org/mike/diffs/html5/workers/Overview.1.33.html#success
run a worker
http://people.w3.org/mike/diffs/html5/workers/Overview.1.33.html#run-a
1.1.3 Worker used for backgroud I/O
http://people.w3.org/mike/diffs/html5/workers/Overview.1.33.html#worker
worker creation failed
http://people.w3.org/mike/diffs/html5/workers/Overview.1.33.html#worker0
1.2 Conformance requirements
http://people.w3.org/mike/diffs/html5/workers/Overview.1.33.html#conformance
http://people.w3.org/mike/diffs/html5/workers/Overview.diff.html
http://dev.w3.org/cvsweb/html5/workers/Overview.html?r1=1.32&r2=1.33&f=h
http://html5.org/tools/web-apps-tracker?from=39&to=40
===================================================================
RCS file: /sources/public/html5/workers/Overview.html,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -d -r1.32 -r1.33
--- Overview.html 6 Aug 2008 10:09:24 -0000 1.32
+++ Overview.html 6 Aug 2008 11:02:33 -0000 1.33
@@ -188,6 +188,9 @@
<li><a href="#a-worker"><span class=secno>1.1.2 </span>A worker for
updating a client-side database</a>
+
+ <li><a href="#worker"><span class=secno>1.1.3 </span>Worker used for
+ backgroud I/O</a>
</ul>
<li><a href="#conformance"><span class=secno>1.2 </span>Conformance
@@ -356,6 +359,118 @@
the server does not actually exist and the database is not created by this
sample code.)
+ <h4 id=worker><span class=secno>1.1.3 </span>Worker used for backgroud I/O</h4>
+
+ <p><em>This section is non-normative.</em>
+
+ <p>In this example, the main document uses two workers, one for fetching
+ stock updates for at regular intervals, and one for fetching performing
+ search queries that the user requests.
+
+ <p>The main page is as follows:
+
+ <pre><!DOCTYPE HTML>
+<html>
+ <head>
+ <title>Worker example: Stock ticker</title>
+ <script>
+ // TICKER
+ var symbol = 'GOOG'; // default symbol to watch
+ var ticker = createWorker('ticker.js');
+ ticker.postMessage(symbol);
+
+ // SEARCHER
+ var searcher = createWorker('searcher.js');
+ function search(query) {
+ searcher.postMessage(query);
+ }
+
+ // SYMBOL SELECTION UI
+ function select(newSymbol) {
+ symbol = newSymbol;
+ ticker.postMessage(symbol);
+ }
+ </script>
+ </head>
+ <body>
+ <p><output id="symbol"></output> <output id="value"></output></p>
+ <script>
+ ticker.onmessage = function (event) {
+ var data = event.message.split(' ');
+ document.getElementById('symbol').textContent = data[0];
+ document.getElementById('value').textContent = data[1];
+ };
+ </script>
+ <p><label>Search: <input type="text" oninput="search(this.value)"></label></p>
+ <ul id="results"></ul>
+ <script>
+ searcher.onmessage = function (event) {
+ var data = event.message.split(' ');
+ var results = document.getElementById('results');
+ while (results.hasChildNodes()) // clear previous results
+ results.removeChild(results.firstChild);
+ for (var i = 0; i < data.length; i += 1) {
+ // add a list item with a button for each result
+ var li = document.createElement('li');
+ var button = document.createElement('button');
+ button.value = data[i];
+ button.type = 'button';
+ button.onclick = function () { select(this.value); };
+ button.textContent = data[i];
+ li.appendChild(button);
+ results.appendChild(li);
+ }
+ };
+ </script>
+ <p>(The data in this example is not real. Try searching for "Google" or "Apple".)</p>
+ </body>
+</html></pre>
+
+ <p>Messages are queued until the <code
+ title=handler-MessagePort-onmessage>onmessage</code> handler is set, so
+ even if it takes a while for the message handler to be attached, no data
+ is lost.
+
+ <p>The two workers use a common library for performing the actual network
+ calls. This library is as follows:
+
+ <pre>function get(url) {
+ try {
+ var xhr = new XMLHttpRequest();
+ xhr.open('GET', url, false);
+ xhr.send();
+ return xhr.responseText;
+ } except (e) {
+ return ''; // turn all errors into empty results
+ }
+}</pre>
+
+ <p>The stock updater worker is as follows:
+
+ <pre>importScript('io.js');
+var timer;
+var symbol;
+function update() {
+ port.postMessage(symbol + ' ' + get('stock.cgi?' + symbol));
+ timer = setTimeout(update, 10000);
+}
+port.onmessage = function (event) {
+ if (timer)
+ clearTimeout(timer);
+ symbol = event.message;
+ update();
+};</pre>
+
+ <p>The search query worker is as follows:
+
+ <pre>importScript('io.js');
+port.onmessage = function (event) {
+ port.postMessage(get('search.cgi?' + event.message));
+};</pre>
+
+ <p><a href="http://www.whatwg.org/demos/workers/stock/page.html">View this
+ example online</a>.
+
<h3 id=conformance><span class=secno>1.2 </span>Conformance requirements</h3>
<p>All diagrams, examples, and notes in this specification are
@@ -637,7 +752,7 @@
<var title="">url</var>.</p>
<p>If the attempt fails, then abort these steps and invoke the <a
- href="#worker" title="worker creation failed">error handling steps</a>
+ href="#worker0" title="worker creation failed">error handling steps</a>
defined by the algorithm that called this one.</p>
<p>If the attempt succeeds, then let <var title="">script</var> be the
@@ -997,8 +1112,8 @@
value is the <code>MessagePort</code> object return by the <a
href="#connect">connect to a worker</a> algorithm.</p>
- <p>Otherwise, if the <dfn id=worker>worker creation failed</dfn>, then at
- the next available opportunity, after any scripts have finished
+ <p>Otherwise, if the <dfn id=worker0>worker creation failed</dfn>, then
+ at the next available opportunity, after any scripts have finished
executing<!-- XXX queue -->, <span>fire a simple event</span> called
<code title=event-error>error</code> at the newly created port.</p>
</ol>
Received on Wednesday, 6 August 2008 11:04:13 UTC