@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.
@@ -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[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,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;
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.14",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"