chore: move signals client to fronend plugin

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2023-12-04 15:00:02 +02:00
parent 01d02b0d3b
commit 5e1a90daa2
20 changed files with 286 additions and 25 deletions
@@ -1,131 +0,0 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { SignalsApi } from './SignalsApi';
import { JsonObject } from '@backstage/types';
import { DiscoveryApi } from '@backstage/core-plugin-api';
/** @public */
export class SignalsClient implements SignalsApi {
static instance: SignalsClient | null = null;
private ws: WebSocket | null = null;
private discoveryApi: DiscoveryApi;
private cbs: Map<string, (message: JsonObject) => void> = new Map();
private queue: JsonObject[] = [];
private reconnectTimeout: any;
static create(options: { discoveryApi: DiscoveryApi }) {
if (!SignalsClient.instance) {
SignalsClient.instance = new SignalsClient(options);
}
return SignalsClient.instance;
}
private constructor(options: { discoveryApi: DiscoveryApi }) {
this.discoveryApi = options.discoveryApi;
}
subscribe(onMessage: (message: JsonObject) => void, topic: string): void {
// Do not allow to subscribe to same topic multiple times
if (this.cbs.has(topic)) {
return;
}
this.cbs.set(topic, onMessage);
this.connect().then(() => {
this.send({ action: 'subscribe', topic });
});
}
unsubscribe(topic: string): void {
this.cbs.delete(topic);
this.send({ action: 'unsubscribe', topic });
}
private send(data?: JsonObject): void {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
if (data) {
this.queue.push(data);
}
return;
}
// First send queue
for (const msg of this.queue) {
this.ws!.send(JSON.stringify(msg));
}
this.queue = [];
if (data) {
this.ws!.send(JSON.stringify(data));
}
}
private async connect() {
if (this.ws) {
return;
}
const apiUrl = `${await this.discoveryApi.getBaseUrl('signals')}`;
const url = new URL(apiUrl);
url.protocol = url.protocol === 'http:' ? 'ws' : 'wss';
this.ws = new WebSocket(url.toString());
this.ws.onmessage = (data: MessageEvent) => {
try {
const json = JSON.parse(data.data) as JsonObject;
if (json.topic) {
const cb = this.cbs.get(json.topic as string);
if (cb) {
cb(json.message as JsonObject);
}
}
} catch (e) {
// NOOP
}
};
this.ws.onerror = () => {
this.reconnect();
};
this.ws.onclose = () => {
this.reconnect();
};
while (this.ws.readyState !== WebSocket.OPEN) {
await new Promise(r => setTimeout(r, 10));
}
this.send();
}
private reconnect() {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
}
this.reconnectTimeout = setTimeout(() => {
if (this.ws) {
this.ws.close();
}
this.ws = null;
this.connect().then(() => {
// Resubscribe to existing topics in case we lost connection
for (const topic of this.cbs.keys()) {
this.send({ action: 'subscribe', topic });
}
});
}, 5000);
}
}
-1
View File
@@ -14,4 +14,3 @@
* limitations under the License.
*/
export * from './SignalsApi';
export * from './SignalsClient';
@@ -13,18 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { SignalsClient } from '../api';
import { discoveryApiRef, useApi } from '@backstage/core-plugin-api';
import { JSONObject } from '@apollo/explorer/src/helpers/types';
import { signalsApiRef } from '../api';
import { useApi } from '@backstage/core-plugin-api';
import { JsonObject } from '@backstage/types';
import { useEffect } from 'react';
/** @public */
export const useSignalsApi = (
topic: string,
onMessage: (message: JSONObject) => void,
onMessage: (message: JsonObject) => void,
) => {
const discovery = useApi(discoveryApiRef);
const signals = SignalsClient.create({ discoveryApi: discovery });
const signals = useApi(signalsApiRef);
useEffect(() => {
signals.subscribe(onMessage, topic);
}, [signals, onMessage, topic]);