123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- /* eslint id-length: 0, handle-callback-err: 0, no-undef: 0, no-unused-vars: 0, func-names: 0 */
- "use strict";
- var memoize = require("../..")
- , nextTick = require("next-tick")
- , Promise = require("plain-promise");
- module.exports = function () {
- return {
- "Regular": {
- Success: function (a, d) {
- var mfn, fn, i = 0, invoked = 0;
- fn = function (x, y) {
- return new Promise(function (res) {
- ++i;
- res(x + y);
- });
- };
- mfn = memoize(fn, { promise: true });
- mfn(3, 7).done(function (res) {
- ++invoked;
- a(res, 10, "Result #1");
- }, a.never);
- mfn(3, 7).done(function (res) {
- ++invoked;
- a(res, 10, "Result #2");
- }, a.never);
- mfn(5, 8).done(function (res) {
- ++invoked;
- a(res, 13, "Result B #1");
- }, a.never);
- mfn(3, 7).done(function (res) {
- ++invoked;
- a(res, 10, "Result #3");
- }, a.never);
- mfn(5, 8).done(function (res) {
- ++invoked;
- a(res, 13, "Result B #2");
- }, a.never);
- setTimeout(function () {
- a(i, 2, "Init Called");
- a(invoked, 5, "Cb Called");
- mfn(3, 7).done(function (res) {
- ++invoked;
- a(res, 10, "Again: Result");
- }, a.never);
- mfn(5, 8).done(function (res) {
- ++invoked;
- a(res, 13, "Again B: Result");
- }, a.never);
- setTimeout(function () {
- a(i, 2, "Init Called #2");
- a(invoked, 7, "Cb Called #2");
- mfn.delete(3, 7);
- mfn(3, 7).done(function (res) {
- ++invoked;
- a(res, 10, "Again: Result");
- }, a.never);
- mfn(5, 8).done(function (res) {
- ++invoked;
- a(res, 13, "Again B: Result");
- }, a.never);
- setTimeout(function () {
- a(i, 3, "Init After delete");
- a(invoked, 9, "Cb After delete");
- d();
- }, 10);
- }, 10);
- }, 10);
- },
- Error: function (a, d) {
- var mfn, fn, i = 0, e = new Error("Test");
- fn = function (x, y) {
- ++i;
- return new Promise(function (res, rej) {
- rej(e);
- });
- };
- mfn = memoize(fn, { promise: "done", dispose: a.never });
- mfn(3, 7).done(a.never, function (err) {
- a(err, e, "Result #1");
- });
- mfn(5, 8).done(a.never, function (err) {
- a(err, e, "Result B #2");
- });
- setTimeout(function () {
- a(i, 2, "Called #2");
- mfn(3, 7).done(a.never, function (err) {
- a(err, e, "Again: Result");
- });
- mfn(5, 8).done(a.never, function (err) {
- a(err, e, "Again B: Result");
- });
- setTimeout(function (err) {
- a(i, 4, "Again Called #2");
- d();
- }, 10);
- }, 10);
- }
- },
- "Primitive": {
- "Success": function (a, d) {
- var mfn, fn, i = 0;
- fn = function (x, y) {
- return new Promise(function (res) {
- ++i;
- res(x + y);
- });
- };
- mfn = memoize(fn, { promise: true, primitive: true });
- mfn(3, 7).done(function (res) {
- a(res, 10, "Result #1");
- }, a.never);
- mfn(3, 7).done(function (res) {
- a(res, 10, "Result #2");
- }, a.never);
- mfn(5, 8).done(function (res) {
- a(res, 13, "Result B #1");
- }, a.never);
- mfn(3, 7).done(function (res) {
- a(res, 10, "Result #3");
- }, a.never);
- mfn(5, 8).done(function (res) {
- a(res, 13, "Result B #2");
- }, a.never);
- setTimeout(function () {
- a(i, 2, "Called #2");
- mfn(3, 7).done(function (res) {
- a(res, 10, "Again: Result");
- }, a.never);
- mfn(5, 8).done(function (res) {
- a(res, 13, "Again B: Result");
- }, a.never);
- setTimeout(function () {
- a(i, 2, "Again Called #2");
- mfn.delete(3, 7);
- mfn(3, 7).done(function (res) {
- a(res, 10, "Again: Result");
- }, a.never);
- mfn(5, 8).done(function (res) {
- a(res, 13, "Again B: Result");
- }, a.never);
- setTimeout(function () {
- a(i, 3, "Call After delete");
- d();
- }, 10);
- }, 10);
- }, 10);
- },
- "Error": function (a, d) {
- var mfn, fn, i = 0, e = new Error("Test");
- fn = function (x, y) {
- return new Promise(function (res, rej) {
- ++i;
- rej(e);
- });
- };
- mfn = memoize(fn, { promise: "done", primitive: true });
- mfn(3, 7).done(a.never, function (err) {
- a(err, e, "Result #1");
- });
- mfn(5, 8).done(a.never, function (err) {
- a(err, e, "Result B #2");
- });
- setTimeout(function () {
- a(i, 2, "Called #2");
- mfn(3, 7).done(a.never, function (err) {
- a(err, e, "Again: Result");
- });
- mfn(5, 8).done(a.never, function (err) {
- a(err, e, "Again B: Result");
- });
- setTimeout(function (err) {
- a(i, 4, "Again Called #2");
- d();
- }, 10);
- }, 10);
- },
- "Primitive null arg case": function (a, d) {
- var mfn, x = {};
- mfn = memoize(
- function f(id) {
- return new Promise(function (res) {
- res(x);
- });
- },
- {
- promise: true,
- primitive: true
- }
- );
- mfn(null).done(function (res) {
- a.deep(res, x, "Args");
- d();
- }, a.never);
- }
- },
- "Sync Clear": function (a, d) {
- var mfn, fn;
- fn = function (x) {
- return new Promise(function (res) {
- nextTick(function () {
- res(x);
- });
- });
- };
- mfn = memoize(fn, { promise: true });
- mfn(1).done(function (res) {
- a(res, 1, "First");
- }, a.never);
- mfn(2).done(function (res) {
- a(res, 2, "Second");
- d();
- }, a.never);
- },
- "Sync Clear: Primitive": function (a, d) {
- var mfn, fn;
- fn = function (x) {
- return new Promise(function (res) {
- nextTick(function () {
- res(x);
- });
- });
- };
- mfn = memoize(fn, { promise: true, primitive: true });
- mfn(1).done(function (res) {
- a(res, 1, "First");
- }, a.never);
- mfn(2).done(function (res) {
- a(res, 2, "Second");
- d();
- }, a.never);
- }
- };
- };
|