chore: restore back to recipients + add test

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2024-02-01 20:28:04 +02:00
parent dde856b228
commit 7ce5f0009a
9 changed files with 55 additions and 18 deletions
@@ -89,7 +89,7 @@ describe('SignalManager', () => {
await onEvent({
topic: 'signals',
eventPayload: {
receivers: null,
recipients: null,
channel: 'test',
message: { msg: 'test' },
},
@@ -109,7 +109,7 @@ describe('SignalManager', () => {
await onEvent({
topic: 'signals',
eventPayload: {
receivers: null,
recipients: null,
channel: 'test',
message: { msg: 'test' },
},
@@ -162,7 +162,7 @@ describe('SignalManager', () => {
await onEvent({
topic: 'signals',
eventPayload: {
receivers: 'user:default/john.doe',
recipients: 'user:default/john.doe',
channel: 'test',
message: { msg: 'test' },
},
@@ -132,11 +132,11 @@ export class SignalManager {
return;
}
const { channel, receivers, message } = eventPayload;
const { channel, recipients, message } = eventPayload;
const jsonMessage = JSON.stringify({ channel, message });
let users: string[] = [];
if (receivers !== null) {
users = Array.isArray(receivers) ? receivers : [receivers];
if (recipients !== null) {
users = Array.isArray(recipients) ? recipients : [recipients];
}
// Actual websocket message sending
@@ -146,7 +146,7 @@ export class SignalManager {
}
// Sending to all users can be done with null
if (
receivers !== null &&
recipients !== null &&
!conn.ownershipEntityRefs.some((ref: string) => users.includes(ref))
) {
return;
@@ -84,7 +84,7 @@ export async function startStandaloneServer(
setInterval(() => {
signals.publish({
receivers: null,
recipients: null,
channel: 'test',
message: { hello: 'world' },
});
+1 -1
View File
@@ -16,7 +16,7 @@ export class DefaultSignalService implements SignalService {
// @public (undocumented)
export type SignalPayload = {
receivers: string[] | string | null;
recipients: string[] | string | null;
channel: string;
message: JsonObject;
};
@@ -0,0 +1,38 @@
/*
* Copyright 2024 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 { DefaultSignalService } from './DefaultSignalService';
describe('DefaultSignalService', () => {
const mockEventBroker = {
publish: jest.fn(),
subscribe: jest.fn(),
};
const service = DefaultSignalService.create({ eventBroker: mockEventBroker });
it('should publish signal', () => {
const signal = {
channel: 'test-channel',
recipients: null,
message: { msg: 'hello world' },
};
service.publish(signal);
expect(mockEventBroker.publish).toHaveBeenCalledWith({
topic: 'signals',
eventPayload: signal,
});
});
});
@@ -37,14 +37,9 @@ export class DefaultSignalService implements SignalService {
* @param message - message to publish
*/
async publish(signal: SignalPayload) {
const { receivers, channel, message } = signal;
await this.eventBroker?.publish({
topic: 'signals',
eventPayload: {
receivers,
message,
channel,
},
eventPayload: signal,
});
}
}
+1 -1
View File
@@ -25,7 +25,7 @@ export type SignalServiceOptions = {
/** @public */
export type SignalPayload = {
receivers: string[] | string | null;
recipients: string[] | string | null;
channel: string;
message: JsonObject;
};
+1
View File
@@ -27,6 +27,7 @@ export interface SignalSubscriber {
// @public (undocumented)
export const useSignal: (channel: string) => {
lastSignal: JsonObject | null;
isSignalsAvailable: boolean;
};
// (No @packageDocumentation comment for this package)
+5 -2
View File
@@ -16,7 +16,7 @@
import { signalApiRef } from '../api';
import { useApiHolder } from '@backstage/core-plugin-api';
import { JsonObject } from '@backstage/types';
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
/** @public */
export const useSignal = (channel: string) => {
@@ -40,5 +40,8 @@ export const useSignal = (channel: string) => {
};
}, [signals, channel]);
return { lastSignal };
// Can be used to fallback (for example to long polling) if signals are not available in the system
const isSignalsAvailable = useMemo(() => !signals, [signals]);
return { lastSignal, isSignalsAvailable };
};