123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- /* eslint id-length: 0, max-lines: 0, max-statements: 0 */
- "use strict";
- var memoize = require("../..")
- , nextTick = require("next-tick")
- , delay = require("timers-ext/delay")
- , Promise = require("plain-promise");
- module.exports = function () {
- return {
- Regular: {
- "Sync": function (a) {
- var mfn, fn, value = [], x, invoked;
- fn = function (arg1, arg2) {
- return arg1 + arg2;
- };
- mfn = memoize(fn, {
- dispose: function (val) {
- value.push(val);
- }
- });
- mfn(3, 7);
- mfn(5, 8);
- mfn(12, 4);
- a.deep(value, [], "Pre");
- mfn.delete(5, 8);
- a.deep(value, [13], "#1");
- value = [];
- mfn.delete(12, 4);
- a.deep(value, [16], "#2");
- value = [];
- mfn(77, 11);
- mfn.clear();
- a.deep(value, [10, 88], "Clear all");
- x = {};
- invoked = false;
- mfn = memoize(
- function () {
- return x;
- },
- {
- dispose: function (val) {
- invoked = val;
- }
- }
- );
- mfn.delete();
- a(invoked, false, "No args: Post invalid delete");
- mfn();
- a(invoked, false, "No args: Post cache");
- mfn.delete();
- a(invoked, x, "No args: Pre delete");
- },
- "Ref counter": function (a) {
- var mfn, fn, value = [];
- fn = function (x, y) {
- return x + y;
- };
- mfn = memoize(fn, {
- refCounter: true,
- dispose: function (val) {
- value.push(val);
- }
- });
- mfn(3, 7);
- mfn(5, 8);
- mfn(12, 4);
- a.deep(value, [], "Pre");
- mfn(5, 8);
- mfn.deleteRef(5, 8);
- a.deep(value, [], "Pre");
- mfn.deleteRef(5, 8);
- a.deep(value, [13], "#1");
- value = [];
- mfn.deleteRef(12, 4);
- a.deep(value, [16], "#2");
- value = [];
- mfn(77, 11);
- mfn.clear();
- a.deep(value, [10, 88], "Clear all");
- },
- "Async": function (a, d) {
- var mfn, fn, u = {}, value = [];
- fn = function (x, y, cb) {
- nextTick(function () {
- cb(null, x + y);
- });
- return u;
- };
- mfn = memoize(fn, {
- async: true,
- dispose: function (val) {
- value.push(val);
- }
- });
- mfn(3, 7, function () {
- mfn(5, 8, function () {
- mfn(12, 4, function () {
- a.deep(value, [], "Pre");
- mfn.delete(5, 8);
- a.deep(value, [13], "#1");
- value = [];
- mfn.delete(12, 4);
- a.deep(value, [16], "#2");
- value = [];
- mfn(77, 11, function () {
- mfn.clear();
- a.deep(value, [10, 88], "Clear all");
- d();
- });
- });
- });
- });
- },
- "Promise": function (a, d) {
- var mfn, fn, value = [];
- fn = function (x, y) {
- return new Promise(function (res) {
- res(x + y);
- });
- };
- mfn = memoize(fn, {
- promise: true,
- dispose: function (val) {
- value.push(val);
- }
- });
- mfn(3, 7).done(function () {
- mfn(5, 8).done(function () {
- mfn(12, 4).done(
- delay(function () {
- a.deep(value, [], "Pre");
- mfn.delete(5, 8);
- a.deep(value, [13], "#1");
- value = [];
- mfn.delete(12, 4);
- a.deep(value, [16], "#2");
- value = [];
- mfn(77, 11).done(
- delay(function () {
- mfn.clear();
- a.deep(value, [10, 88], "Clear all");
- d();
- })
- );
- })
- );
- });
- });
- }
- },
- Primitive: {
- "Sync": function (a) {
- var mfn, fn, value = [];
- fn = function (x, y) {
- return x + y;
- };
- mfn = memoize(fn, {
- dispose: function (val) {
- value.push(val);
- }
- });
- mfn(3, 7);
- mfn(5, 8);
- mfn(12, 4);
- a.deep(value, [], "Pre");
- mfn.delete(5, 8);
- a.deep(value, [13], "#1");
- value = [];
- mfn.delete(12, 4);
- a.deep(value, [16], "#2");
- value = [];
- mfn(77, 11);
- mfn.clear();
- a.deep(value, [10, 88], "Clear all");
- },
- "Ref counter": function (a) {
- var mfn, fn, value = [];
- fn = function (x, y) {
- return x + y;
- };
- mfn = memoize(fn, {
- refCounter: true,
- dispose: function (val) {
- value.push(val);
- }
- });
- mfn(3, 7);
- mfn(5, 8);
- mfn(12, 4);
- a.deep(value, [], "Pre");
- mfn(5, 8);
- mfn.deleteRef(5, 8);
- a.deep(value, [], "Pre");
- mfn.deleteRef(5, 8);
- a.deep(value, [13], "#1");
- value = [];
- mfn.deleteRef(12, 4);
- a.deep(value, [16], "#2");
- value = [];
- mfn(77, 11);
- mfn.clear();
- a.deep(value, [10, 88], "Clear all");
- },
- "Async": function (a, d) {
- var mfn, fn, u = {}, value = [];
- fn = function (x, y, cb) {
- nextTick(function () {
- cb(null, x + y);
- });
- return u;
- };
- mfn = memoize(fn, {
- async: true,
- dispose: function (val) {
- value.push(val);
- }
- });
- mfn(3, 7, function () {
- mfn(5, 8, function () {
- mfn(12, 4, function () {
- a.deep(value, [], "Pre");
- mfn.delete(5, 8);
- a.deep(value, [13], "#1");
- value = [];
- mfn.delete(12, 4);
- a.deep(value, [16], "#2");
- value = [];
- mfn(77, 11, function () {
- mfn.clear();
- a.deep(value, [10, 88], "Clear all");
- d();
- });
- });
- });
- });
- },
- "Promise": function (a, d) {
- var mfn, fn, value = [];
- fn = function (x, y) {
- return new Promise(function (res) {
- res(x + y);
- });
- };
- mfn = memoize(fn, {
- promise: true,
- dispose: function (val) {
- value.push(val);
- }
- });
- mfn(3, 7).done(function () {
- mfn(5, 8).done(function () {
- mfn(12, 4).done(
- delay(function () {
- a.deep(value, [], "Pre");
- mfn.delete(5, 8);
- a.deep(value, [13], "#1");
- value = [];
- mfn.delete(12, 4);
- a.deep(value, [16], "#2");
- value = [];
- mfn(77, 11).done(
- delay(function () {
- mfn.clear();
- a.deep(value, [10, 88], "Clear all");
- d();
- })
- );
- })
- );
- });
- });
- }
- }
- };
- };
|