Permalink
Browse files

markdown

  • Loading branch information...
1 parent fc23d0b commit ab51a9c330e492fc9812aaf38c3b8861fe792bd4 @cho45 committed Mar 16, 2012
Showing with 4 additions and 259 deletions.
  1. +1 −231 README.markdown
  2. +3 −28 index.html
View
@@ -1,234 +1,4 @@
JSDeferred
==========
-![JSDeferred Structure]( http://f.hatena.ne.jp/images/fotolife/c/cho45/20071208/20071208021643.png )
-
-Simple and clean asynchronous processing.
-
-
-Sample
-------
-
-[JSDeferred Samples]( http://cho45.stfuawsc.com/jsdeferred/sample.html )
-
-
-Download
---------
-
- * [jsdeferred.js]( http://github.com/cho45/jsdeferred/raw/master/jsdeferred.js )
- * No comments: [jsdeferred.nodoc.js]( http://github.com/cho45/jsdeferred/raw/master/jsdeferred.nodoc.js )
- * With jQuery supports: [jsdeferred.jquery.js]( http://github.com/cho45/jsdeferred/raw/master/jsdeferred.jquery.js )
-
-Repository:
-
- git clone git://github.com/cho45/jsdeferred.git
-
-For userscript
---------------
-
-Copy and paste following at end of your userscript:
-
- * [jsdeferred.userscript.js]( http://github.com/cho45/jsdeferred/raw/master/jsdeferred.userscript.js )
-
-Example:
-
- with (D()) {
-
- next(fun...);
-
- // normal xhr
- http.get("...").
- next(fun...);
-
- // cross site
- xhttp.get("...").
- next(fun...);
-
- }
-
- // pasted code
- function D () {
- ...JSDeferred...
- }
-
-See [binding/userscript.js]( http://github.com/cho45/jsdeferred/raw/master/binding/userscript.js )
-to get more information of utility functions (http.get/xhttp.get)
-
-Documentation
--------------
-
-See source.
-
-## Introduction ##
-
- * [doc/intro.html (Japanese)]( http://cho45.stfuawsc.com/jsdeferred/doc/intro.html )
- * [doc/intro.en.html (English)]( http://cho45.stfuawsc.com/jsdeferred/doc/intro.en.html )
-
-[doc/index.html]( http://cho45.stfuawsc.com/jsdeferred/doc/index.html )
-
-Tests
------
-
-[test.html]( http://cho45.stfuawsc.com/jsdeferred/test.html )
-
-License
--------
-
-Copyright 2007-2009 cho45 <cho45@lowreal.net>
-
-MIT. See header of [jsdeferred.js]( http://github.com/cho45/jsdeferred/raw/master/jsdeferred.js )
-
-Concept
--------
-
- * Compact
- * Greasemonkey friendly (standalone and compact...)
- * Method chain
- * Short and meaning function names
- * Useful shorthand ways
-
-Internal
---------
-
-This sections use some words as following meanings.
-
- chain::
- a sequence of processes.
- child::
- the Deferred which returns from a callback.
- Deferred#foobar::
- Deferred.prototype.foobar
-
-### Deferred structure and chain structure ###
-
-A Deferred object has only one callback as its process. Deferred object packages a process (function) as callback and has reference to next Deferred (this is thought like continuation).
-
-Example for understanding Deferred structure.
-
- var d1 = new Deferred();
- d1.callback.ok = function () {
- alert("1");
- };
-
- var d2 = new Deferred();
- d2.callback.ok = function () {
- alert("2");
- };
-
- // Set d2 as continuation of d1.
- d1._next = d2;
-
- // Invoke the chain.
- d1.call();
-
-And example for usual use.
-
- next(function () { // this `next` is global function
- alert("1");
- }).
- next(function () { // this `next` is Deferred#next
- alert("2");
- }).
- next(function () {
- alert("3");
- });
-
-Deferred#next creates new Deferred, sets the passed functions to process of it, sets it as continuation of `this` and returns it.
-
-
-This structure makes easy to chain child Deferreds.
-
- next(function () {
- alert("1");
- }).
- next(function () {
- alert("2");
- // child Deferred
- return next(function () {
- alert("3");
- });
- }).
- next(function () {
- alert("4");
- });
-
-When the callback returns Deferred, the Deferred calling the callback only sets its continuation (`_next`) to returned Deferred's continuation.
-
- next(function () {
- alert("1");
- }).
- next(function () {
- alert("2");
- var d = next(function () {
- alert("3");
- });
- d._next = this._next;
- this.cancel();
- }).
- next(function () {
- alert("4");
- });
-
-After the process, above code is same as following:
-
- next(function () {
- alert("1");
- }).
- next(function () {
- alert("2");
- next(function () {
- alert("3");
- }).
- next(function () {
- alert("4");
- });
- });
-
-![Chain child deferred]( http://f.hatena.ne.jp/images/fotolife/c/cho45/20071207/20071207014817.png )
-
-### Error processing and recovering ###
-
-A Deferred has also error-back for error processing. Let's just see an example (this is from test):
-
- next(function () {
- throw "Error";
- }).
- error(function (e) {
- expect("Errorback called", "Error", e);
- return e; // recovering error
- }).
- next(function (e) {
- expect("Callback called", "Error", e);
- throw "Error2";
- }).
- next(function (e) {
- // This process is not called because
- // the error is not recovered.
- ng("Must not be called!!");
- }).
- error(function (e) {
- expect("Errorback called", "Error2", e);
- });
-
-The error thrown in callback is propagated by error-back chain. If the error-back returns normal value, the error is considered as recovery, and the callback chain continues.
-
-### Difference between MochiKit and JSDeferred ###
-
-JSDeferred is inspired by MochiKit Deferred object, so both is similar but JSDeferred drops some functions and simplified it.
-
- * MochiKit Deferred has chain by Array. JSDeferred has chain by chain of Deferred.
- * MochiKit Deferred separates parent chain and child chain, JSDeferred not.
-
-## Different-origin Deferred instances
-
-JSDeferred can be used in inter-environment which is independent respectively like browser extension
-because JSDeferred determines a self-class identity by instance id.
-
-Author
--------
-
-Copyright 2007-2009 cho45 <cho45@lowreal.net>
-
- * Portfolio: [www.lowreal.net]( http://www.lowreal.net/ )
- * Diary (Japanese): [subtech]( http://subtech.g.hatena.ne.jp/cho45/ )
-
+See: http://cho45.stfuawsc.com/jsdeferred/
View
@@ -413,9 +413,9 @@
<h2>Automatically Divide a Long Loop</h2>
<p>loop() function is effective when each loop is a heavy process. However, when a single loop is not so heavy but the number of iteration is numerous, it's not efficient.
- Here I define a function called aloop()</p>
+ Here I define a function called repeat()</p>
<pre>
-<span class="Identifier">function</span> aloop (n, f) {
+<span class="Identifier">function</span> repeat (n, f) {
<span class="Type">var</span> i = <span class="Constant">0</span>, end = {}, ret = <span class="Type">null</span>;
<span class="Statement">return</span> Deferred.next(<span class="Identifier">function</span> () {
<span class="Type">var</span> t = (<span class="Statement">new</span> <span class="Special">Date</span>()).getTime();
@@ -585,32 +585,7 @@
<section>
<h1>Auto Divided Loop</h1>
<pre class="runnable">
-<span class="Identifier">function</span> aloop (n, f) {
- <span class="Type">var</span> i = <span class="Constant">0</span>;
- <span class="Type">var</span> end = <span class="Statement">new</span> <span class="Special">Object</span>;
- <span class="Type">var</span> ret = <span class="Type">null</span>;
- <span class="Statement">return</span> Deferred.next(<span class="Identifier">function</span> () {
- <span class="Type">var</span> t = (<span class="Statement">new</span> <span class="Special">Date</span>()).getTime();
- <span class="Statement">try</span> {
- <span class="Statement">do</span> {
- ret = f(i)
- i++;
- <span class="Statement">if</span> (i &gt;= n) <span class="Statement">throw</span> end;
- } <span class="Statement">while</span> ((<span class="Statement">new</span> <span class="Special">Date</span>()).getTime() - t &lt; <span class="Constant">50</span>);
- console.log(<span class="Constant">&quot;Devided: &quot;</span> + ((<span class="Statement">new</span> <span class="Special">Date</span>()).getTime() - t) + <span class="Constant">&quot;msec.&quot;</span>);
- <span class="Statement">return</span> Deferred.call(arguments.callee);
- } <span class="Statement">catch</span> (e) {
- <span class="Statement">if</span> (e == end) {
- console.log(<span class="Constant">&quot;End&quot;</span>);
- <span class="Statement">return</span> ret;
- } <span class="Statement">else</span> {
- <span class="Statement">throw</span> e;
- }
- }
- });
-}
-
-aloop(<span class="Constant">100</span>, <span class="Identifier">function</span> (n, o) {
+Deferred.repeat(<span class="Constant">100</span>, <span class="Identifier">function</span> (n, o) {
console.log(n);
<span class="Statement">for</span> (<span class="Type">var</span> i = <span class="Constant">0</span>; i &lt; <span class="Special">Math</span>.pow(n, <span class="Constant">2</span>); i++) {
<span class="Statement">for</span> (<span class="Type">var</span> j = n; j; j--);

0 comments on commit ab51a9c

Please sign in to comment.