feat: signals plugins

next try after #18153 without any external dependencies and only
supporting websocket. missing tests and necessary documentation but will
work on those after initial comments if this would be proper way to go
forward. already planning for the notification plugins on top of this.

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2023-12-04 12:36:50 +02:00
parent 04d94dc3d0
commit 047beadd9d
38 changed files with 1308 additions and 7 deletions
+1
View File
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
+5
View File
@@ -0,0 +1,5 @@
# @backstage/plugin-signals-react
Welcome to the web library package for the signals plugin!
_This plugin was created through the Backstage CLI_
+45
View File
@@ -0,0 +1,45 @@
## API Report File for "@backstage/plugin-signals-react"
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { ApiRef } from '@backstage/core-plugin-api';
import { DiscoveryApi } from '@backstage/core-plugin-api';
import { JSONObject } from '@apollo/explorer/src/helpers/types';
import { JsonObject } from '@backstage/types';
// @public (undocumented)
export type SignalsApi = {
subscribe(
onMessage: (message: JsonObject, topic?: string) => void,
topic?: string,
): void;
unsubscribe(topic?: string): void;
};
// @public (undocumented)
export const signalsApiRef: ApiRef<SignalsApi>;
// @public (undocumented)
export class SignalsClient implements SignalsApi {
// (undocumented)
static create(options: { discoveryApi: DiscoveryApi }): SignalsClient;
// (undocumented)
static instance: SignalsClient | null;
// (undocumented)
subscribe(
onMessage: (message: JsonObject, topic?: string) => void,
topic?: string,
): void;
// (undocumented)
unsubscribe(topic?: string): void;
}
// @public (undocumented)
export const useSignalsApi: (
onMessage: (message: JSONObject) => void,
topic?: string,
) => void;
// (No @packageDocumentation comment for this package)
```
+10
View File
@@ -0,0 +1,10 @@
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: backstage-plugin-signals-react
title: '@backstage/plugin-signals-react'
description: Web library for the signals plugin
spec:
lifecycle: experimental
type: backstage-web-library
owner: maintainers
+43
View File
@@ -0,0 +1,43 @@
{
"name": "@backstage/plugin-signals-react",
"description": "Web library for the signals plugin",
"version": "0.0.0",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
"main": "dist/index.esm.js",
"types": "dist/index.d.ts"
},
"backstage": {
"role": "web-library"
},
"sideEffects": false,
"scripts": {
"start": "backstage-cli package start",
"build": "backstage-cli package build",
"lint": "backstage-cli package lint",
"test": "backstage-cli package test",
"clean": "backstage-cli package clean",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack"
},
"dependencies": {
"@backstage/core-plugin-api": "workspace:^",
"@backstage/types": "workspace:^",
"@material-ui/core": "^4.9.13"
},
"peerDependencies": {
"react": "^16.13.1 || ^17.0.0"
},
"devDependencies": {
"@backstage/cli": "workspace:^",
"@backstage/test-utils": "workspace:^",
"@testing-library/jest-dom": "^5.10.1",
"@testing-library/react": "^12.1.3"
},
"files": [
"dist"
]
}
@@ -0,0 +1,32 @@
/*
* 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 { createApiRef } from '@backstage/core-plugin-api';
import { JsonObject } from '@backstage/types';
/** @public */
export const signalsApiRef = createApiRef<SignalsApi>({
id: 'plugin.signals.service',
});
/** @public */
export type SignalsApi = {
subscribe(
onMessage: (message: JsonObject, topic?: string) => void,
topic?: string,
): void;
unsubscribe(topic?: string): void;
};
@@ -0,0 +1,133 @@
/*
* 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, topic?: string) => 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, topic?: string) => void,
topic?: string,
): void {
const subscriptionTopic = topic ?? '*';
// Do not allow to subscribe to same topic multiple times
if (this.cbs.has(subscriptionTopic)) {
return;
}
this.cbs.set(subscriptionTopic, onMessage);
this.connect().then(() => {
this.send({ action: 'subscribe', topic });
});
}
unsubscribe(topic?: string): void {
const subscriptionTopic = topic ?? '*';
this.cbs.delete(subscriptionTopic);
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;
let cb = this.cbs.get('*');
if (json.topic) {
cb = this.cbs.get(json.topic as string);
}
if (cb) {
cb(json.message as JsonObject, json.topic as string);
}
} 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();
}, 5000);
}
}
+17
View File
@@ -0,0 +1,17 @@
/*
* 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.
*/
export * from './SignalsApi';
export * from './SignalsClient';
+16
View File
@@ -0,0 +1,16 @@
/*
* 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.
*/
export * from './useSignalsApi';
@@ -0,0 +1,37 @@
/*
* 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 { SignalsClient } from '../api';
import { discoveryApiRef, useApi } from '@backstage/core-plugin-api';
import { JSONObject } from '@apollo/explorer/src/helpers/types';
import { useEffect } from 'react';
/** @public */
export const useSignalsApi = (
onMessage: (message: JSONObject) => void,
topic?: string,
) => {
const discovery = useApi(discoveryApiRef);
const signals = SignalsClient.create({ discoveryApi: discovery });
useEffect(() => {
signals.subscribe(onMessage, topic);
}, [signals, onMessage, topic]);
useEffect(() => {
return () => {
signals.unsubscribe(topic);
};
}, [signals, topic]);
};
+18
View File
@@ -0,0 +1,18 @@
/*
* 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.
*/
export * from './api';
export * from './hooks';
+16
View File
@@ -0,0 +1,16 @@
/*
* 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 '@testing-library/jest-dom';