async.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* eslint id-length: 0, func-names: 0, handle-callback-err: 0, max-lines: 0 */
  2. "use strict";
  3. var memoize = require("../..")
  4. , nextTick = require("next-tick");
  5. module.exports = function () {
  6. return {
  7. "Regular": {
  8. Success: function (a, d) {
  9. var mfn, fn, u = {}, i = 0, invoked = 0;
  10. fn = function (x, y, cb) {
  11. nextTick(function () {
  12. ++i;
  13. cb(null, x + y);
  14. });
  15. return u;
  16. };
  17. mfn = memoize(fn, { async: true });
  18. a(
  19. mfn(3, 7, function (err, res) {
  20. ++invoked;
  21. a.deep([err, res], [null, 10], "Result #1");
  22. }),
  23. u,
  24. "Initial"
  25. );
  26. a(
  27. mfn(3, 7, function (err, res) {
  28. ++invoked;
  29. a.deep([err, res], [null, 10], "Result #2");
  30. }),
  31. u,
  32. "Initial #2"
  33. );
  34. a(
  35. mfn(5, 8, function (err, res) {
  36. ++invoked;
  37. a.deep([err, res], [null, 13], "Result B #1");
  38. }),
  39. u,
  40. "Initial #2"
  41. );
  42. a(
  43. mfn(3, 7, function (err, res) {
  44. ++invoked;
  45. a.deep([err, res], [null, 10], "Result #3");
  46. }),
  47. u,
  48. "Initial #2"
  49. );
  50. a(
  51. mfn(5, 8, function (err, res) {
  52. ++invoked;
  53. a.deep([err, res], [null, 13], "Result B #2");
  54. }),
  55. u,
  56. "Initial #3"
  57. );
  58. nextTick(function () {
  59. a(i, 2, "Init Called");
  60. a(invoked, 5, "Cb Called");
  61. a(
  62. mfn(3, 7, function (err, res) {
  63. ++invoked;
  64. a.deep([err, res], [null, 10], "Again: Result");
  65. }),
  66. u,
  67. "Again: Initial"
  68. );
  69. a(
  70. mfn(5, 8, function (err, res) {
  71. ++invoked;
  72. a.deep([err, res], [null, 13], "Again B: Result");
  73. }),
  74. u,
  75. "Again B: Initial"
  76. );
  77. nextTick(function () {
  78. a(i, 2, "Init Called #2");
  79. a(invoked, 7, "Cb Called #2");
  80. mfn.delete(3, 7);
  81. a(
  82. mfn(3, 7, function (err, res) {
  83. ++invoked;
  84. a.deep([err, res], [null, 10], "Again: Result");
  85. }),
  86. u,
  87. "Again: Initial"
  88. );
  89. a(
  90. mfn(5, 8, function (err, res) {
  91. ++invoked;
  92. a.deep([err, res], [null, 13], "Again B: Result");
  93. }),
  94. u,
  95. "Again B: Initial"
  96. );
  97. nextTick(function () {
  98. a(i, 3, "Init After delete");
  99. a(invoked, 9, "Cb After delete");
  100. d();
  101. });
  102. });
  103. });
  104. },
  105. Error: function (a, d) {
  106. var mfn, fn, u = {}, i = 0, e = new Error("Test");
  107. fn = function (x, y, cb) {
  108. nextTick(function () {
  109. ++i;
  110. cb(e);
  111. });
  112. return u;
  113. };
  114. mfn = memoize(fn, { async: true, dispose: a.never });
  115. a(
  116. mfn(3, 7, function (err, res) {
  117. a.deep([err, res], [e, undefined], "Result #1");
  118. }),
  119. u,
  120. "Initial"
  121. );
  122. a(
  123. mfn(3, 7, function (err, res) {
  124. a.deep([err, res], [e, undefined], "Result #2");
  125. }),
  126. u,
  127. "Initial #2"
  128. );
  129. a(
  130. mfn(5, 8, function (err, res) {
  131. a.deep([err, res], [e, undefined], "Result B #1");
  132. }),
  133. u,
  134. "Initial #2"
  135. );
  136. a(
  137. mfn(3, 7, function (err, res) {
  138. a.deep([err, res], [e, undefined], "Result #3");
  139. }),
  140. u,
  141. "Initial #2"
  142. );
  143. a(
  144. mfn(5, 8, function (err, res) {
  145. a.deep([err, res], [e, undefined], "Result B #2");
  146. }),
  147. u,
  148. "Initial #3"
  149. );
  150. nextTick(function () {
  151. a(i, 2, "Called #2");
  152. a(
  153. mfn(3, 7, function (err, res) {
  154. a.deep([err, res], [e, undefined], "Again: Result");
  155. }),
  156. u,
  157. "Again: Initial"
  158. );
  159. a(
  160. mfn(5, 8, function (err, res) {
  161. a.deep([err, res], [e, undefined], "Again B: Result");
  162. }),
  163. u,
  164. "Again B: Initial"
  165. );
  166. nextTick(function () {
  167. a(i, 4, "Again Called #2");
  168. d();
  169. });
  170. });
  171. }
  172. },
  173. "Primitive": {
  174. "Success": function (a, d) {
  175. var mfn, fn, u = {}, i = 0;
  176. fn = function (x, y, cb) {
  177. nextTick(function () {
  178. ++i;
  179. cb(null, x + y);
  180. });
  181. return u;
  182. };
  183. mfn = memoize(fn, { async: true, primitive: true });
  184. a(
  185. mfn(3, 7, function (err, res) {
  186. a.deep([err, res], [null, 10], "Result #1");
  187. }),
  188. u,
  189. "Initial"
  190. );
  191. a(
  192. mfn(3, 7, function (err, res) {
  193. a.deep([err, res], [null, 10], "Result #2");
  194. }),
  195. u,
  196. "Initial #2"
  197. );
  198. a(
  199. mfn(5, 8, function (err, res) {
  200. a.deep([err, res], [null, 13], "Result B #1");
  201. }),
  202. u,
  203. "Initial #2"
  204. );
  205. a(
  206. mfn(3, 7, function (err, res) {
  207. a.deep([err, res], [null, 10], "Result #3");
  208. }),
  209. u,
  210. "Initial #2"
  211. );
  212. a(
  213. mfn(5, 8, function (err, res) {
  214. a.deep([err, res], [null, 13], "Result B #2");
  215. }),
  216. u,
  217. "Initial #3"
  218. );
  219. nextTick(function () {
  220. a(i, 2, "Called #2");
  221. a(
  222. mfn(3, 7, function (err, res) {
  223. a.deep([err, res], [null, 10], "Again: Result");
  224. }),
  225. u,
  226. "Again: Initial"
  227. );
  228. a(
  229. mfn(5, 8, function (err, res) {
  230. a.deep([err, res], [null, 13], "Again B: Result");
  231. }),
  232. u,
  233. "Again B: Initial"
  234. );
  235. nextTick(function () {
  236. a(i, 2, "Again Called #2");
  237. mfn.delete(3, 7);
  238. a(
  239. mfn(3, 7, function (err, res) {
  240. a.deep([err, res], [null, 10], "Again: Result");
  241. }),
  242. u,
  243. "Again: Initial"
  244. );
  245. a(
  246. mfn(5, 8, function (err, res) {
  247. a.deep([err, res], [null, 13], "Again B: Result");
  248. }),
  249. u,
  250. "Again B: Initial"
  251. );
  252. nextTick(function () {
  253. a(i, 3, "Call After delete");
  254. d();
  255. });
  256. });
  257. });
  258. },
  259. "Error": function (a, d) {
  260. var mfn, fn, u = {}, i = 0, e = new Error("Test");
  261. fn = function (x, y, cb) {
  262. nextTick(function () {
  263. ++i;
  264. cb(e);
  265. });
  266. return u;
  267. };
  268. mfn = memoize(fn, { async: true, primitive: true });
  269. a(
  270. mfn(3, 7, function (err, res) {
  271. a.deep([err, res], [e, undefined], "Result #1");
  272. }),
  273. u,
  274. "Initial"
  275. );
  276. a(
  277. mfn(3, 7, function (err, res) {
  278. a.deep([err, res], [e, undefined], "Result #2");
  279. }),
  280. u,
  281. "Initial #2"
  282. );
  283. a(
  284. mfn(5, 8, function (err, res) {
  285. a.deep([err, res], [e, undefined], "Result B #1");
  286. }),
  287. u,
  288. "Initial #2"
  289. );
  290. a(
  291. mfn(3, 7, function (err, res) {
  292. a.deep([err, res], [e, undefined], "Result #3");
  293. }),
  294. u,
  295. "Initial #2"
  296. );
  297. a(
  298. mfn(5, 8, function (err, res) {
  299. a.deep([err, res], [e, undefined], "Result B #2");
  300. }),
  301. u,
  302. "Initial #3"
  303. );
  304. nextTick(function () {
  305. a(i, 2, "Called #2");
  306. a(
  307. mfn(3, 7, function (err, res) {
  308. a.deep([err, res], [e, undefined], "Again: Result");
  309. }),
  310. u,
  311. "Again: Initial"
  312. );
  313. a(
  314. mfn(5, 8, function (err, res) {
  315. a.deep([err, res], [e, undefined], "Again B: Result");
  316. }),
  317. u,
  318. "Again B: Initial"
  319. );
  320. nextTick(function () {
  321. a(i, 4, "Again Called #2");
  322. d();
  323. });
  324. });
  325. },
  326. "Primitive null arg case": function (a, d) {
  327. var x = {}
  328. , mfn = memoize(
  329. function f(id, cb) {
  330. cb(null, x);
  331. },
  332. {
  333. async: true,
  334. primitive: true
  335. }
  336. );
  337. mfn(null, function (err, res) {
  338. a.deep([err, res], [null, x], "Args");
  339. d();
  340. });
  341. }
  342. },
  343. "Sync Clear": function (a, d) {
  344. var mfn, fn;
  345. fn = function (x, cb) {
  346. nextTick(function () {
  347. cb(null, x);
  348. });
  349. };
  350. mfn = memoize(fn, { async: true });
  351. mfn(1, function (err, i) {
  352. a(i, 1, "First");
  353. });
  354. mfn.clear();
  355. mfn(2, function (err, i) {
  356. a(i, 2, "Second");
  357. d();
  358. });
  359. },
  360. "Sync Clear: Primitive": function (a, d) {
  361. var mfn, fn;
  362. fn = function (x, cb) {
  363. nextTick(function () {
  364. cb(null, x);
  365. });
  366. };
  367. mfn = memoize(fn, { async: true, primitive: true });
  368. mfn(2, function (err, i) {
  369. a(i, 2, "First");
  370. });
  371. mfn(1, function (err, i) {
  372. a(i, 1, "Second");
  373. nextTick(d);
  374. });
  375. mfn.clear();
  376. mfn(2, function (err, i) {
  377. a(i, 2, "Third");
  378. });
  379. }
  380. };
  381. };