From 0368e6d4b69538ee563d86662d458bb0ad08eea9 Mon Sep 17 00:00:00 2001 From: Franck Labracherie Date: Sun, 28 Jul 2024 22:43:25 +0200 Subject: [PATCH] Move and change messagePipeline selector that maps schemas by topic name --- .../src/selectWithUnstableIdentityWarning.ts | 2 +- .../suite-base/src/PanelAPI/useConfigById.ts | 2 +- .../src/components/MessagePipeline/index.tsx | 5 --- .../MessagePipeline/selectors.test.ts | 43 +++++++++++++++++++ .../components/MessagePipeline/selectors.ts | 15 +++++++ .../PanelExtensionAdapter/renderState.test.ts | 3 +- .../src/components/PanelSettings/index.tsx | 6 +-- 7 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 packages/suite-base/src/components/MessagePipeline/selectors.test.ts create mode 100644 packages/suite-base/src/components/MessagePipeline/selectors.ts diff --git a/packages/hooks/src/selectWithUnstableIdentityWarning.ts b/packages/hooks/src/selectWithUnstableIdentityWarning.ts index 0ff188fde5b..45fe58f1055 100644 --- a/packages/hooks/src/selectWithUnstableIdentityWarning.ts +++ b/packages/hooks/src/selectWithUnstableIdentityWarning.ts @@ -14,7 +14,7 @@ export function selectWithUnstableIdentityWarning(value: T, selector: (val const secondResult = selector(value); if (result !== secondResult) { log.warn(`Selector ${selector.toString()} produced different values for the same input. - This will cause unecesessery re-renders of your component.`); + This will cause unecesessery re-renders of your component.`); } return secondResult; } diff --git a/packages/suite-base/src/PanelAPI/useConfigById.ts b/packages/suite-base/src/PanelAPI/useConfigById.ts index e218055a74c..645f04dceaa 100644 --- a/packages/suite-base/src/PanelAPI/useConfigById.ts +++ b/packages/suite-base/src/PanelAPI/useConfigById.ts @@ -9,7 +9,7 @@ import * as _ from "lodash-es"; import { useCallback } from "react"; import { DeepPartial } from "ts-essentials"; -import { useMessagePipeline } from "@foxglove/studio-base/components/MessagePipeline"; +import { useMessagePipeline } from "@lichtblick/suite-base/components/MessagePipeline"; import { LayoutState, useCurrentLayoutActions, diff --git a/packages/suite-base/src/components/MessagePipeline/index.tsx b/packages/suite-base/src/components/MessagePipeline/index.tsx index 743426d0a83..2cc7e5a2b44 100644 --- a/packages/suite-base/src/components/MessagePipeline/index.tsx +++ b/packages/suite-base/src/components/MessagePipeline/index.tsx @@ -338,8 +338,3 @@ function createPlayerListener(args: { }, }; } - -export const getTopicToSchemaNameMap = ( - state: MessagePipelineContext, -): Record => - _.mapValues(_.keyBy(state.sortedTopics, "name"), ({ schemaName }) => schemaName); diff --git a/packages/suite-base/src/components/MessagePipeline/selectors.test.ts b/packages/suite-base/src/components/MessagePipeline/selectors.test.ts new file mode 100644 index 00000000000..c7c7fb468f3 --- /dev/null +++ b/packages/suite-base/src/components/MessagePipeline/selectors.test.ts @@ -0,0 +1,43 @@ +// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// SPDX-License-Identifier: MPL-2.0 + +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/ + +import { getTopicToSchemaNameMap } from "@lichtblick/suite-base/components/MessagePipeline/selectors"; +import { MessagePipelineContext } from "@lichtblick/suite-base/components/MessagePipeline/types"; +import { PlayerPresence } from "@lichtblick/suite-base/players/types"; + +it("map schema names by topic name", () => { + const state: MessagePipelineContext = { + sortedTopics: [ + { name: "topic1", schemaName: "schema1" }, + { name: "topic2", schemaName: "schema2" }, + ], + playerState: { + presence: PlayerPresence.PRESENT, + progress: {}, + capabilities: [], + profile: undefined, + playerId: "", + }, + callService: jest.fn(), + datatypes: new Map(), + fetchAsset: jest.fn(), + messageEventsBySubscriberId: new Map(), + pauseFrame: jest.fn(), + publish: jest.fn(), + seekPlayback: jest.fn(), + setParameter: jest.fn(), + setPublishers: jest.fn(), + setSubscriptions: jest.fn(), + subscriptions: [], + getMetadata: jest.fn(), + }; + const result = getTopicToSchemaNameMap(state); + expect(result).toEqual({ + topic1: "schema1", + topic2: "schema2", + }); +}); diff --git a/packages/suite-base/src/components/MessagePipeline/selectors.ts b/packages/suite-base/src/components/MessagePipeline/selectors.ts new file mode 100644 index 00000000000..ca031e4baa2 --- /dev/null +++ b/packages/suite-base/src/components/MessagePipeline/selectors.ts @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// SPDX-License-Identifier: MPL-2.0 + +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/ + +import * as _ from "lodash-es"; + +import { MessagePipelineContext } from "@lichtblick/suite-base/components/MessagePipeline/types"; + +export const getTopicToSchemaNameMap = ( + state: MessagePipelineContext, +): Record => + _.mapValues(_.keyBy(state.sortedTopics, "name"), ({ schemaName }) => schemaName); diff --git a/packages/suite-base/src/components/PanelExtensionAdapter/renderState.test.ts b/packages/suite-base/src/components/PanelExtensionAdapter/renderState.test.ts index 43e36751b54..82e72574828 100644 --- a/packages/suite-base/src/components/PanelExtensionAdapter/renderState.test.ts +++ b/packages/suite-base/src/components/PanelExtensionAdapter/renderState.test.ts @@ -7,6 +7,7 @@ import { produce } from "immer"; +import { PanelSettings } from "@lichtblick/suite"; import { PlayerPresence } from "@lichtblick/suite-base/players/types"; import { BuilderRenderStateInput, initRenderStateBuilder } from "./renderState"; @@ -957,7 +958,7 @@ describe("renderState", () => { const state2 = buildRenderState( produce(initialState, (draft) => { draft.currentFrame = undefined; - draft.playerState.progress.messageCache = undefined; + draft.playerState!.progress.messageCache = undefined; draft.subscriptions.push({ topic: "test", convertTo: "anotherSchema", preload: true }); }), ); diff --git a/packages/suite-base/src/components/PanelSettings/index.tsx b/packages/suite-base/src/components/PanelSettings/index.tsx index 6d9447f7394..db4dc2a7b65 100644 --- a/packages/suite-base/src/components/PanelSettings/index.tsx +++ b/packages/suite-base/src/components/PanelSettings/index.tsx @@ -15,10 +15,8 @@ import { SettingsTree } from "@lichtblick/suite"; import { AppSetting } from "@lichtblick/suite-base/AppSetting"; import { useConfigById } from "@lichtblick/suite-base/PanelAPI"; import EmptyState from "@lichtblick/suite-base/components/EmptyState"; -import { - getTopicToSchemaNameMap, - useMessagePipeline, -} from "@lichtblick/suite-base/components/MessagePipeline"; +import { useMessagePipeline } from "@lichtblick/suite-base/components/MessagePipeline"; +import { getTopicToSchemaNameMap } from "@lichtblick/suite-base/components/MessagePipeline/selectors"; import { ActionMenu } from "@lichtblick/suite-base/components/PanelSettings/ActionMenu"; import SettingsTreeEditor from "@lichtblick/suite-base/components/SettingsTreeEditor"; import { ShareJsonModal } from "@lichtblick/suite-base/components/ShareJsonModal";