@sebspark/promise-cache 2.0.0 → 2.0.2

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
@@ -22,6 +22,7 @@ declare class Persistor {
22
22
  private clientId;
23
23
  private onError;
24
24
  private onSuccess;
25
+ private isConnected;
25
26
  private readonly redis?;
26
27
  constructor(options: PersistorConstructorType);
27
28
  connect(): Promise<void>;
@@ -29,6 +30,7 @@ declare class Persistor {
29
30
  size(): Promise<number>;
30
31
  get<T>(key: string): Promise<GetType<T> | null>;
31
32
  getClientId(): UUID;
33
+ getIsClientConnected(): boolean;
32
34
  private createOptions;
33
35
  set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
34
36
  delete(key: string): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -22,6 +22,7 @@ declare class Persistor {
22
22
  private clientId;
23
23
  private onError;
24
24
  private onSuccess;
25
+ private isConnected;
25
26
  private readonly redis?;
26
27
  constructor(options: PersistorConstructorType);
27
28
  connect(): Promise<void>;
@@ -29,6 +30,7 @@ declare class Persistor {
29
30
  size(): Promise<number>;
30
31
  get<T>(key: string): Promise<GetType<T> | null>;
31
32
  getClientId(): UUID;
33
+ getIsClientConnected(): boolean;
32
34
  private createOptions;
33
35
  set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
34
36
  delete(key: string): Promise<void>;
package/dist/index.js CHANGED
@@ -81,6 +81,7 @@ var Persistor = class {
81
81
  clientId = (0, import_node_crypto.randomUUID)();
82
82
  onError;
83
83
  onSuccess;
84
+ isConnected = false;
84
85
  redis;
85
86
  constructor(options) {
86
87
  const { redis, onError, onSuccess } = options;
@@ -91,7 +92,9 @@ var Persistor = class {
91
92
  } else {
92
93
  CACHE_CLIENT = createLocalMemoryClient;
93
94
  }
94
- this.connect();
95
+ if (!this.isConnected) {
96
+ this.connect();
97
+ }
95
98
  }
96
99
  async connect() {
97
100
  const settings = {
@@ -100,7 +103,8 @@ var Persistor = class {
100
103
  },
101
104
  maxRetries: 5,
102
105
  retryCondition: () => {
103
- console.log("Trying to connect!");
106
+ var _a;
107
+ console.log(`Trying to connect: ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`);
104
108
  return true;
105
109
  }
106
110
  };
@@ -112,17 +116,23 @@ var Persistor = class {
112
116
  this.client = CACHE_CLIENT(this.redis);
113
117
  this.client.on("error", (err) => {
114
118
  var _a2, _b2;
119
+ this.isConnected = false;
115
120
  if (this.onError) {
116
121
  this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
117
122
  }
118
123
  throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
119
124
  });
120
125
  this.client.on("connect", () => {
121
- var _a2, _b2;
126
+ var _a2, _b2, _c, _d;
127
+ this.isConnected = true;
122
128
  if (this.onSuccess) {
123
- this.onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
129
+ this.onSuccess(
130
+ `\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
131
+ );
124
132
  }
125
- console.log(`\u{1F4E6} REDIS | Connection Ready | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`);
133
+ console.log(
134
+ `\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
135
+ );
126
136
  });
127
137
  return await this.client.connect();
128
138
  } catch (err) {
@@ -155,6 +165,9 @@ var Persistor = class {
155
165
  getClientId() {
156
166
  return this.clientId;
157
167
  }
168
+ getIsClientConnected() {
169
+ return this.isConnected;
170
+ }
158
171
  createOptions(ttl) {
159
172
  if (ttl !== null && ttl !== void 0) {
160
173
  return { PX: Math.round(ttl) };
package/dist/index.mjs CHANGED
@@ -50,6 +50,7 @@ var Persistor = class {
50
50
  clientId = randomUUID();
51
51
  onError;
52
52
  onSuccess;
53
+ isConnected = false;
53
54
  redis;
54
55
  constructor(options) {
55
56
  const { redis, onError, onSuccess } = options;
@@ -60,7 +61,9 @@ var Persistor = class {
60
61
  } else {
61
62
  CACHE_CLIENT = createLocalMemoryClient;
62
63
  }
63
- this.connect();
64
+ if (!this.isConnected) {
65
+ this.connect();
66
+ }
64
67
  }
65
68
  async connect() {
66
69
  const settings = {
@@ -69,7 +72,8 @@ var Persistor = class {
69
72
  },
70
73
  maxRetries: 5,
71
74
  retryCondition: () => {
72
- console.log("Trying to connect!");
75
+ var _a;
76
+ console.log(`Trying to connect: ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`);
73
77
  return true;
74
78
  }
75
79
  };
@@ -81,17 +85,23 @@ var Persistor = class {
81
85
  this.client = CACHE_CLIENT(this.redis);
82
86
  this.client.on("error", (err) => {
83
87
  var _a2, _b2;
88
+ this.isConnected = false;
84
89
  if (this.onError) {
85
90
  this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
86
91
  }
87
92
  throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
88
93
  });
89
94
  this.client.on("connect", () => {
90
- var _a2, _b2;
95
+ var _a2, _b2, _c, _d;
96
+ this.isConnected = true;
91
97
  if (this.onSuccess) {
92
- this.onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
98
+ this.onSuccess(
99
+ `\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
100
+ );
93
101
  }
94
- console.log(`\u{1F4E6} REDIS | Connection Ready | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`);
102
+ console.log(
103
+ `\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
104
+ );
95
105
  });
96
106
  return await this.client.connect();
97
107
  } catch (err) {
@@ -124,6 +134,9 @@ var Persistor = class {
124
134
  getClientId() {
125
135
  return this.clientId;
126
136
  }
137
+ getIsClientConnected() {
138
+ return this.isConnected;
139
+ }
127
140
  createOptions(ttl) {
128
141
  if (ttl !== null && ttl !== void 0) {
129
142
  return { PX: Math.round(ttl) };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",