Skip to content

Commit 4a3ca43

Browse files
committed
refactor: hoist ws provider to cardano-services-client package
1 parent 74bfac0 commit 4a3ca43

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

packages/cardano-services-client/src/WebSocket.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import {
55
Cardano,
66
EpochInfo,
77
EraSummary,
8+
HealthCheckResponse,
89
NetworkInfoMethods,
910
NetworkInfoProvider,
11+
Provider,
1012
ProviderError,
1113
ProviderFailure,
1214
StakeSummary,
1315
SupplySummary,
1416
WSMessage,
15-
WsProvider,
1617
createSlotEpochInfoCalc
1718
} from '@cardano-sdk/core';
1819
import { Logger } from 'ts-log';
@@ -54,6 +55,35 @@ const isEventError = (error: unknown): error is { error: Error } =>
5455
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5556
typeof error === 'object' && !!error && (error as any).error instanceof Error;
5657

58+
export class WsProvider implements Provider {
59+
/** Emits the health state. */
60+
public health$: Observable<HealthCheckResponse>;
61+
62+
private healthSubject$: ReplaySubject<HealthCheckResponse>;
63+
private reason?: string;
64+
65+
constructor() {
66+
this.health$ = this.healthSubject$ = new ReplaySubject<HealthCheckResponse>(1);
67+
this.healthSubject$.next({ ok: false, reason: 'starting' });
68+
}
69+
70+
protected emitHealth(reason?: string, overwrite?: boolean) {
71+
if (!reason) {
72+
this.reason = undefined;
73+
74+
return this.healthSubject$.next({ ok: true });
75+
}
76+
77+
if (overwrite || !this.reason) this.reason = reason;
78+
79+
this.healthSubject$.next({ ok: false, reason: this.reason });
80+
}
81+
82+
public healthCheck() {
83+
return firstValueFrom(this.health$);
84+
}
85+
}
86+
5787
export class CardanoWsClient extends WsProvider {
5888
/** The client id, assigned by the server. */
5989
clientId = NOT_CONNECTED_ID;

packages/cardano-services/src/WsServer/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {
44
NetworkInfoResponses,
55
Seconds,
66
WSMessage,
7-
WsProvider,
87
createSlotEpochInfoCalc
98
} from '@cardano-sdk/core';
109
import { GenesisData } from '..';
1110
import { Logger } from 'ts-log';
1211
import { Notification, Pool } from 'pg';
1312
import { Server, createServer } from 'http';
1413
import { WebSocket, WebSocketServer } from 'ws';
14+
import { WsProvider } from '@cardano-sdk/cardano-services-client';
1515
import { getLovelaceSupply, getProtocolParameters, getStake } from './requests';
1616
import { initDB } from './db';
1717
import { toGenesisParams } from '../NetworkInfo/DbSyncNetworkInfoProvider/mappers';

packages/core/src/WebSocket.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { HealthCheckResponse, NetworkInfoProvider, Provider } from './Provider';
2-
import { Observable, ReplaySubject, firstValueFrom } from 'rxjs';
1+
import { NetworkInfoProvider } from './Provider';
32

43
export type AsyncReturnType<F extends () => unknown> = F extends () => Promise<infer R> ? R : never;
54

@@ -13,32 +12,3 @@ export interface WSMessage {
1312
/** Latest value(s) for the `NetworkInfoProvider` methods.*/
1413
networkInfo?: Partial<NetworkInfoResponses>;
1514
}
16-
17-
export class WsProvider implements Provider {
18-
/** Emits the health state. */
19-
public health$: Observable<HealthCheckResponse>;
20-
21-
private healthSubject$: ReplaySubject<HealthCheckResponse>;
22-
private reason?: string;
23-
24-
constructor() {
25-
this.health$ = this.healthSubject$ = new ReplaySubject<HealthCheckResponse>(1);
26-
this.healthSubject$.next({ ok: false, reason: 'starting' });
27-
}
28-
29-
protected emitHealth(reason?: string, overwrite?: boolean) {
30-
if (!reason) {
31-
this.reason = undefined;
32-
33-
return this.healthSubject$.next({ ok: true });
34-
}
35-
36-
if (overwrite || !this.reason) this.reason = reason;
37-
38-
this.healthSubject$.next({ ok: false, reason: this.reason });
39-
}
40-
41-
public healthCheck() {
42-
return firstValueFrom(this.health$);
43-
}
44-
}

packages/e2e/test/ws-server/webSocket.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { CardanoWsClient } from '@cardano-sdk/cardano-services-client';
1+
import { CardanoWsClient, WsProvider } from '@cardano-sdk/cardano-services-client';
22
import {
33
CardanoWsServer,
44
GenesisData,
55
createDnsResolver,
66
getOgmiosCardanoNode,
77
util
88
} from '@cardano-sdk/cardano-services';
9-
import { HealthCheckResponse, WsProvider } from '@cardano-sdk/core';
9+
import { HealthCheckResponse } from '@cardano-sdk/core';
1010
import { OgmiosCardanoNode } from '@cardano-sdk/ogmios';
1111
import { Pool } from 'pg';
1212
import { filter, firstValueFrom } from 'rxjs';

0 commit comments

Comments
 (0)