@sebspark/promise-cache 2.0.3 → 2.0.5

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
@@ -15,15 +15,14 @@ type SetParams<T> = {
15
15
  type PersistorConstructorType = {
16
16
  redis?: RedisClientOptions;
17
17
  clientId?: UUID;
18
- onError?: (c: string) => void;
19
- onSuccess?: (c: string) => void;
18
+ onError: (error: string) => void;
19
+ onSuccess: () => void;
20
20
  };
21
21
  declare class Persistor {
22
22
  client: ReturnType<typeof createClient> | null;
23
23
  private clientId?;
24
24
  private onError;
25
25
  private onSuccess;
26
- private isConnected;
27
26
  private readonly redis?;
28
27
  constructor({ redis, clientId, onSuccess, onError, }: PersistorConstructorType);
29
28
  connect(): Promise<void>;
@@ -41,7 +40,7 @@ type PromiseCacheOptions = {
41
40
  ttlInSeconds?: number;
42
41
  caseSensitive?: boolean;
43
42
  redis?: RedisClientOptions;
44
- onError?: () => void;
43
+ onError?: (error: string) => void;
45
44
  onSuccess?: () => void;
46
45
  };
47
46
  declare class PromiseCache<U> {
@@ -84,6 +83,7 @@ declare class PromiseCache<U> {
84
83
 
85
84
  declare class LocalStorage {
86
85
  client: Map<any, any>;
86
+ isReady: boolean;
87
87
  get(key: string): any;
88
88
  set(key: string, value: string, options?: {
89
89
  PX: number;
package/dist/index.d.ts CHANGED
@@ -15,15 +15,14 @@ type SetParams<T> = {
15
15
  type PersistorConstructorType = {
16
16
  redis?: RedisClientOptions;
17
17
  clientId?: UUID;
18
- onError?: (c: string) => void;
19
- onSuccess?: (c: string) => void;
18
+ onError: (error: string) => void;
19
+ onSuccess: () => void;
20
20
  };
21
21
  declare class Persistor {
22
22
  client: ReturnType<typeof createClient> | null;
23
23
  private clientId?;
24
24
  private onError;
25
25
  private onSuccess;
26
- private isConnected;
27
26
  private readonly redis?;
28
27
  constructor({ redis, clientId, onSuccess, onError, }: PersistorConstructorType);
29
28
  connect(): Promise<void>;
@@ -41,7 +40,7 @@ type PromiseCacheOptions = {
41
40
  ttlInSeconds?: number;
42
41
  caseSensitive?: boolean;
43
42
  redis?: RedisClientOptions;
44
- onError?: () => void;
43
+ onError?: (error: string) => void;
45
44
  onSuccess?: () => void;
46
45
  };
47
46
  declare class PromiseCache<U> {
@@ -84,6 +83,7 @@ declare class PromiseCache<U> {
84
83
 
85
84
  declare class LocalStorage {
86
85
  client: Map<any, any>;
86
+ isReady: boolean;
87
87
  get(key: string): any;
88
88
  set(key: string, value: string, options?: {
89
89
  PX: number;
package/dist/index.js CHANGED
@@ -37,6 +37,7 @@ var import_redis = require("redis");
37
37
  // src/localMemory.ts
38
38
  var LocalStorage = class {
39
39
  client = /* @__PURE__ */ new Map();
40
+ isReady = false;
40
41
  get(key) {
41
42
  return this.client.get(key);
42
43
  }
@@ -59,8 +60,9 @@ var LocalStorage = class {
59
60
  }
60
61
  // This is just for testing
61
62
  on(event, callback) {
62
- if (event === "connect" && callback) {
63
- callback("connect");
63
+ if (event === "ready" && callback) {
64
+ this.isReady = true;
65
+ callback("ready");
64
66
  }
65
67
  return this;
66
68
  }
@@ -81,7 +83,6 @@ var Persistor = class {
81
83
  clientId;
82
84
  onError;
83
85
  onSuccess;
84
- isConnected = false;
85
86
  redis;
86
87
  constructor({
87
88
  redis,
@@ -97,7 +98,7 @@ var Persistor = class {
97
98
  } else {
98
99
  CACHE_CLIENT = createLocalMemoryClient;
99
100
  }
100
- if (!this.isConnected) {
101
+ if (!this.client || !this.client.isReady) {
101
102
  this.connect();
102
103
  }
103
104
  }
@@ -106,46 +107,28 @@ var Persistor = class {
106
107
  interval: (x) => {
107
108
  return x * 2 * 1e3;
108
109
  },
109
- maxRetries: 5,
110
+ maxRetries: 3,
110
111
  retryCondition: () => {
111
112
  var _a;
112
- console.log(`Trying to connect: ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`);
113
+ console.log(
114
+ `Connecting to redis... ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`
115
+ );
113
116
  return true;
114
117
  }
115
118
  };
116
119
  await (0, import_retry.retry)(() => this.startConnection(), settings);
117
120
  }
118
- async startConnection() {
119
- var _a, _b;
120
- try {
121
- this.client = CACHE_CLIENT(this.redis);
122
- this.client.on("error", (err) => {
123
- var _a2, _b2;
124
- this.isConnected = false;
125
- if (this.onError) {
126
- this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
127
- }
128
- throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
129
- });
130
- this.client.on("connect", () => {
131
- var _a2, _b2, _c, _d;
132
- this.isConnected = true;
133
- if (this.onSuccess) {
134
- this.onSuccess(
135
- `\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
136
- );
137
- }
138
- console.log(
139
- `\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
140
- );
121
+ startConnection() {
122
+ return new Promise((resolve, reject) => {
123
+ this.client = CACHE_CLIENT(this.redis).on("error", (err) => {
124
+ this.onError(err);
125
+ reject();
126
+ }).on("ready", () => {
127
+ this.onSuccess();
128
+ resolve(true);
141
129
  });
142
- return await this.client.connect();
143
- } catch (err) {
144
- if (this.onError) {
145
- this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
146
- }
147
- throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
148
- }
130
+ return this.client.connect();
131
+ });
149
132
  }
150
133
  async size() {
151
134
  if (!this.client) {
@@ -171,7 +154,8 @@ var Persistor = class {
171
154
  return this.clientId;
172
155
  }
173
156
  getIsClientConnected() {
174
- return this.isConnected;
157
+ var _a;
158
+ return !!((_a = this.client) == null ? void 0 : _a.isReady);
175
159
  }
176
160
  createOptions(ttl) {
177
161
  if (ttl !== null && ttl !== void 0) {
@@ -180,7 +164,7 @@ var Persistor = class {
180
164
  return {};
181
165
  }
182
166
  async set(key, { value, timestamp, ttl }) {
183
- if (!this.isConnected || !this.client) {
167
+ if (!this.client) {
184
168
  throw new Error("Client not connected");
185
169
  }
186
170
  try {
@@ -192,7 +176,7 @@ var Persistor = class {
192
176
  }
193
177
  }
194
178
  async delete(key) {
195
- if (!this.isConnected || !this.client) {
179
+ if (!this.client) {
196
180
  throw new Error("Client not connected");
197
181
  }
198
182
  try {
@@ -211,12 +195,22 @@ var getPersistor = ({
211
195
  onSuccess,
212
196
  clientId
213
197
  }) => {
214
- const connectionName = (redis == null ? void 0 : redis.name) || "default";
198
+ const connectionName = redis ? (redis == null ? void 0 : redis.name) || "default" : "local";
215
199
  if (!persistors[connectionName]) {
216
200
  persistors[connectionName] = new Persistor({
217
201
  redis,
218
- onError,
219
- onSuccess,
202
+ onError: (error) => {
203
+ onError == null ? void 0 : onError(error);
204
+ console.error(
205
+ `\u274C REDIS | Client Error | ${connectionName} | ${redis == null ? void 0 : redis.url}: ${error}`
206
+ );
207
+ },
208
+ onSuccess: () => {
209
+ onSuccess == null ? void 0 : onSuccess();
210
+ console.log(
211
+ `\u{1F4E6} REDIS | Connection Ready | ${connectionName} | ${redis == null ? void 0 : redis.url}`
212
+ );
213
+ },
220
214
  clientId
221
215
  });
222
216
  }
package/dist/index.mjs CHANGED
@@ -8,6 +8,7 @@ import { createClient } from "redis";
8
8
  // src/localMemory.ts
9
9
  var LocalStorage = class {
10
10
  client = /* @__PURE__ */ new Map();
11
+ isReady = false;
11
12
  get(key) {
12
13
  return this.client.get(key);
13
14
  }
@@ -30,8 +31,9 @@ var LocalStorage = class {
30
31
  }
31
32
  // This is just for testing
32
33
  on(event, callback) {
33
- if (event === "connect" && callback) {
34
- callback("connect");
34
+ if (event === "ready" && callback) {
35
+ this.isReady = true;
36
+ callback("ready");
35
37
  }
36
38
  return this;
37
39
  }
@@ -52,7 +54,6 @@ var Persistor = class {
52
54
  clientId;
53
55
  onError;
54
56
  onSuccess;
55
- isConnected = false;
56
57
  redis;
57
58
  constructor({
58
59
  redis,
@@ -68,7 +69,7 @@ var Persistor = class {
68
69
  } else {
69
70
  CACHE_CLIENT = createLocalMemoryClient;
70
71
  }
71
- if (!this.isConnected) {
72
+ if (!this.client || !this.client.isReady) {
72
73
  this.connect();
73
74
  }
74
75
  }
@@ -77,46 +78,28 @@ var Persistor = class {
77
78
  interval: (x) => {
78
79
  return x * 2 * 1e3;
79
80
  },
80
- maxRetries: 5,
81
+ maxRetries: 3,
81
82
  retryCondition: () => {
82
83
  var _a;
83
- console.log(`Trying to connect: ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`);
84
+ console.log(
85
+ `Connecting to redis... ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`
86
+ );
84
87
  return true;
85
88
  }
86
89
  };
87
90
  await retry(() => this.startConnection(), settings);
88
91
  }
89
- async startConnection() {
90
- var _a, _b;
91
- try {
92
- this.client = CACHE_CLIENT(this.redis);
93
- this.client.on("error", (err) => {
94
- var _a2, _b2;
95
- this.isConnected = false;
96
- if (this.onError) {
97
- this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
98
- }
99
- throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
100
- });
101
- this.client.on("connect", () => {
102
- var _a2, _b2, _c, _d;
103
- this.isConnected = true;
104
- if (this.onSuccess) {
105
- this.onSuccess(
106
- `\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
107
- );
108
- }
109
- console.log(
110
- `\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
111
- );
92
+ startConnection() {
93
+ return new Promise((resolve, reject) => {
94
+ this.client = CACHE_CLIENT(this.redis).on("error", (err) => {
95
+ this.onError(err);
96
+ reject();
97
+ }).on("ready", () => {
98
+ this.onSuccess();
99
+ resolve(true);
112
100
  });
113
- return await this.client.connect();
114
- } catch (err) {
115
- if (this.onError) {
116
- this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
117
- }
118
- throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
119
- }
101
+ return this.client.connect();
102
+ });
120
103
  }
121
104
  async size() {
122
105
  if (!this.client) {
@@ -142,7 +125,8 @@ var Persistor = class {
142
125
  return this.clientId;
143
126
  }
144
127
  getIsClientConnected() {
145
- return this.isConnected;
128
+ var _a;
129
+ return !!((_a = this.client) == null ? void 0 : _a.isReady);
146
130
  }
147
131
  createOptions(ttl) {
148
132
  if (ttl !== null && ttl !== void 0) {
@@ -151,7 +135,7 @@ var Persistor = class {
151
135
  return {};
152
136
  }
153
137
  async set(key, { value, timestamp, ttl }) {
154
- if (!this.isConnected || !this.client) {
138
+ if (!this.client) {
155
139
  throw new Error("Client not connected");
156
140
  }
157
141
  try {
@@ -163,7 +147,7 @@ var Persistor = class {
163
147
  }
164
148
  }
165
149
  async delete(key) {
166
- if (!this.isConnected || !this.client) {
150
+ if (!this.client) {
167
151
  throw new Error("Client not connected");
168
152
  }
169
153
  try {
@@ -182,12 +166,22 @@ var getPersistor = ({
182
166
  onSuccess,
183
167
  clientId
184
168
  }) => {
185
- const connectionName = (redis == null ? void 0 : redis.name) || "default";
169
+ const connectionName = redis ? (redis == null ? void 0 : redis.name) || "default" : "local";
186
170
  if (!persistors[connectionName]) {
187
171
  persistors[connectionName] = new Persistor({
188
172
  redis,
189
- onError,
190
- onSuccess,
173
+ onError: (error) => {
174
+ onError == null ? void 0 : onError(error);
175
+ console.error(
176
+ `\u274C REDIS | Client Error | ${connectionName} | ${redis == null ? void 0 : redis.url}: ${error}`
177
+ );
178
+ },
179
+ onSuccess: () => {
180
+ onSuccess == null ? void 0 : onSuccess();
181
+ console.log(
182
+ `\u{1F4E6} REDIS | Connection Ready | ${connectionName} | ${redis == null ? void 0 : redis.url}`
183
+ );
184
+ },
191
185
  clientId
192
186
  });
193
187
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",