| 1 |
/* eslint max-lines: 0, id-length: 0, no-undef: 0 */ |
| 2 |
|
| 3 |
"use strict"; |
| 4 |
|
| 5 |
var memoize = require("../..") |
| 6 |
, nextTick = require("next-tick") |
| 7 |
, Promise = require("plain-promise"); |
| 8 |
|
| 9 |
module.exports = function () { |
| 10 |
return { |
| 11 |
"Regular": function (a) { |
| 12 |
var i = 0 |
| 13 |
, fn = function (x, y, z) { |
| 14 |
++i; |
| 15 |
return x + y + z; |
| 16 |
} |
| 17 |
, mfn; |
| 18 |
mfn = memoize(fn, { refCounter: true }); |
| 19 |
a(mfn.deleteRef(3, 5, 7), null, "Delete before"); |
| 20 |
a(mfn(3, 5, 7), 15, "Initial"); |
| 21 |
a(mfn(3, 5, 7), 15, "Cache"); |
| 22 |
a(mfn.deleteRef(3, 5, 7), false, "Delete #1"); |
| 23 |
mfn(3, 5, 7); |
| 24 |
a(mfn.deleteRef(3, 5, 7), false, "Delete #2"); |
| 25 |
mfn(3, 5, 7); |
| 26 |
a(mfn.deleteRef(3, 5, 7), false, "Delete #3"); |
| 27 |
mfn(3, 5, 7); |
| 28 |
a(i, 1, "Not deleteed"); |
| 29 |
a(mfn.deleteRef(3, 5, 7), false, "Delete #4"); |
| 30 |
a(mfn.deleteRef(3, 5, 7), true, "Delete final"); |
| 31 |
mfn(3, 5, 7); |
| 32 |
a(i, 2, "Restarted"); |
| 33 |
mfn(3, 5, 7); |
| 34 |
a(i, 2, "Cached again"); |
| 35 |
}, |
| 36 |
"Regular: Async": function (a, d) { |
| 37 |
var mfn, fn, u = {}, i = 0; |
| 38 |
fn = function (x, y, cb) { |
| 39 |
nextTick(function () { |
| 40 |
++i; |
| 41 |
cb(null, x + y); |
| 42 |
}); |
| 43 |
return u; |
| 44 |
}; |
| 45 |
|
| 46 |
mfn = memoize(fn, { async: true, refCounter: true }); |
| 47 |
|
| 48 |
a(mfn.deleteRef(3, 7), null, "Delete ref before"); |
| 49 |
|
| 50 |
a( |
| 51 |
mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, |
| 52 |
"Initial" |
| 53 |
); |
| 54 |
a( |
| 55 |
mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, |
| 56 |
"Initial #2" |
| 57 |
); |
| 58 |
a( |
| 59 |
mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), |
| 60 |
u, "Initial #2" |
| 61 |
); |
| 62 |
a( |
| 63 |
mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, |
| 64 |
"Initial #2" |
| 65 |
); |
| 66 |
a( |
| 67 |
mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), |
| 68 |
u, "Initial #3" |
| 69 |
); |
| 70 |
|
| 71 |
nextTick(function () { |
| 72 |
a(i, 2, "Called #2"); |
| 73 |
|
| 74 |
a( |
| 75 |
mfn(3, 7, function (err, res) { |
| 76 |
a.deep([err, res], [null, 10], "Again: Result"); |
| 77 |
}), |
| 78 |
u, "Again: Initial" |
| 79 |
); |
| 80 |
a( |
| 81 |
mfn(5, 8, function (err, res) { |
| 82 |
a.deep([err, res], [null, 13], "Again B: Result"); |
| 83 |
}), |
| 84 |
u, "Again B: Initial" |
| 85 |
); |
| 86 |
|
| 87 |
nextTick(function () { |
| 88 |
a(i, 2, "Again Called #2"); |
| 89 |
|
| 90 |
a(mfn.deleteRef(3, 7), false, "Delete ref #1"); |
| 91 |
a(mfn.deleteRef(3, 7), false, "Delete ref #2"); |
| 92 |
a(mfn.deleteRef(3, 7), false, "Delete ref #3"); |
| 93 |
a(mfn.deleteRef(3, 7), true, "Delete ref Final"); |
| 94 |
|
| 95 |
a( |
| 96 |
mfn(3, 7, function (err, res) { |
| 97 |
a.deep([err, res], [null, 10], "Again: Result"); |
| 98 |
}), |
| 99 |
u, "Again: Initial" |
| 100 |
); |
| 101 |
a( |
| 102 |
mfn(5, 8, function (err, res) { |
| 103 |
a.deep([err, res], [null, 13], "Again B: Result"); |
| 104 |
}), |
| 105 |
u, "Again B: Initial" |
| 106 |
); |
| 107 |
|
| 108 |
nextTick(function () { |
| 109 |
a(i, 3, "Call After delete"); |
| 110 |
d(); |
| 111 |
}); |
| 112 |
}); |
| 113 |
}); |
| 114 |
}, |
| 115 |
"Regular: Promise": function (a, d) { |
| 116 |
var mfn, fn, i = 0; |
| 117 |
fn = function (x, y) { |
| 118 |
++i; |
| 119 |
return new Promise(function (res) { res(x + y); }); |
| 120 |
}; |
| 121 |
|
| 122 |
mfn = memoize(fn, { promise: true, refCounter: true }); |
| 123 |
|
| 124 |
a(mfn.deleteRef(3, 7), null, "Delete ref before"); |
| 125 |
|
| 126 |
mfn(3, 7).done(function (res) { a(res, 10, "Result #1"); }); |
| 127 |
mfn(3, 7).done(function (res) { a(res, 10, "Result #2"); }); |
| 128 |
mfn(5, 8).done(function (res) { a(res, 13, "Result B #1"); }); |
| 129 |
mfn(3, 7).done(function (res) { a(res, 10, "Result #3"); }); |
| 130 |
mfn(5, 8).done(function (res) { a(res, 13, "Result B #2"); }); |
| 131 |
|
| 132 |
setTimeout(function () { |
| 133 |
a(i, 2, "Called #2"); |
| 134 |
|
| 135 |
mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }); |
| 136 |
mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }); |
| 137 |
|
| 138 |
setTimeout(function () { |
| 139 |
a(i, 2, "Again Called #2"); |
| 140 |
|
| 141 |
a(mfn.deleteRef(3, 7), false, "Delete ref #1"); |
| 142 |
a(mfn.deleteRef(3, 7), false, "Delete ref #2"); |
| 143 |
a(mfn.deleteRef(3, 7), false, "Delete ref #3"); |
| 144 |
a(mfn.deleteRef(3, 7), true, "Delete ref Final"); |
| 145 |
|
| 146 |
mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }); |
| 147 |
mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }); |
| 148 |
|
| 149 |
setTimeout(function () { |
| 150 |
a(i, 3, "Call After delete"); |
| 151 |
d(); |
| 152 |
}, 10); |
| 153 |
}, 10); |
| 154 |
}, 10); |
| 155 |
}, |
| 156 |
"Primitive": function (a) { |
| 157 |
var i = 0 |
| 158 |
, fn = function (x, y, z) { |
| 159 |
++i; |
| 160 |
return x + y + z; |
| 161 |
} |
| 162 |
, mfn; |
| 163 |
mfn = memoize(fn, { primitive: true, refCounter: true }); |
| 164 |
a(mfn.deleteRef(3, 5, 7), null, "Delete before"); |
| 165 |
a(mfn(3, 5, 7), 15, "Initial"); |
| 166 |
a(mfn(3, 5, 7), 15, "Cache"); |
| 167 |
a(mfn.deleteRef(3, 5, 7), false, "Delete #1"); |
| 168 |
mfn(3, 5, 7); |
| 169 |
a(mfn.deleteRef(3, 5, 7), false, "Delete #2"); |
| 170 |
mfn(3, 5, 7); |
| 171 |
a(mfn.deleteRef(3, 5, 7), false, "Delete #3"); |
| 172 |
mfn(3, 5, 7); |
| 173 |
a(i, 1, "Not deleteed"); |
| 174 |
a(mfn.deleteRef(3, 5, 7), false, "Delete #4"); |
| 175 |
a(mfn.deleteRef(3, 5, 7), true, "Delete final"); |
| 176 |
mfn(3, 5, 7); |
| 177 |
a(i, 2, "Restarted"); |
| 178 |
mfn(3, 5, 7); |
| 179 |
a(i, 2, "Cached again"); |
| 180 |
}, |
| 181 |
"Primitive: Async": function (a, d) { |
| 182 |
var mfn, fn, u = {}, i = 0; |
| 183 |
fn = function (x, y, cb) { |
| 184 |
nextTick(function () { |
| 185 |
++i; |
| 186 |
cb(null, x + y); |
| 187 |
}); |
| 188 |
return u; |
| 189 |
}; |
| 190 |
|
| 191 |
mfn = memoize(fn, { async: true, primitive: true, refCounter: true }); |
| 192 |
|
| 193 |
a(mfn.deleteRef(3, 7), null, "Delete ref before"); |
| 194 |
|
| 195 |
a( |
| 196 |
mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #1"); }), u, |
| 197 |
"Initial" |
| 198 |
); |
| 199 |
a( |
| 200 |
mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #2"); }), u, |
| 201 |
"Initial #2" |
| 202 |
); |
| 203 |
a( |
| 204 |
mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #1"); }), |
| 205 |
u, "Initial #2" |
| 206 |
); |
| 207 |
a( |
| 208 |
mfn(3, 7, function (err, res) { a.deep([err, res], [null, 10], "Result #3"); }), u, |
| 209 |
"Initial #2" |
| 210 |
); |
| 211 |
a( |
| 212 |
mfn(5, 8, function (err, res) { a.deep([err, res], [null, 13], "Result B #2"); }), |
| 213 |
u, "Initial #3" |
| 214 |
); |
| 215 |
|
| 216 |
nextTick(function () { |
| 217 |
a(i, 2, "Called #2"); |
| 218 |
|
| 219 |
a( |
| 220 |
mfn(3, 7, function (err, res) { |
| 221 |
a.deep([err, res], [null, 10], "Again: Result"); |
| 222 |
}), |
| 223 |
u, "Again: Initial" |
| 224 |
); |
| 225 |
a( |
| 226 |
mfn(5, 8, function (err, res) { |
| 227 |
a.deep([err, res], [null, 13], "Again B: Result"); |
| 228 |
}), |
| 229 |
u, "Again B: Initial" |
| 230 |
); |
| 231 |
|
| 232 |
nextTick(function () { |
| 233 |
a(i, 2, "Again Called #2"); |
| 234 |
|
| 235 |
a(mfn.deleteRef(3, 7), false, "Delete ref #1"); |
| 236 |
a(mfn.deleteRef(3, 7), false, "Delete ref #2"); |
| 237 |
a(mfn.deleteRef(3, 7), false, "Delete ref #3"); |
| 238 |
a(mfn.deleteRef(3, 7), true, "Delete ref Final"); |
| 239 |
|
| 240 |
a( |
| 241 |
mfn(3, 7, function (err, res) { |
| 242 |
a.deep([err, res], [null, 10], "Again: Result"); |
| 243 |
}), |
| 244 |
u, "Again: Initial" |
| 245 |
); |
| 246 |
a( |
| 247 |
mfn(5, 8, function (err, res) { |
| 248 |
a.deep([err, res], [null, 13], "Again B: Result"); |
| 249 |
}), |
| 250 |
u, "Again B: Initial" |
| 251 |
); |
| 252 |
|
| 253 |
nextTick(function () { |
| 254 |
a(i, 3, "Call After delete"); |
| 255 |
d(); |
| 256 |
}); |
| 257 |
}); |
| 258 |
}); |
| 259 |
}, |
| 260 |
"Primitive: Promise": function (a, d) { |
| 261 |
var mfn, fn, i = 0; |
| 262 |
fn = function (x, y) { |
| 263 |
++i; |
| 264 |
return new Promise(function (res) { res(x + y); }); |
| 265 |
}; |
| 266 |
|
| 267 |
mfn = memoize(fn, { promise: true, primitive: true, refCounter: true }); |
| 268 |
|
| 269 |
a(mfn.deleteRef(3, 7), null, "Delete ref before"); |
| 270 |
|
| 271 |
mfn(3, 7).done(function (res) { a(res, 10, "Result #1"); }); |
| 272 |
mfn(3, 7).done(function (res) { a(res, 10, "Result #2"); }); |
| 273 |
mfn(5, 8).done(function (res) { a(res, 13, "Result B #1"); }); |
| 274 |
mfn(3, 7).done(function (res) { a(res, 10, "Result #3"); }); |
| 275 |
mfn(5, 8).done(function (res) { a(res, 13, "Result B #2"); }); |
| 276 |
|
| 277 |
setTimeout(function () { |
| 278 |
a(i, 2, "Called #2"); |
| 279 |
|
| 280 |
mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }); |
| 281 |
mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }); |
| 282 |
|
| 283 |
setTimeout(function () { |
| 284 |
a(i, 2, "Again Called #2"); |
| 285 |
|
| 286 |
a(mfn.deleteRef(3, 7), false, "Delete ref #1"); |
| 287 |
a(mfn.deleteRef(3, 7), false, "Delete ref #2"); |
| 288 |
a(mfn.deleteRef(3, 7), false, "Delete ref #3"); |
| 289 |
a(mfn.deleteRef(3, 7), true, "Delete ref Final"); |
| 290 |
|
| 291 |
mfn(3, 7).done(function (res) { a(res, 10, "Again: Result"); }); |
| 292 |
mfn(5, 8).done(function (res) { a(res, 13, "Again B: Result"); }); |
| 293 |
|
| 294 |
setTimeout(function () { |
| 295 |
a(i, 3, "Call After delete"); |
| 296 |
d(); |
| 297 |
}, 10); |
| 298 |
}, 10); |
| 299 |
}, 10); |
| 300 |
}, |
| 301 |
}; |
| 302 |
}; |