@8ms/helpers 1.2.13 → 1.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/global/forceUpdate.js +7 -7
- package/global/init.js +5 -2
- package/global/isSet.d.ts +8 -0
- package/global/isSet.js +9 -0
- package/global/maxCache.d.ts +8 -0
- package/global/maxCache.js +17 -0
- package/global/read.js +3 -3
- package/package.json +1 -1
package/global/forceUpdate.js
CHANGED
|
@@ -5,19 +5,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
5
5
|
*/
|
|
6
6
|
const forceUpdate = async ({ key }) => {
|
|
7
7
|
// Doesn't exist
|
|
8
|
-
if (undefined === global[key] || null === global[key]) {
|
|
8
|
+
if (undefined === global['ems_cache'][key] || null === global['ems_cache'][key]) {
|
|
9
9
|
// Throw error - should always exist
|
|
10
10
|
throw `Global key '${key}' does not exist, requires initialisation.`;
|
|
11
11
|
}
|
|
12
|
-
// Reassign the global key
|
|
12
|
+
// Reassign the global key using the update function
|
|
13
13
|
else {
|
|
14
|
-
global[key] = {
|
|
15
|
-
expires: global[key].expires,
|
|
14
|
+
global['ems_cache'][key] = {
|
|
15
|
+
expires: global['ems_cache'][key].expires,
|
|
16
16
|
insertedAt: Date.now(),
|
|
17
|
-
updateFn: global[key].updateFn,
|
|
18
|
-
value: await global[key].updateFn(),
|
|
17
|
+
updateFn: global['ems_cache'][key].updateFn,
|
|
18
|
+
value: await global['ems_cache'][key].updateFn(),
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
-
return global[key];
|
|
21
|
+
return global['ems_cache'][key];
|
|
22
22
|
};
|
|
23
23
|
exports.default = forceUpdate;
|
package/global/init.js
CHANGED
|
@@ -4,12 +4,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
* Initialise a new global data.
|
|
5
5
|
*/
|
|
6
6
|
const init = async ({ expires, key, updateFn }) => {
|
|
7
|
-
global[
|
|
7
|
+
if (undefined === global['ems_cache']) {
|
|
8
|
+
global['ems_cache'] = {};
|
|
9
|
+
}
|
|
10
|
+
global['ems_cache'][key] = {
|
|
8
11
|
expires,
|
|
9
12
|
insertedAt: Date.now(),
|
|
10
13
|
updateFn,
|
|
11
14
|
value: await updateFn(),
|
|
12
15
|
};
|
|
13
|
-
return global[key];
|
|
16
|
+
return global['ems_cache'][key];
|
|
14
17
|
};
|
|
15
18
|
exports.default = init;
|
package/global/isSet.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Clear cache more than the provided number of cache.
|
|
5
|
+
*/
|
|
6
|
+
const maxCache = ({ limit }) => {
|
|
7
|
+
if (undefined !== global['ems_cache']) {
|
|
8
|
+
let counter = 0;
|
|
9
|
+
for (let cacheKey in global['ems_cache']) {
|
|
10
|
+
if (counter > limit) {
|
|
11
|
+
delete global['ems_cache'][cacheKey];
|
|
12
|
+
}
|
|
13
|
+
counter++;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
exports.default = maxCache;
|
package/global/read.js
CHANGED
|
@@ -9,12 +9,12 @@ const forceUpdate_1 = __importDefault(require("./forceUpdate"));
|
|
|
9
9
|
*/
|
|
10
10
|
const read = async ({ key }) => {
|
|
11
11
|
// Doesn't exist
|
|
12
|
-
if (undefined === global[key] || null === global[key]) {
|
|
12
|
+
if (undefined === global['ems_cache'][key] || null === global['ems_cache'][key]) {
|
|
13
13
|
// Throw error - should always exist
|
|
14
14
|
throw `Global key '${key}' does not exist, requires initialisation.`;
|
|
15
15
|
}
|
|
16
16
|
// Exists (0 is determined as forever)
|
|
17
|
-
else if (0 != global[key].expires) {
|
|
17
|
+
else if (0 != global['ems_cache'][key].expires) {
|
|
18
18
|
// https://ui.dev/get-current-timestamp-javascript
|
|
19
19
|
const expiresAt = Math.floor(global[key].insertedAt / 1000) + global[key].expires;
|
|
20
20
|
const now = Math.floor(Date.now() / 1000);
|
|
@@ -23,6 +23,6 @@ const read = async ({ key }) => {
|
|
|
23
23
|
await (0, forceUpdate_1.default)({ key });
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
-
return global[key].value;
|
|
26
|
+
return global['ems_cache'][key].value;
|
|
27
27
|
};
|
|
28
28
|
exports.default = read;
|