Skip to content

Commit 06a56f0

Browse files
committed
fixes #1
1 parent 8e6dd90 commit 06a56f0

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

temporalcache.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ const toProperCase = (st) =>
1919
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(),
2020
);
2121

22+
let TEMPORAL_CACHE_GLOBAL_DISABLE = false;
23+
24+
const disable = () => {
25+
TEMPORAL_CACHE_GLOBAL_DISABLE = true;
26+
};
27+
const enable = () => {
28+
TEMPORAL_CACHE_GLOBAL_DISABLE = false;
29+
};
30+
2231
/**
2332
* @param {date} last last datetime
2433
* @param {date} now current datetime
@@ -191,7 +200,8 @@ const expire = (options) => {
191200
day_of_week,
192201
week,
193202
month,
194-
)
203+
) ||
204+
TEMPORAL_CACHE_GLOBAL_DISABLE
195205
) {
196206
const val = foo(...args);
197207
cache.set(key, val);
@@ -241,7 +251,8 @@ const interval = (options) => {
241251

242252
if (
243253
(now - last) / 1000 >
244-
calc({ seconds, minutes, hours, days, weeks, months, years })
254+
calc({ seconds, minutes, hours, days, weeks, months, years }) ||
255+
TEMPORAL_CACHE_GLOBAL_DISABLE
245256
) {
246257
const val = foo(...args);
247258
cache[args] = val;
@@ -264,6 +275,9 @@ module.exports = {
264275
TCException,
265276
should_expire,
266277
calc,
278+
TEMPORAL_CACHE_GLOBAL_DISABLE,
279+
enable,
280+
disable,
267281
// expire
268282
expire,
269283
expire_minutely,

test/expire.test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* the Apache License 2.0. The full license can be found in the LICENSE file.
77
*
88
*/
9-
const {expire} = require("../temporalcache");
9+
const {expire, disable, enable} = require("../temporalcache");
1010

1111

1212
describe("Expire tests", () => {
@@ -40,4 +40,43 @@ describe("Expire tests", () => {
4040

4141
global.Date = realDate;
4242
});
43+
44+
test("test disable", () => {
45+
let now = new Date(2018, 1, 1, 1, 1, 1);
46+
let later = new Date(2018, 1, 1, 1, 2, 0);
47+
let later2 = new Date(2018, 1, 1, 1, 2, 1);
48+
49+
// mock date
50+
const realDate = Date;
51+
global.Date = class extends Date {
52+
constructor() {
53+
return now;
54+
}
55+
};
56+
57+
const foo = expire({second: 1})(() => {
58+
return Math.random();
59+
});
60+
61+
let x = foo();
62+
expect(x).toBe(foo());
63+
64+
disable();
65+
expect(x !== foo()).toBe(true);
66+
67+
enable();
68+
x = foo();
69+
expect(x === foo()).toBe(true);
70+
71+
72+
now = later;
73+
74+
expect(x === foo()).toBe(true);
75+
76+
now = later2;
77+
78+
expect(x !== foo()).toBe(true);
79+
80+
global.Date = realDate;
81+
});
4382
});

0 commit comments

Comments
 (0)