Skip to content

Commit

Permalink
Merge pull request #113 from buggregator/bugfix/normalize-ws-connections
Browse files Browse the repository at this point in the history
fix ws reconnections calls
  • Loading branch information
butschster authored Mar 3, 2024
2 parents 4bb0414 + ab0c61a commit a1a44a6
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions src/shared/lib/io/centrifuge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,47 @@ import { logger } from "./logger";
type TUseCentrifuge = () => {
centrifuge: Centrifuge
}
export const useCentrifuge: TUseCentrifuge = () => {
const centrifuge = new Centrifuge(WS_URL)

centrifuge.on('connected', (ctx) => {
logger(['connected', ctx]);
});
class WSConnection {
private readonly centrifuge: Centrifuge;

centrifuge.on('publication', (ctx) => {
logger(['publication', ctx]);
});
// eslint-disable-next-line no-use-before-define
private static instance: WSConnection;

centrifuge.on('disconnected', (ctx) => {
logger(['disconnected', ctx]);
});
private constructor() {
this.centrifuge = new Centrifuge(WS_URL);

centrifuge.on('error', (ctx) => {
logger(['error', ctx]);
})
this.centrifuge.on('connected', (ctx) => {
logger(['connected', ctx]);
});

centrifuge.connect();
this.centrifuge.on('publication', (ctx) => {
logger(['publication', ctx]);
});

return { centrifuge }
this.centrifuge.on('disconnected', (ctx) => {
logger(['disconnected', ctx]);
});

this.centrifuge.on('error', (ctx) => {
logger(['error', ctx]);
})

this.centrifuge.connect();
}

public static getInstance(): WSConnection {
if (!WSConnection.instance) {
WSConnection.instance = new WSConnection();
}

return WSConnection.instance;
}

public getCentrifuge () {
return this.centrifuge;
}
}


export const useCentrifuge: TUseCentrifuge = () => ({ centrifuge: WSConnection.getInstance().getCentrifuge() })

0 comments on commit a1a44a6

Please sign in to comment.