@sebspark/promise-cache 1.3.1 → 1.4.0

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/dist/index.d.mts CHANGED
@@ -13,14 +13,17 @@ type SetParams<T> = {
13
13
  };
14
14
  type PersistorConstructorType = {
15
15
  redis?: RedisClientOptions;
16
- onError?: () => void;
17
- onSuccess?: () => void;
16
+ onError?: (c: string) => void;
17
+ onSuccess?: (c: string) => void;
18
18
  };
19
19
  declare class Persistor {
20
20
  client: ReturnType<typeof createClient> | null;
21
+ private onError;
22
+ private onSuccess;
21
23
  private readonly redis?;
22
24
  constructor(options: PersistorConstructorType);
23
- connect(onError?: (message: string) => void, onSuccess?: (message: string) => void): Promise<void>;
25
+ connect(): Promise<void>;
26
+ startConnection(): Promise<unknown>;
24
27
  size(): Promise<number>;
25
28
  get<T>(key: string): Promise<GetType<T> | null>;
26
29
  private createOptions;
package/dist/index.d.ts CHANGED
@@ -13,14 +13,17 @@ type SetParams<T> = {
13
13
  };
14
14
  type PersistorConstructorType = {
15
15
  redis?: RedisClientOptions;
16
- onError?: () => void;
17
- onSuccess?: () => void;
16
+ onError?: (c: string) => void;
17
+ onSuccess?: (c: string) => void;
18
18
  };
19
19
  declare class Persistor {
20
20
  client: ReturnType<typeof createClient> | null;
21
+ private onError;
22
+ private onSuccess;
21
23
  private readonly redis?;
22
24
  constructor(options: PersistorConstructorType);
23
- connect(onError?: (message: string) => void, onSuccess?: (message: string) => void): Promise<void>;
25
+ connect(): Promise<void>;
26
+ startConnection(): Promise<unknown>;
24
27
  size(): Promise<number>;
25
28
  get<T>(key: string): Promise<GetType<T> | null>;
26
29
  private createOptions;
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ __export(src_exports, {
30
30
  module.exports = __toCommonJS(src_exports);
31
31
 
32
32
  // src/persistor.ts
33
+ var import_retry = require("@sebspark/retry");
33
34
  var import_redis = require("redis");
34
35
 
35
36
  // src/localMemory.ts
@@ -76,48 +77,57 @@ var CACHE_CLIENT = import_redis.createClient;
76
77
  var isTestRunning = process.env.NODE_ENV === "test";
77
78
  var Persistor = class {
78
79
  client = null;
80
+ onError;
81
+ onSuccess;
79
82
  redis;
80
83
  constructor(options) {
81
84
  const { redis, onError, onSuccess } = options;
85
+ this.onError = onError;
86
+ this.onSuccess = onSuccess;
82
87
  if (redis && !isTestRunning) {
83
88
  this.redis = redis;
84
89
  } else {
85
90
  CACHE_CLIENT = createLocalMemoryClient;
86
91
  }
87
- this.connect(onError, onSuccess);
92
+ this.connect();
88
93
  }
89
- async connect(onError, onSuccess) {
94
+ async connect() {
95
+ const settings = {
96
+ interval: (x) => {
97
+ return x * 2 * 1e3;
98
+ },
99
+ maxRetries: 5,
100
+ retryCondition: () => {
101
+ console.log("Trying to connect!");
102
+ return true;
103
+ }
104
+ };
105
+ await (0, import_retry.retry)(() => this.startConnection(), settings);
106
+ }
107
+ async startConnection() {
90
108
  var _a, _b;
91
109
  try {
92
110
  this.client = CACHE_CLIENT(this.redis);
93
111
  this.client.on("error", (err) => {
94
112
  var _a2, _b2;
95
- if (onError) {
96
- onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
97
- } else {
98
- console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
113
+ if (this.onError) {
114
+ this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
99
115
  }
116
+ throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
100
117
  });
101
- this.client.connect();
102
- await new Promise((resolve, reject) => {
103
- if (!this.client) {
104
- reject("Client not initialized");
105
- return;
118
+ this.client.on("connect", () => {
119
+ var _a2, _b2;
120
+ if (this.onSuccess) {
121
+ this.onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
106
122
  }
107
- this.client.on("connect", () => {
108
- var _a2;
109
- if (onSuccess) {
110
- onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
111
- }
112
- resolve(true);
113
- });
123
+ console.log(`\u{1F4E6} REDIS | Connection Ready | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`);
114
124
  });
125
+ return await this.client.connect();
115
126
  } catch (err) {
116
- if (onError) {
117
- onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
118
- } else {
119
- console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
127
+ if (this.onError) {
128
+ this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
120
129
  }
130
+ throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
121
131
  }
122
132
  }
123
133
  async size() {
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  // src/persistor.ts
2
+ import { retry } from "@sebspark/retry";
2
3
  import { createClient } from "redis";
3
4
 
4
5
  // src/localMemory.ts
@@ -45,48 +46,57 @@ var CACHE_CLIENT = createClient;
45
46
  var isTestRunning = process.env.NODE_ENV === "test";
46
47
  var Persistor = class {
47
48
  client = null;
49
+ onError;
50
+ onSuccess;
48
51
  redis;
49
52
  constructor(options) {
50
53
  const { redis, onError, onSuccess } = options;
54
+ this.onError = onError;
55
+ this.onSuccess = onSuccess;
51
56
  if (redis && !isTestRunning) {
52
57
  this.redis = redis;
53
58
  } else {
54
59
  CACHE_CLIENT = createLocalMemoryClient;
55
60
  }
56
- this.connect(onError, onSuccess);
61
+ this.connect();
57
62
  }
58
- async connect(onError, onSuccess) {
63
+ async connect() {
64
+ const settings = {
65
+ interval: (x) => {
66
+ return x * 2 * 1e3;
67
+ },
68
+ maxRetries: 5,
69
+ retryCondition: () => {
70
+ console.log("Trying to connect!");
71
+ return true;
72
+ }
73
+ };
74
+ await retry(() => this.startConnection(), settings);
75
+ }
76
+ async startConnection() {
59
77
  var _a, _b;
60
78
  try {
61
79
  this.client = CACHE_CLIENT(this.redis);
62
80
  this.client.on("error", (err) => {
63
81
  var _a2, _b2;
64
- if (onError) {
65
- onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
66
- } else {
67
- console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
82
+ if (this.onError) {
83
+ this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
68
84
  }
85
+ throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
69
86
  });
70
- this.client.connect();
71
- await new Promise((resolve, reject) => {
72
- if (!this.client) {
73
- reject("Client not initialized");
74
- return;
87
+ this.client.on("connect", () => {
88
+ var _a2, _b2;
89
+ if (this.onSuccess) {
90
+ this.onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
75
91
  }
76
- this.client.on("connect", () => {
77
- var _a2;
78
- if (onSuccess) {
79
- onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
80
- }
81
- resolve(true);
82
- });
92
+ console.log(`\u{1F4E6} REDIS | Connection Ready | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`);
83
93
  });
94
+ return await this.client.connect();
84
95
  } catch (err) {
85
- if (onError) {
86
- onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
87
- } else {
88
- console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
96
+ if (this.onError) {
97
+ this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
89
98
  }
99
+ throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
90
100
  }
91
101
  }
92
102
  async size() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -19,6 +19,7 @@
19
19
  "tsconfig": "*"
20
20
  },
21
21
  "dependencies": {
22
- "redis": "4.6.14"
22
+ "redis": "4.6.14",
23
+ "@sebspark/retry": "*"
23
24
  }
24
25
  }