chore: restore signals changes to master

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2024-01-26 12:06:58 +02:00
parent 8724ed1fdf
commit fdf03a6997
5 changed files with 26 additions and 36 deletions
+2 -2
View File
@@ -22,9 +22,9 @@ export type SignalPayload = {
};
// @public (undocumented)
export interface SignalService {
export type SignalService = {
publish(signal: SignalPayload): Promise<void>;
}
};
// @public (undocumented)
export const signalService: ServiceRef<SignalService, 'plugin'>;
+2 -2
View File
@@ -16,9 +16,9 @@
import { SignalPayload } from './types';
/** @public */
export interface SignalService {
export type SignalService = {
/**
* Publishes a message to user refs to specific topic
*/
publish(signal: SignalPayload): Promise<void>;
}
};
+5 -10
View File
@@ -7,23 +7,18 @@ import { ApiRef } from '@backstage/core-plugin-api';
import { JsonObject } from '@backstage/types';
// @public (undocumented)
export interface SignalApi {
// (undocumented)
export type SignalApi = {
subscribe(
channel: string,
onMessage: (message: JsonObject) => void,
): SignalSubscriber;
}
): {
unsubscribe: () => void;
};
};
// @public (undocumented)
export const signalApiRef: ApiRef<SignalApi>;
// @public (undocumented)
export interface SignalSubscriber {
// (undocumented)
unsubscribe(): void;
}
// @public (undocumented)
export const useSignal: (channel: string) => {
lastSignal: JsonObject | null;
+3 -8
View File
@@ -22,14 +22,9 @@ export const signalApiRef = createApiRef<SignalApi>({
});
/** @public */
export interface SignalSubscriber {
unsubscribe(): void;
}
/** @public */
export interface SignalApi {
export type SignalApi = {
subscribe(
channel: string,
onMessage: (message: JsonObject) => void,
): SignalSubscriber;
}
): { unsubscribe: () => void };
};
+14 -14
View File
@@ -146,6 +146,20 @@ export class SignalClient implements SignalApi {
url.protocol = url.protocol === 'http:' ? 'ws:' : 'wss:';
this.ws = new WebSocket(url.toString(), token);
this.ws.onmessage = (data: MessageEvent) => {
this.handleMessage(data);
};
this.ws.onerror = () => {
this.reconnect();
};
this.ws.onclose = (ev: CloseEvent) => {
if (ev.code !== WS_CLOSE_NORMAL && ev.code !== WS_CLOSE_GOING_AWAY) {
this.reconnect();
}
};
// Wait until connection is open
let connectSleep = 0;
while (
@@ -160,20 +174,6 @@ export class SignalClient implements SignalApi {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
throw new Error('Connect timeout');
}
this.ws.onmessage = (data: MessageEvent) => {
this.handleMessage(data);
};
this.ws.onerror = () => {
this.reconnect();
};
this.ws.onclose = (ev: CloseEvent) => {
if (ev.code !== WS_CLOSE_NORMAL && ev.code !== WS_CLOSE_GOING_AWAY) {
this.reconnect();
}
};
}
private handleMessage(data: MessageEvent) {