ti2 1.0.66 → 1.0.68

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/cache.js CHANGED
@@ -11,12 +11,13 @@ const save = async ({
11
11
  value,
12
12
  ttl = defaultTTL,
13
13
  }) => {
14
- const key = `${pluginName}:${keyParam}`;
15
- const keyType = typeof value === 'string';
16
- await cache.set(key, JSON.stringify({
17
- keyType,
18
- value: keyType === 'string' ? value : JSON.stringify(value),
19
- }));
14
+ const key = `${pluginName}:${(() => {
15
+ if (typeof keyParam !== 'string') {
16
+ return hash(keyParam);
17
+ }
18
+ return keyParam;
19
+ })()}`;
20
+ await cache.set(key, JSON.stringify(value));
20
21
  await cache.expire(key, ttl);
21
22
  };
22
23
 
@@ -24,14 +25,15 @@ const get = async ({
24
25
  pluginName,
25
26
  key: keyParam,
26
27
  }) => {
27
- const key = `${pluginName}:${keyParam}`;
28
+ const key = `${pluginName}:${(() => {
29
+ if (typeof keyParam !== 'string') {
30
+ return hash(keyParam);
31
+ }
32
+ return keyParam;
33
+ })()}`;
28
34
  const storeVal = await cache.get(key);
29
35
  if (!storeVal) return storeVal;
30
- const { keyType, value } = JSON.parse(storeVal);
31
- if (keyType === 'string') {
32
- return value;
33
- }
34
- return JSON.parse(value);
36
+ return JSON.parse(storeVal);
35
37
  };
36
38
 
37
39
  const getOrExec = async ({
@@ -41,7 +43,15 @@ const getOrExec = async ({
41
43
  fn,
42
44
  fnParams,
43
45
  }) => {
44
- const key = keyParam || hash({ fn, fnParams });
46
+ const key = (() => {
47
+ if (!keyParam) {
48
+ return hash({ fn, fnParams });
49
+ }
50
+ if (typeof keyParam !== 'string') {
51
+ return hash(keyParam);
52
+ }
53
+ return keyParam;
54
+ })();
45
55
  let value = await get({ pluginName, key });
46
56
  if (value !== null) return value;
47
57
  value = await fn(...fnParams);
@@ -60,7 +70,15 @@ const drop = async ({
60
70
  fn,
61
71
  fnParams,
62
72
  }) => {
63
- let key = keyParam || hash({ fn, fnParams });
73
+ const key = `${pluginName}:${(() => {
74
+ if (!keyParam) {
75
+ return hash({ fn, fnParams });
76
+ }
77
+ if (typeof keyParam !== 'string') {
78
+ return hash(keyParam);
79
+ }
80
+ return keyParam;
81
+ })()}`;
64
82
  key = `${pluginName}:${key}`;
65
83
  await cache.del(key);
66
84
  };
@@ -24,6 +24,7 @@ const getAllFuncs = toCheck => {
24
24
 
25
25
  return props.sort().filter((e, i, arr) => {
26
26
  try {
27
+ if (e === 'cache') return false;
27
28
  if (e !== arr[i + 1] && typeof toCheck[e] === 'function') return true;
28
29
  } catch {
29
30
  return undefined;
package/index.js CHANGED
@@ -79,8 +79,14 @@ module.exports = async ({
79
79
  params[nuName] = value;
80
80
  });
81
81
  const pluginInstance = await new Plugin({
82
- name: pluginName,
82
+ cache: {
83
+ drop: args => cache.getOrExec({ ...args, pluginName }),
84
+ get: args => cache.getOrExec({ ...args, pluginName }),
85
+ getOrExec: args => cache.getOrExec({ ...args, pluginName }),
86
+ save: args => cache.getOrExec({ ...args, pluginName }),
87
+ },
83
88
  events: ti2Events,
89
+ name: pluginName,
84
90
  ...params,
85
91
  });
86
92
  return pluginInstance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2",
3
- "version": "1.0.66",
3
+ "version": "1.0.68",
4
4
  "description": "Tourist Industry Exchange (TI2)",
5
5
  "main": "index.js",
6
6
  "scripts": {