@8ms/helpers 1.2.13 → 1.2.15

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.
@@ -5,19 +5,21 @@ 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'] ||
9
+ undefined === global['ems_cache']?.[key] ||
10
+ null === global['ems_cache']?.[key]) {
9
11
  // Throw error - should always exist
10
12
  throw `Global key '${key}' does not exist, requires initialisation.`;
11
13
  }
12
- // Reassign the global key
14
+ // Reassign the global key using the update function
13
15
  else {
14
- global[key] = {
15
- expires: global[key].expires,
16
+ global['ems_cache'][key] = {
17
+ expires: global['ems_cache'][key].expires,
16
18
  insertedAt: Date.now(),
17
- updateFn: global[key].updateFn,
18
- value: await global[key].updateFn(),
19
+ updateFn: global['ems_cache'][key].updateFn,
20
+ value: await global['ems_cache'][key].updateFn(),
19
21
  };
20
22
  }
21
- return global[key];
23
+ return global['ems_cache'][key];
22
24
  };
23
25
  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[key] = {
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;
@@ -0,0 +1,8 @@
1
+ type IsSet = {
2
+ key: string;
3
+ };
4
+ /**
5
+ * Checks to see if a global key is set.
6
+ */
7
+ declare const isSet: ({ key }: IsSet) => Promise<boolean>;
8
+ export default isSet;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Checks to see if a global key is set.
5
+ */
6
+ const isSet = async ({ key }) => {
7
+ return undefined === global['ems_cache'][key];
8
+ };
9
+ exports.default = isSet;
@@ -0,0 +1,8 @@
1
+ type MaxCache = {
2
+ limit: number;
3
+ };
4
+ /**
5
+ * Clear cache more than the provided number of cache.
6
+ */
7
+ declare const maxCache: ({ limit }: MaxCache) => void;
8
+ export default maxCache;
@@ -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,20 +9,22 @@ 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'] ||
13
+ undefined === global['ems_cache']?.[key] ||
14
+ null === global['ems_cache']?.[key]) {
13
15
  // Throw error - should always exist
14
16
  throw `Global key '${key}' does not exist, requires initialisation.`;
15
17
  }
16
18
  // Exists (0 is determined as forever)
17
- else if (0 != global[key].expires) {
19
+ else if (0 != global['ems_cache'][key].expires) {
18
20
  // https://ui.dev/get-current-timestamp-javascript
19
- const expiresAt = Math.floor(global[key].insertedAt / 1000) + global[key].expires;
21
+ const expiresAt = Math.floor(global['ems_cache'][key].insertedAt / 1000) + global[key].expires;
20
22
  const now = Math.floor(Date.now() / 1000);
21
23
  // If we are now beyond the expiry date, update the data
22
24
  if (now > expiresAt) {
23
25
  await (0, forceUpdate_1.default)({ key });
24
26
  }
25
27
  }
26
- return global[key].value;
28
+ return global['ems_cache'][key].value;
27
29
  };
28
30
  exports.default = read;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.2.13",
4
+ "version": "1.2.15",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"