@backstage/plugin-signals-react 0.0.0-nightly-20240118021622
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +52 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.esm.js +30 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +44 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# @backstage/plugin-signals-react
|
|
2
|
+
|
|
3
|
+
## 0.0.0-nightly-20240118021622
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 047bead: Add support to subscribe and publish messages through signals plugins
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @backstage/core-plugin-api@1.8.2
|
|
10
|
+
- @backstage/types@1.1.1
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @backstage/plugin-signals-react
|
|
2
|
+
|
|
3
|
+
Welcome to the web library package for the signals plugin!
|
|
4
|
+
|
|
5
|
+
Signals plugin allows backend plugins to publish messages to frontend plugins.
|
|
6
|
+
|
|
7
|
+
## Getting started
|
|
8
|
+
|
|
9
|
+
This plugin contains functionalities that help utilize the signals plugin. To get started,
|
|
10
|
+
see installation instructions from `@backstage/plugin-signals-node`, `@backstage/plugin-signals-backend`, and
|
|
11
|
+
`@backstage/plugin-signals`.
|
|
12
|
+
|
|
13
|
+
There are two ways to utilize the signals plugin; either by using the hook or by directly using the API.
|
|
14
|
+
|
|
15
|
+
## Using the hook
|
|
16
|
+
|
|
17
|
+
By using the hook, unsubscribe is automatically taken care of. This helps to maintain only necessary amount
|
|
18
|
+
of connections to the backend and also to allow multiple subscriptions using the same connection.
|
|
19
|
+
|
|
20
|
+
Example of using the hook:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { useSignal } from '@backstage/plugin-signals-react';
|
|
24
|
+
|
|
25
|
+
const { lastSignal } = useSignal('myplugin:channel');
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
console.log(lastSignal);
|
|
29
|
+
}, [lastSignal]);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Whenever backend publishes new message to the channel `myplugin:channel`, the `lastSignal` is changed. The `lastSignal`
|
|
33
|
+
is always initiated with null value before any messages are received from the backend.
|
|
34
|
+
|
|
35
|
+
## Using API directly
|
|
36
|
+
|
|
37
|
+
You can also use the signal API directly. This allows more fine-grained control over the state of the connections and
|
|
38
|
+
subscriptions.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { signalsApiRef } from '@backstage/plugin-signals-react';
|
|
42
|
+
|
|
43
|
+
const signals = useApi(signalsApiRef);
|
|
44
|
+
const { unsubscribe } = signals.subscribe(
|
|
45
|
+
'myplugin:channel',
|
|
46
|
+
(message: JsonObject) => {
|
|
47
|
+
console.log(message);
|
|
48
|
+
},
|
|
49
|
+
);
|
|
50
|
+
// Remember to unsubscribe
|
|
51
|
+
unsubscribe();
|
|
52
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as _backstage_core_plugin_api from '@backstage/core-plugin-api';
|
|
2
|
+
import { JsonObject } from '@backstage/types';
|
|
3
|
+
|
|
4
|
+
/** @public */
|
|
5
|
+
declare const signalApiRef: _backstage_core_plugin_api.ApiRef<SignalApi>;
|
|
6
|
+
/** @public */
|
|
7
|
+
type SignalApi = {
|
|
8
|
+
subscribe(channel: string, onMessage: (message: JsonObject) => void): {
|
|
9
|
+
unsubscribe: () => void;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** @public */
|
|
14
|
+
declare const useSignal: (channel: string) => {
|
|
15
|
+
lastSignal: JsonObject | null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { SignalApi, signalApiRef, useSignal };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createApiRef, useApiHolder } from '@backstage/core-plugin-api';
|
|
2
|
+
import { useState, useEffect } from 'react';
|
|
3
|
+
|
|
4
|
+
const signalApiRef = createApiRef({
|
|
5
|
+
id: "plugin.signal.service"
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
const useSignal = (channel) => {
|
|
9
|
+
const apiHolder = useApiHolder();
|
|
10
|
+
const signals = apiHolder.get(signalApiRef);
|
|
11
|
+
const [lastSignal, setLastSignal] = useState(null);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
let unsub = null;
|
|
14
|
+
if (signals) {
|
|
15
|
+
const { unsubscribe } = signals.subscribe(channel, (msg) => {
|
|
16
|
+
setLastSignal(msg);
|
|
17
|
+
});
|
|
18
|
+
unsub = unsubscribe;
|
|
19
|
+
}
|
|
20
|
+
return () => {
|
|
21
|
+
if (signals && unsub) {
|
|
22
|
+
unsub();
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}, [signals, channel]);
|
|
26
|
+
return { lastSignal };
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { signalApiRef, useSignal };
|
|
30
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/api/SignalApi.ts","../src/hooks/useSignal.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { createApiRef } from '@backstage/core-plugin-api';\nimport { JsonObject } from '@backstage/types';\n\n/** @public */\nexport const signalApiRef = createApiRef<SignalApi>({\n id: 'plugin.signal.service',\n});\n\n/** @public */\nexport type SignalApi = {\n subscribe(\n channel: string,\n onMessage: (message: JsonObject) => void,\n ): { unsubscribe: () => void };\n};\n","/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { signalApiRef } from '../api';\nimport { useApiHolder } from '@backstage/core-plugin-api';\nimport { JsonObject } from '@backstage/types';\nimport { useEffect, useState } from 'react';\n\n/** @public */\nexport const useSignal = (channel: string) => {\n const apiHolder = useApiHolder();\n // Use apiHolder instead useApi in case signalApi is not available in the\n // backstage instance this is used\n const signals = apiHolder.get(signalApiRef);\n const [lastSignal, setLastSignal] = useState<JsonObject | null>(null);\n useEffect(() => {\n let unsub: null | (() => void) = null;\n if (signals) {\n const { unsubscribe } = signals.subscribe(channel, (msg: JsonObject) => {\n setLastSignal(msg);\n });\n unsub = unsubscribe;\n }\n return () => {\n if (signals && unsub) {\n unsub();\n }\n };\n }, [signals, channel]);\n\n return { lastSignal };\n};\n"],"names":[],"mappings":";;;AAmBO,MAAM,eAAe,YAAwB,CAAA;AAAA,EAClD,EAAI,EAAA,uBAAA;AACN,CAAC;;ACAY,MAAA,SAAA,GAAY,CAAC,OAAoB,KAAA;AAC5C,EAAA,MAAM,YAAY,YAAa,EAAA,CAAA;AAG/B,EAAM,MAAA,OAAA,GAAU,SAAU,CAAA,GAAA,CAAI,YAAY,CAAA,CAAA;AAC1C,EAAA,MAAM,CAAC,UAAA,EAAY,aAAa,CAAA,GAAI,SAA4B,IAAI,CAAA,CAAA;AACpE,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,IAAI,KAA6B,GAAA,IAAA,CAAA;AACjC,IAAA,IAAI,OAAS,EAAA;AACX,MAAA,MAAM,EAAE,WAAY,EAAA,GAAI,QAAQ,SAAU,CAAA,OAAA,EAAS,CAAC,GAAoB,KAAA;AACtE,QAAA,aAAA,CAAc,GAAG,CAAA,CAAA;AAAA,OAClB,CAAA,CAAA;AACD,MAAQ,KAAA,GAAA,WAAA,CAAA;AAAA,KACV;AACA,IAAA,OAAO,MAAM;AACX,MAAA,IAAI,WAAW,KAAO,EAAA;AACpB,QAAM,KAAA,EAAA,CAAA;AAAA,OACR;AAAA,KACF,CAAA;AAAA,GACC,EAAA,CAAC,OAAS,EAAA,OAAO,CAAC,CAAA,CAAA;AAErB,EAAA,OAAO,EAAE,UAAW,EAAA,CAAA;AACtB;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@backstage/plugin-signals-react",
|
|
3
|
+
"description": "Web library for the signals plugin",
|
|
4
|
+
"version": "0.0.0-nightly-20240118021622",
|
|
5
|
+
"main": "dist/index.esm.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public",
|
|
10
|
+
"main": "dist/index.esm.js",
|
|
11
|
+
"types": "dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"backstage": {
|
|
14
|
+
"role": "web-library"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"scripts": {
|
|
18
|
+
"start": "backstage-cli package start",
|
|
19
|
+
"build": "backstage-cli package build",
|
|
20
|
+
"lint": "backstage-cli package lint",
|
|
21
|
+
"test": "backstage-cli package test",
|
|
22
|
+
"clean": "backstage-cli package clean",
|
|
23
|
+
"prepack": "backstage-cli package prepack",
|
|
24
|
+
"postpack": "backstage-cli package postpack"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@backstage/core-plugin-api": "^1.8.2",
|
|
28
|
+
"@backstage/types": "^1.1.1",
|
|
29
|
+
"@material-ui/core": "^4.12.4"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": "^16.13.1 || ^17.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@backstage/cli": "^0.0.0-nightly-20240118021622",
|
|
36
|
+
"@backstage/test-utils": "^0.0.0-nightly-20240118021622",
|
|
37
|
+
"@testing-library/jest-dom": "^6.0.0",
|
|
38
|
+
"@testing-library/react": "^14.0.0"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"module": "./dist/index.esm.js"
|
|
44
|
+
}
|