@hastehaul/common 2.0.46 → 2.0.48

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,13 @@
1
- import IORedis from 'ioredis';
2
1
  export declare class RedisClient {
3
2
  private master;
4
3
  private replicas;
5
- private _redisClient?;
6
4
  private static instance;
7
5
  private constructor();
8
- static getInstance(masterHost: string, replicaHosts: string[]): RedisClient;
9
- executeMasterOperation(command: string, ...args: any[]): Promise<any>;
10
- executeReplicaOperation(command: string, ...args: any[]): Promise<any>;
11
- executeOperation(client: IORedis, command: string, ...args: any[]): Promise<any>;
6
+ get client(): RedisClient;
7
+ static connect(masterHost: string, replicaHosts: string[]): Promise<RedisClient>;
8
+ private executeMasterOperation;
9
+ private executeReplicaOperation;
10
+ private executeOperation;
12
11
  set(key: string, value: string): Promise<any>;
13
12
  get(key: string): Promise<any>;
14
13
  del(key: string): Promise<any>;
@@ -38,23 +38,48 @@ class RedisClient {
38
38
  constructor(masterHost, replicaHosts) {
39
39
  this.master = new ioredis_1.default({ host: masterHost, port: 6379 });
40
40
  this.replicas = replicaHosts.map(replicaHost => new ioredis_1.default({ host: replicaHost, port: 6379 }));
41
- this.master.on("connect", () => console.info("Redis Server Connected!"));
42
- this.master.on("error", (error) => console.error("Error in Redis", error.message));
43
- this.replicas.forEach((client) => {
44
- client.on('connect', () => console.log('Replica connected'));
45
- client.on('error', (error) => console.log('Replica connected', error.message));
41
+ const masterConnected = new Promise((resolve) => {
42
+ this.master.on('connect', () => {
43
+ console.log('Master connected');
44
+ resolve();
45
+ });
46
+ this.master.on('error', (error) => {
47
+ console.log('Master connection error:', error.message);
48
+ resolve();
49
+ });
50
+ });
51
+ const replicaPromises = this.replicas.map((replica) => {
52
+ return new Promise((resolve) => {
53
+ replica.on('connect', () => {
54
+ console.log('Replica connected');
55
+ resolve();
56
+ });
57
+ replica.on('error', (error) => {
58
+ console.log('Replica connection error:', error.message);
59
+ resolve();
60
+ });
61
+ });
62
+ });
63
+ // Wait for all connections to be established
64
+ Promise.all([masterConnected, ...replicaPromises]).then(() => {
65
+ console.log('All connections established');
46
66
  });
47
67
  }
48
- // get client(): IORedis {
49
- // if(!this._redisClient) throw new Error("Cannot access Redis Client before connecting")
50
- // return this._redisClient;
51
- // }
52
- static getInstance(masterHost, replicaHosts) {
53
- if (!RedisClient.instance) {
54
- RedisClient.instance = new RedisClient(masterHost, replicaHosts);
55
- }
68
+ get client() {
69
+ if (!RedisClient.instance)
70
+ throw new Error("Cannot access Redis Client before connecting");
56
71
  return RedisClient.instance;
57
72
  }
73
+ static connect(masterHost, replicaHosts) {
74
+ return new Promise((resolve) => {
75
+ if (!RedisClient.instance) {
76
+ const redisClient = new RedisClient(masterHost, replicaHosts);
77
+ RedisClient.instance = redisClient;
78
+ resolve(redisClient);
79
+ }
80
+ resolve(RedisClient.instance);
81
+ });
82
+ }
58
83
  executeMasterOperation(command, ...args) {
59
84
  return __awaiter(this, void 0, void 0, function* () {
60
85
  return this.executeOperation(this.master, command, ...args);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "2.0.46",
3
+ "version": "2.0.48",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",