@sebspark/promise-cache 2.0.4 → 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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +19 -24
- package/dist/index.mjs +19 -24
- package/package.json +1 -1
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
|
@@ -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 === "
|
|
63
|
-
|
|
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.
|
|
101
|
+
if (!this.client || !this.client.isReady) {
|
|
101
102
|
this.connect();
|
|
102
103
|
}
|
|
103
104
|
}
|
|
@@ -106,34 +107,27 @@ var Persistor = class {
|
|
|
106
107
|
interval: (x) => {
|
|
107
108
|
return x * 2 * 1e3;
|
|
108
109
|
},
|
|
109
|
-
maxRetries:
|
|
110
|
+
maxRetries: 3,
|
|
110
111
|
retryCondition: () => {
|
|
111
112
|
var _a;
|
|
112
|
-
console.log(
|
|
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
|
-
|
|
121
|
+
startConnection() {
|
|
119
122
|
return new Promise((resolve, reject) => {
|
|
120
|
-
|
|
121
|
-
this.
|
|
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}`);
|
|
123
|
+
this.client = CACHE_CLIENT(this.redis).on("error", (err) => {
|
|
124
|
+
this.onError(err);
|
|
135
125
|
reject();
|
|
136
|
-
}
|
|
126
|
+
}).on("ready", () => {
|
|
127
|
+
this.onSuccess();
|
|
128
|
+
resolve(true);
|
|
129
|
+
});
|
|
130
|
+
return this.client.connect();
|
|
137
131
|
});
|
|
138
132
|
}
|
|
139
133
|
async size() {
|
|
@@ -160,7 +154,8 @@ var Persistor = class {
|
|
|
160
154
|
return this.clientId;
|
|
161
155
|
}
|
|
162
156
|
getIsClientConnected() {
|
|
163
|
-
|
|
157
|
+
var _a;
|
|
158
|
+
return !!((_a = this.client) == null ? void 0 : _a.isReady);
|
|
164
159
|
}
|
|
165
160
|
createOptions(ttl) {
|
|
166
161
|
if (ttl !== null && ttl !== void 0) {
|
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 === "
|
|
34
|
-
|
|
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.
|
|
72
|
+
if (!this.client || !this.client.isReady) {
|
|
72
73
|
this.connect();
|
|
73
74
|
}
|
|
74
75
|
}
|
|
@@ -77,34 +78,27 @@ var Persistor = class {
|
|
|
77
78
|
interval: (x) => {
|
|
78
79
|
return x * 2 * 1e3;
|
|
79
80
|
},
|
|
80
|
-
maxRetries:
|
|
81
|
+
maxRetries: 3,
|
|
81
82
|
retryCondition: () => {
|
|
82
83
|
var _a;
|
|
83
|
-
console.log(
|
|
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
|
-
|
|
92
|
+
startConnection() {
|
|
90
93
|
return new Promise((resolve, reject) => {
|
|
91
|
-
|
|
92
|
-
this.
|
|
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}`);
|
|
94
|
+
this.client = CACHE_CLIENT(this.redis).on("error", (err) => {
|
|
95
|
+
this.onError(err);
|
|
106
96
|
reject();
|
|
107
|
-
}
|
|
97
|
+
}).on("ready", () => {
|
|
98
|
+
this.onSuccess();
|
|
99
|
+
resolve(true);
|
|
100
|
+
});
|
|
101
|
+
return this.client.connect();
|
|
108
102
|
});
|
|
109
103
|
}
|
|
110
104
|
async size() {
|
|
@@ -131,7 +125,8 @@ var Persistor = class {
|
|
|
131
125
|
return this.clientId;
|
|
132
126
|
}
|
|
133
127
|
getIsClientConnected() {
|
|
134
|
-
|
|
128
|
+
var _a;
|
|
129
|
+
return !!((_a = this.client) == null ? void 0 : _a.isReady);
|
|
135
130
|
}
|
|
136
131
|
createOptions(ttl) {
|
|
137
132
|
if (ttl !== null && ttl !== void 0) {
|