chainsaw.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. var assert = require('assert');
  2. var Chainsaw = require('../index');
  3. exports.getset = function () {
  4. var to = setTimeout(function () {
  5. assert.fail('builder never fired');
  6. }, 1000);
  7. var ch = Chainsaw(function (saw) {
  8. clearTimeout(to);
  9. var num = 0;
  10. this.get = function (cb) {
  11. cb(num);
  12. saw.next();
  13. };
  14. this.set = function (n) {
  15. num = n;
  16. saw.next();
  17. };
  18. var ti = setTimeout(function () {
  19. assert.fail('end event not emitted');
  20. }, 50);
  21. saw.on('end', function () {
  22. clearTimeout(ti);
  23. assert.equal(times, 3);
  24. });
  25. });
  26. var times = 0;
  27. ch
  28. .get(function (x) {
  29. assert.equal(x, 0);
  30. times ++;
  31. })
  32. .set(10)
  33. .get(function (x) {
  34. assert.equal(x, 10);
  35. times ++;
  36. })
  37. .set(20)
  38. .get(function (x) {
  39. assert.equal(x, 20);
  40. times ++;
  41. })
  42. ;
  43. };
  44. exports.nest = function () {
  45. var ch = (function () {
  46. var vars = {};
  47. return Chainsaw(function (saw) {
  48. this.do = function (cb) {
  49. saw.nest(cb, vars);
  50. };
  51. });
  52. })();
  53. var order = [];
  54. var to = setTimeout(function () {
  55. assert.fail("Didn't get to the end");
  56. }, 50);
  57. ch
  58. .do(function (vars) {
  59. vars.x = 'y';
  60. order.push(1);
  61. this
  62. .do(function (vs) {
  63. order.push(2);
  64. vs.x = 'x';
  65. })
  66. .do(function (vs) {
  67. order.push(3);
  68. vs.z = 'z';
  69. })
  70. ;
  71. })
  72. .do(function (vars) {
  73. vars.y = 'y';
  74. order.push(4);
  75. })
  76. .do(function (vars) {
  77. assert.eql(order, [1,2,3,4]);
  78. assert.eql(vars, { x : 'x', y : 'y', z : 'z' });
  79. clearTimeout(to);
  80. })
  81. ;
  82. };
  83. exports.nestWait = function () {
  84. var ch = (function () {
  85. var vars = {};
  86. return Chainsaw(function (saw) {
  87. this.do = function (cb) {
  88. saw.nest(cb, vars);
  89. };
  90. this.wait = function (n) {
  91. setTimeout(function () {
  92. saw.next();
  93. }, n);
  94. };
  95. });
  96. })();
  97. var order = [];
  98. var to = setTimeout(function () {
  99. assert.fail("Didn't get to the end");
  100. }, 1000);
  101. var times = {};
  102. ch
  103. .do(function (vars) {
  104. vars.x = 'y';
  105. order.push(1);
  106. this
  107. .do(function (vs) {
  108. order.push(2);
  109. vs.x = 'x';
  110. times.x = Date.now();
  111. })
  112. .wait(50)
  113. .do(function (vs) {
  114. order.push(3);
  115. vs.z = 'z';
  116. times.z = Date.now();
  117. var dt = times.z - times.x;
  118. assert.ok(dt >= 50 && dt < 75);
  119. })
  120. ;
  121. })
  122. .do(function (vars) {
  123. vars.y = 'y';
  124. order.push(4);
  125. times.y = Date.now();
  126. })
  127. .wait(100)
  128. .do(function (vars) {
  129. assert.eql(order, [1,2,3,4]);
  130. assert.eql(vars, { x : 'x', y : 'y', z : 'z' });
  131. clearTimeout(to);
  132. times.end = Date.now();
  133. var dt = times.end - times.y;
  134. assert.ok(dt >= 100 && dt < 125)
  135. })
  136. ;
  137. };
  138. exports.nestNext = function () {
  139. var ch = (function () {
  140. var vars = {};
  141. return Chainsaw(function (saw) {
  142. this.do = function (cb) {
  143. saw.nest(false, function () {
  144. var args = [].slice.call(arguments);
  145. args.push(saw.next);
  146. cb.apply(this, args);
  147. }, vars);
  148. };
  149. });
  150. })();
  151. var order = [];
  152. var to = setTimeout(function () {
  153. assert.fail("Didn't get to the end");
  154. }, 500);
  155. var times = [];
  156. ch
  157. .do(function (vars, next_) {
  158. vars.x = 'y';
  159. order.push(1);
  160. this
  161. .do(function (vs, next) {
  162. order.push(2);
  163. vs.x = 'x';
  164. setTimeout(next, 30);
  165. })
  166. .do(function (vs, next) {
  167. order.push(3);
  168. vs.z = 'z';
  169. setTimeout(next, 10);
  170. })
  171. .do(function () {
  172. setTimeout(next_, 20);
  173. })
  174. ;
  175. })
  176. .do(function (vars, next) {
  177. vars.y = 'y';
  178. order.push(4);
  179. setTimeout(next, 5);
  180. })
  181. .do(function (vars) {
  182. assert.eql(order, [1,2,3,4]);
  183. assert.eql(vars, { x : 'x', y : 'y', z : 'z' });
  184. clearTimeout(to);
  185. })
  186. ;
  187. };
  188. exports.builder = function () {
  189. var cx = Chainsaw(function (saw) {
  190. this.x = function () {};
  191. });
  192. assert.ok(cx.x);
  193. var cy = Chainsaw(function (saw) {
  194. return { y : function () {} };
  195. });
  196. assert.ok(cy.y);
  197. var cz = Chainsaw(function (saw) {
  198. return { z : function (cb) { saw.nest(cb) } };
  199. });
  200. assert.ok(cz.z);
  201. var to = setTimeout(function () {
  202. assert.fail("Nested z didn't run");
  203. }, 50);
  204. cz.z(function () {
  205. clearTimeout(to);
  206. assert.ok(this.z);
  207. });
  208. };
  209. this.attr = function () {
  210. var to = setTimeout(function () {
  211. assert.fail("attr chain didn't finish");
  212. }, 50);
  213. var xy = [];
  214. var ch = Chainsaw(function (saw) {
  215. this.h = {
  216. x : function () {
  217. xy.push('x');
  218. saw.next();
  219. },
  220. y : function () {
  221. xy.push('y');
  222. saw.next();
  223. assert.eql(xy, ['x','y']);
  224. clearTimeout(to);
  225. }
  226. };
  227. });
  228. assert.ok(ch.h);
  229. assert.ok(ch.h.x);
  230. assert.ok(ch.h.y);
  231. ch.h.x().h.y();
  232. };
  233. exports.down = function () {
  234. var error = null;
  235. var s;
  236. var ch = Chainsaw(function (saw) {
  237. s = saw;
  238. this.raise = function (err) {
  239. error = err;
  240. saw.down('catch');
  241. };
  242. this.do = function (cb) {
  243. cb.call(this);
  244. };
  245. this.catch = function (cb) {
  246. if (error) {
  247. saw.nest(cb, error);
  248. error = null;
  249. }
  250. else saw.next();
  251. };
  252. });
  253. var to = setTimeout(function () {
  254. assert.fail(".do() after .catch() didn't fire");
  255. }, 50);
  256. ch
  257. .do(function () {
  258. this.raise('pow');
  259. })
  260. .do(function () {
  261. assert.fail("raise didn't skip over this do block");
  262. })
  263. .catch(function (err) {
  264. assert.equal(err, 'pow');
  265. })
  266. .do(function () {
  267. clearTimeout(to);
  268. })
  269. ;
  270. };
  271. exports.trap = function () {
  272. var error = null;
  273. var ch = Chainsaw(function (saw) {
  274. var pars = 0;
  275. var stack = [];
  276. var i = 0;
  277. this.par = function (cb) {
  278. pars ++;
  279. var j = i ++;
  280. cb.call(function () {
  281. pars --;
  282. stack[j] = [].slice.call(arguments);
  283. saw.down('result');
  284. });
  285. saw.next();
  286. };
  287. this.join = function (cb) {
  288. saw.trap('result', function () {
  289. if (pars == 0) {
  290. cb.apply(this, stack);
  291. saw.next();
  292. }
  293. });
  294. };
  295. this.raise = function (err) {
  296. error = err;
  297. saw.down('catch');
  298. };
  299. this.do = function (cb) {
  300. cb.call(this);
  301. };
  302. this.catch = function (cb) {
  303. if (error) {
  304. saw.nest(cb, error);
  305. error = null;
  306. }
  307. else saw.next();
  308. };
  309. });
  310. var to = setTimeout(function () {
  311. assert.fail(".do() after .join() didn't fire");
  312. }, 100);
  313. var tj = setTimeout(function () {
  314. assert.fail('.join() never fired');
  315. }, 100);
  316. var joined = false;
  317. ch
  318. .par(function () {
  319. setTimeout(this.bind(null, 1), 50);
  320. })
  321. .par(function () {
  322. setTimeout(this.bind(null, 2), 25);
  323. })
  324. .join(function (x, y) {
  325. assert.equal(x[0], 1);
  326. assert.equal(y[0], 2);
  327. clearTimeout(tj);
  328. joined = true;
  329. })
  330. .do(function () {
  331. clearTimeout(to);
  332. assert.ok(joined);
  333. })
  334. ;
  335. };
  336. exports.jump = function () {
  337. var to = setTimeout(function () {
  338. assert.fail('builder never fired');
  339. }, 50);
  340. var xs = [ 4, 5, 6, -4, 8, 9, -1, 8 ];
  341. var xs_ = [];
  342. var ch = Chainsaw(function (saw) {
  343. this.x = function (i) {
  344. xs_.push(i);
  345. saw.next();
  346. };
  347. this.y = function (step) {
  348. var x = xs.shift();
  349. if (x > 0) saw.jump(step);
  350. else saw.next();
  351. };
  352. saw.on('end', function () {
  353. clearTimeout(to);
  354. assert.eql(xs, [ 8 ]);
  355. assert.eql(xs_, [ 1, 1, 1, 1, 2, 3, 2, 3, 2, 3 ]);
  356. });
  357. });
  358. ch
  359. .x(1)
  360. .y(0)
  361. .x(2)
  362. .x(3)
  363. .y(2)
  364. ;
  365. };