Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 132 lines (88 sloc) 3.943 kb
e16cb02 Add Travis build status image to README
Michiel Kalkman authored
1 [![Build Status](https://travis-ci.org/michiel/sequencer-js.png)](https://travis-ci.org/michiel/sequencer-js)
2
0909d91 Init repo
Michiel Kalkman authored
3 # sequencer-js
4
5 Library functions for various sequencing related patterns.
6
379c46c Updated README
Michiel Kalkman authored
7 There are three main functions.
8
9 - sequence, performs arbitrary sync/async calls in sequence, the final callback is the last in the sequence
10 - collect, performs arbitrary sync/async calls in parallel and ends with a final callback
b83121c Typo fix
Michiel Kalkman authored
11 - pipeline, performs arbitrary sync/async calls in parallel as long as there are slots available and ends with a final callback
379c46c Updated README
Michiel Kalkman authored
12
7394277 Added npm install instructions
Michiel Kalkman authored
13 Install
14 -------
15
16 Using npm,
17
18 ~$ npm install sequencer
19
2f059cf Updated README
Michiel Kalkman authored
20 Code
21 ----
22
23 The code is all written using 1990s capabilities ECMAScript/262 (apart from the
24 CommonJS 'require' convention). In theory that means you can go back in time
25 and run it on really old JavaScript runtimes. In practice it means that it will
26 also work in IE and all the horrid forms in which it presents itself to us
27 today.
28
9a90ddd Updated README
Michiel Kalkman authored
29 There is also a Ruby implementation of the sequencer pattern (that lags behind
30 this one) at https://github.com/michiel/sequencer-rb .
31
ac23974 Updated README
Michiel Kalkman authored
32 Examples
33 --------
34
35 Examples can be found in the source examples/ directory. Each example can be run from the commandline. Ex,
36
37 ~/src/sequencer-js/examples$ node ex01-sequence-100-async-calls.js
38
39 The Sequencer patterns
40 ----------------------
41
42 Sequencing deals with common problems related to ordering asynchronous
43 functions. The primary one is decoupling flow control from functions. The
44 secondary problem is executing tasks that are inter-dependant serially and
45 tasks that are not in parallel. (The sequence method and collect/pipeline
46 methods, respectively). Additionally it should be trivial to mix async and sync
47 functions.
48
49 This is accomplished by keeping each discrete piece of functionality inside its
50 own closure and having these closures take an argumentless callback function as
51 their sole parameter.
52
53 For example,
54
55 doAsyncA(function() {
56 doAsyncB(function() {
57 doAsyncC(function() {
58 console.log("All done");
59 });
60 };
61 };
62
63 can be written using the basic sequence function as,
64
e71447a Updated README
Michiel Kalkman authored
65 function doAsyncA(callback) {
66 callback();
67 };
68 function doAsyncB(callback) {
69 callback();
70 };
71 function doAsyncC(callback) {
72 callback();
73 };
74
75 sequence([
76 doAsyncA,
77 doAsyncB,
78 doAsyncC
79 ]);
ac23974 Updated README
Michiel Kalkman authored
80
81 This is a bit more verbose than the original version, but apart from the
82 advantages that should later become apparent, it is already easier to read and
83 refactor. For example, changing the order or doAsyncB and doAsyncC is a matter
84 of re-ordering the elements in the array.
85
e71447a Updated README
Michiel Kalkman authored
86 sequence([
87 doAsyncA,
88 doAsyncB,
89 doAsyncC
90 ]);
ac23974 Updated README
Michiel Kalkman authored
91
92 Becomes,
93
e71447a Updated README
Michiel Kalkman authored
94 sequence([
95 doAsyncA,
96 doAsyncC,
97 doAsyncB
98 ]);
ac23974 Updated README
Michiel Kalkman authored
99
100 The same goes for removing or adding new items.
b67d0a7 Updated README
Michiel Kalkman authored
101
b6b1de2 Updated example
Michiel Kalkman authored
102 ### sequence ###
103
104 The sequence function is for ordering calls that have to be executed sequentially (i.e. calls that are interdependant).
105
106 ### collect ###
107
108 The collect function is for parallelization of calls that are not interdependant. It takes a seperate callback function to invoke on completion. It can be mixed with multiple sequence or collect calls to provide calling scheduling similar to Thread.join(). After completion of all calls a final callback will be invoked.
109
110 ### pipeline ###
111
112 The pipeline function is for maximum perfomance when executing calls that are not interdependant. Calls will be executed until there are no other slots available. Pipeline execution will continue until all calls have been executed. After completion of all calls a final callback will be invoked.
113
114
9a90ddd Updated README
Michiel Kalkman authored
115 TODO
116 ----
117
118 - More documentation
119 - More tests
120 - Extend the basic implementations with start/stop/etc fluff
121 - Write up a rationale on error handling
122 - Possibly some inline docs
123
124
125
b67d0a7 Updated README
Michiel Kalkman authored
126
0909d91 Init repo
Michiel Kalkman authored
127
38edbc1 @bitdeli-chef Add a Bitdeli badge to README
bitdeli-chef authored
128
129
130 [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/michiel/sequencer-js/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
131
Something went wrong with that request. Please try again.