|
1 | | -/* eslint id-length: 0, handle-callback-err: 0, no-undef: 0, no-unused-vars: 0, func-names: 0 */ |
| 1 | +/* eslint id-length: 0, handle-callback-err: 0, no-undef: 0, no-unused-vars: 0, func-names: 0, |
| 2 | +max-lines: 0 */ |
2 | 3 |
|
3 | 4 | "use strict"; |
4 | 5 |
|
5 | 6 | var memoize = require("../..") |
6 | 7 | , nextTick = require("next-tick") |
7 | | - , Promise = require("plain-promise"); |
| 8 | + , Promise = require("plain-promise") |
| 9 | + , Bluebird = require("bluebird"); |
| 10 | + |
| 11 | +Bluebird.config({ |
| 12 | + cancellation: true |
| 13 | +}); |
8 | 14 |
|
9 | 15 | module.exports = function () { |
10 | 16 | return { |
@@ -110,6 +116,90 @@ module.exports = function () { |
110 | 116 | }, 10); |
111 | 117 | } |
112 | 118 | }, |
| 119 | + "Cancellation": { |
| 120 | + Immediate: function (a, d) { |
| 121 | + var mfn, fn, i = 0; |
| 122 | + fn = function (x, y) { |
| 123 | + ++i; |
| 124 | + var p = new Bluebird(function (res) { |
| 125 | + setTimeout(function () { |
| 126 | + res(x + y); |
| 127 | + }, 100); |
| 128 | + }); |
| 129 | + p.cancel(); |
| 130 | + return p; |
| 131 | + }; |
| 132 | + |
| 133 | + mfn = memoize(fn, { promise: true }); |
| 134 | + |
| 135 | + mfn(3, 7).done(a.never, function (err) { |
| 136 | + a.throws(function () { |
| 137 | + throw err; |
| 138 | + }, Bluebird.CancellationError, "Result #1"); |
| 139 | + }); |
| 140 | + |
| 141 | + mfn(5, 8).done(a.never, function (err) { |
| 142 | + a.throws(function () { |
| 143 | + throw err; |
| 144 | + }, Bluebird.CancellationError, "Result B #2"); |
| 145 | + }); |
| 146 | + |
| 147 | + setTimeout(function () { |
| 148 | + a(i, 2, "Called #2"); |
| 149 | + |
| 150 | + mfn(3, 7).done(a.never, function (err) { |
| 151 | + a.throws(function () { |
| 152 | + throw err; |
| 153 | + }, Bluebird.CancellationError, "Again: Result"); |
| 154 | + }); |
| 155 | + |
| 156 | + mfn(5, 8).done(a.never, function (err) { |
| 157 | + a.throws(function () { |
| 158 | + throw err; |
| 159 | + }, Bluebird.CancellationError, "Again B: Result"); |
| 160 | + }); |
| 161 | + |
| 162 | + setTimeout(function (err) { |
| 163 | + a(i, 4, "Again Called #2"); |
| 164 | + d(); |
| 165 | + }, 10); |
| 166 | + }, 10); |
| 167 | + }, |
| 168 | + Delayed: function (a, d) { |
| 169 | + var mfn, fn, i = 0; |
| 170 | + fn = function (x, y) { |
| 171 | + ++i; |
| 172 | + var p = new Bluebird(function (res) { |
| 173 | + setTimeout(function () { |
| 174 | + res(x + y); |
| 175 | + }, 100); |
| 176 | + }); |
| 177 | + nextTick(function () { |
| 178 | + p.cancel(); |
| 179 | + }, 1); |
| 180 | + return p; |
| 181 | + }; |
| 182 | + |
| 183 | + mfn = memoize(fn, { promise: true }); |
| 184 | + |
| 185 | + mfn(3, 7).done(a.never, a.never); |
| 186 | + |
| 187 | + mfn(5, 8).done(a.never, a.never); |
| 188 | + |
| 189 | + setTimeout(function () { |
| 190 | + a(i, 2, "Called #2"); |
| 191 | + |
| 192 | + mfn(3, 7).done(a.never, a.never); |
| 193 | + |
| 194 | + mfn(5, 8).done(a.never, a.never); |
| 195 | + |
| 196 | + setTimeout(function (err) { |
| 197 | + a(i, 4, "Again Called #2"); |
| 198 | + d(); |
| 199 | + }, 500); |
| 200 | + }, 500); |
| 201 | + } |
| 202 | + }, |
113 | 203 | "Primitive": { |
114 | 204 | "Success": function (a, d) { |
115 | 205 | var mfn, fn, i = 0; |
|
0 commit comments