LazyChain
Asynchronous method chain implemented in JavaScript
FRP(Functional reactive programming) supporting jQuery plugin
Lazy [1, 2, 3] -> .lazy() -> [2], [1], [3] [3], [2], [1] -> .lazy() -> [1], [3], [2] sequential [1, 2, 3] -> .lazy() -> [2, 1, 3] [3], [2], [1] -> .lazy() -> [1], [3], [2] Stream [1, 2, 3] -> .stream() -> [1, 2, 3] [3], [2], [1] -> .stream() -> [3], [2], [1] Branch [1, 2, 3] -> .stream() -> [1, 2, 3] -> [1, 2, 3] [3], [2], [1] -> .stream() -> [3], [2], [1] -> [3], [2], [1] Split [1, Err, 2] -> .stream() -> [1, 2] -> [Err] [2], [Err], [1]-> .stream() -> [2], [1] -> [Err] Merge [1, 2, 3] [4, 5, 6] -> .stream() -> [4, 5, 6], [1, 2, 3] [3], [2], [1] [6], [5], [4] -> .stream() -> [6], [5], [4], [3], [2], [1] Compose stream1 stream2 stream3 -> .stream() -> stream3 * stream2 * stream1 ( stream1 . stream2 . stream3 ) Buffer [2, 3], [1] -> .lazy() -> [3], [1, 2]
// Asynchronous method chain LazyChain([1, 2]) .lazy(function (v, i, a, e, d) { setTimeout(function () { d.resolve(v * 2) }, 50) }, true) .map(function (v) { return v * 3 }) .lazy(function (v, i, a, e, d) { setTimeout(function () { d.resolve(v * 10) }, 50) }, true) .reduce(function (r, v, i, a) { return r + v }, 0) .forEach(function (v) { console.log(v); // 180 = (1 * 2 * 3 * 10) + (2 * 2 * 3 * 10) }); LazyChain($.ajax('')) .filter(function (v) { return 'object' === typeof v }) .forEach(function (xhr) { console.log(xhr); // XHR object }); LazyChain(true); ['./'] .lazy(function (v) { return $.ajax(v) }) .filter(function (v) { return 'object' === typeof v }) .forEach(function (xhr) { console.log(xhr); // XHR object }); LazyChain(false); // Stream generate and control var a = LazyChain(), b = LazyChain(); LazyChain(a) .stream('number', b) .stream(function (v, i, a, e) { console.log(v); // 0 }); a.notify(0, ''); b .stream(function (v, i, a, e) { console.log(typeof v); // string });
Installation
<script charset="utf-8" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script charset="utf-8" src="/js/lazychain.js"></script>
Extend
Underscore.js / Lo-Dash
LazyChain([1, 2, 3], _ ) .stream(function (v) { console.log(v); // 1, 2, 3 }) .difference([1, 3]) .stream(function (v) { console.log(v); // 2 });
Documentation
API
LazyChain
Extend the array and jQueryDeferred object.
.lazy()
Messaging api.
.stream()
Stream control api.
Spec
Sorry, japanese documents only. I welcome translation.
Browser
- IE9+
- Chrome
- Firefox
- Safari
- Android
- iOS
jQuery
1.7.2+