@sebspark/promise-cache 2.0.4 → 2.0.6

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
@@ -23,7 +23,6 @@ declare class Persistor {
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>;
@@ -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
@@ -23,7 +23,6 @@ declare class Persistor {
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>;
@@ -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
@@ -31,12 +31,12 @@ module.exports = __toCommonJS(src_exports);
31
31
  var import_node_crypto = require("crypto");
32
32
 
33
33
  // src/persistor.ts
34
- var import_retry = require("@sebspark/retry");
35
34
  var import_redis = require("redis");
36
35
 
37
36
  // src/localMemory.ts
38
37
  var LocalStorage = class {
39
38
  client = /* @__PURE__ */ new Map();
39
+ isReady = false;
40
40
  get(key) {
41
41
  return this.client.get(key);
42
42
  }
@@ -59,8 +59,9 @@ var LocalStorage = class {
59
59
  }
60
60
  // This is just for testing
61
61
  on(event, callback) {
62
- if (event === "connect" && callback) {
63
- callback("connect");
62
+ if (event === "ready" && callback) {
63
+ this.isReady = true;
64
+ callback("ready");
64
65
  }
65
66
  return this;
66
67
  }
@@ -81,7 +82,6 @@ var Persistor = class {
81
82
  clientId;
82
83
  onError;
83
84
  onSuccess;
84
- isConnected = false;
85
85
  redis;
86
86
  constructor({
87
87
  redis,
@@ -97,43 +97,38 @@ var Persistor = class {
97
97
  } else {
98
98
  CACHE_CLIENT = createLocalMemoryClient;
99
99
  }
100
- if (!this.isConnected) {
100
+ if (!this.client || !this.client.isReady) {
101
101
  this.connect();
102
102
  }
103
103
  }
104
104
  async connect() {
105
- const settings = {
106
- interval: (x) => {
107
- return x * 2 * 1e3;
108
- },
109
- maxRetries: 5,
110
- retryCondition: () => {
111
- var _a;
112
- console.log(`Trying to connect: ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`);
113
- return true;
114
- }
115
- };
116
- await (0, import_retry.retry)(() => this.startConnection(), settings);
105
+ await this.startConnection();
117
106
  }
118
- async startConnection() {
107
+ startConnection() {
119
108
  return new Promise((resolve, reject) => {
120
- try {
121
- this.client = CACHE_CLIENT(this.redis);
122
- this.client.on("error", (err) => {
123
- this.isConnected = false;
124
- this.onError(err);
125
- reject();
126
- });
127
- this.client.on("connect", () => {
128
- this.isConnected = true;
129
- this.onSuccess();
130
- resolve(true);
131
- });
132
- this.client.connect();
133
- } catch (err) {
134
- this.onError(`${err}`);
109
+ this.client = CACHE_CLIENT({
110
+ ...this.redis,
111
+ socket: {
112
+ reconnectStrategy: (retries, cause) => {
113
+ if (retries === 3) {
114
+ console.error("Error reconnecting... ", cause);
115
+ return false;
116
+ }
117
+ return Math.min(retries * 50, 1e3);
118
+ }
119
+ }
120
+ }).on("error", (err) => {
121
+ this.onError(err);
135
122
  reject();
136
- }
123
+ }).on("ready", () => {
124
+ this.onSuccess();
125
+ resolve(true);
126
+ }).on("reconnecting", () => {
127
+ console.log("reconnecting...", this.clientId);
128
+ }).on("end", () => {
129
+ console.log("end...", this.clientId);
130
+ });
131
+ return this.client.connect();
137
132
  });
138
133
  }
139
134
  async size() {
@@ -160,7 +155,8 @@ var Persistor = class {
160
155
  return this.clientId;
161
156
  }
162
157
  getIsClientConnected() {
163
- return this.isConnected;
158
+ var _a;
159
+ return !!((_a = this.client) == null ? void 0 : _a.isReady);
164
160
  }
165
161
  createOptions(ttl) {
166
162
  if (ttl !== null && ttl !== void 0) {
package/dist/index.mjs CHANGED
@@ -2,12 +2,12 @@
2
2
  import { randomUUID } from "node:crypto";
3
3
 
4
4
  // src/persistor.ts
5
- import { retry } from "@sebspark/retry";
6
5
  import { createClient } from "redis";
7
6
 
8
7
  // src/localMemory.ts
9
8
  var LocalStorage = class {
10
9
  client = /* @__PURE__ */ new Map();
10
+ isReady = false;
11
11
  get(key) {
12
12
  return this.client.get(key);
13
13
  }
@@ -30,8 +30,9 @@ var LocalStorage = class {
30
30
  }
31
31
  // This is just for testing
32
32
  on(event, callback) {
33
- if (event === "connect" && callback) {
34
- callback("connect");
33
+ if (event === "ready" && callback) {
34
+ this.isReady = true;
35
+ callback("ready");
35
36
  }
36
37
  return this;
37
38
  }
@@ -52,7 +53,6 @@ var Persistor = class {
52
53
  clientId;
53
54
  onError;
54
55
  onSuccess;
55
- isConnected = false;
56
56
  redis;
57
57
  constructor({
58
58
  redis,
@@ -68,43 +68,38 @@ var Persistor = class {
68
68
  } else {
69
69
  CACHE_CLIENT = createLocalMemoryClient;
70
70
  }
71
- if (!this.isConnected) {
71
+ if (!this.client || !this.client.isReady) {
72
72
  this.connect();
73
73
  }
74
74
  }
75
75
  async connect() {
76
- const settings = {
77
- interval: (x) => {
78
- return x * 2 * 1e3;
79
- },
80
- maxRetries: 5,
81
- retryCondition: () => {
82
- var _a;
83
- console.log(`Trying to connect: ${this.clientId}, ${(_a = this.redis) == null ? void 0 : _a.name}`);
84
- return true;
85
- }
86
- };
87
- await retry(() => this.startConnection(), settings);
76
+ await this.startConnection();
88
77
  }
89
- async startConnection() {
78
+ startConnection() {
90
79
  return new Promise((resolve, reject) => {
91
- try {
92
- this.client = CACHE_CLIENT(this.redis);
93
- this.client.on("error", (err) => {
94
- this.isConnected = false;
95
- this.onError(err);
96
- reject();
97
- });
98
- this.client.on("connect", () => {
99
- this.isConnected = true;
100
- this.onSuccess();
101
- resolve(true);
102
- });
103
- this.client.connect();
104
- } catch (err) {
105
- this.onError(`${err}`);
80
+ this.client = CACHE_CLIENT({
81
+ ...this.redis,
82
+ socket: {
83
+ reconnectStrategy: (retries, cause) => {
84
+ if (retries === 3) {
85
+ console.error("Error reconnecting... ", cause);
86
+ return false;
87
+ }
88
+ return Math.min(retries * 50, 1e3);
89
+ }
90
+ }
91
+ }).on("error", (err) => {
92
+ this.onError(err);
106
93
  reject();
107
- }
94
+ }).on("ready", () => {
95
+ this.onSuccess();
96
+ resolve(true);
97
+ }).on("reconnecting", () => {
98
+ console.log("reconnecting...", this.clientId);
99
+ }).on("end", () => {
100
+ console.log("end...", this.clientId);
101
+ });
102
+ return this.client.connect();
108
103
  });
109
104
  }
110
105
  async size() {
@@ -131,7 +126,8 @@ var Persistor = class {
131
126
  return this.clientId;
132
127
  }
133
128
  getIsClientConnected() {
134
- return this.isConnected;
129
+ var _a;
130
+ return !!((_a = this.client) == null ? void 0 : _a.isReady);
135
131
  }
136
132
  createOptions(ttl) {
137
133
  if (ttl !== null && ttl !== void 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -19,7 +19,7 @@
19
19
  "tsconfig": "*"
20
20
  },
21
21
  "dependencies": {
22
- "redis": "4.6.14",
22
+ "redis": "4.7.0",
23
23
  "@sebspark/retry": "*"
24
24
  }
25
25
  }