Skip to content

Commit 1852891

Browse files
authored
feat(graphql): remember connection status without subscribers (#809)
* Remember connection status with no subscribers * Add nohup.out to .gitignore
1 parent 86d5970 commit 1852891

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ build
4141
!.yarn/sdks
4242
!.yarn/versions
4343
.pnp.*
44+
nohup.out

packages/javascript-api/src/lib/services/graphql/graphql.service.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import {
55
SourceLocation,
66
} from 'graphql';
77
import WebSocket, { CloseEvent } from 'isomorphic-ws';
8-
import { Observable, Observer, startWith, Subject } from 'rxjs';
9-
import { distinctUntilChanged, shareReplay } from 'rxjs/operators';
8+
import {
9+
BehaviorSubject,
10+
distinctUntilChanged,
11+
Observable,
12+
Observer,
13+
shareReplay,
14+
} from 'rxjs';
15+
1016
import { ConnectionStatus } from '../../model/connection-status.js';
1117
import { calculateRandomizedExponentialBackoffTime } from '../../util/randomized-exponential-backoff/randomized-exponential-backoff.js';
1218
import { sleepMs } from '../../util/sleep-ms/sleep-ms.js';
@@ -85,25 +91,32 @@ const CLIENT_SIDE_CLOSE_EVENT = 1000;
8591
* trying to import GraphQLService.
8692
*/
8793
export class GraphqlService {
88-
private logger = new Logger('GraphQL');
94+
private readonly logger = new Logger('GraphQL');
8995
private apiServer: string;
9096

9197
private socket: WebSocket = null;
9298

9399
private connectionStatus: ConnectionStatus;
94-
private connectionStatus$ = new Subject<ConnectionStatus>();
95100

96-
private nextSubscriptionId: number = 1;
101+
private readonly connectionStatus$ = new BehaviorSubject<ConnectionStatus>(
102+
ConnectionStatus.DISCONNECTED,
103+
);
104+
105+
private nextSubscriptionId = 1;
97106

98107
private subscriptions: Subscription[] = [];
99-
private subscriptionObserverMap: { [id: string]: Observer<object> } = {};
100-
private subscriptionConnection$: Observable<ConnectionStatus>;
108+
109+
private readonly subscriptionObserverMap: { [id: string]: Observer<object> } =
110+
{};
111+
112+
private readonly subscriptionConnection$: Observable<ConnectionStatus>;
101113
private temporaryApiKeyService: TemporaryApiKeyService | undefined;
102114

103115
private pongTimeout: any;
104116
private pingPongInterval: any;
105-
private sendPingWithThisBound = this.sendPing.bind(this);
106-
private handleConnectionDropWithThisBound =
117+
private readonly sendPingWithThisBound = this.sendPing.bind(this);
118+
119+
private readonly handleConnectionDropWithThisBound =
107120
this.handleConnectionDrop.bind(this);
108121

109122
private connectionAttemptsCount = 0;
@@ -112,7 +125,6 @@ export class GraphqlService {
112125
this.setServer('api.qminder.com');
113126

114127
this.subscriptionConnection$ = this.connectionStatus$.pipe(
115-
startWith(ConnectionStatus.CONNECTING),
116128
distinctUntilChanged(),
117129
shareReplay(1),
118130
);

0 commit comments

Comments
 (0)