This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
/
index.ts
83 lines (72 loc) · 2.89 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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/
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright 2018-2021 Cruise LLC
//
// This source code is licensed under the Apache License, Version 2.0,
// found at http://www.apache.org/licenses/LICENSE-2.0
// You may not use this file except in compliance with the License.
import React, { ReactElement, useMemo } from "react";
import { Time } from "rosbag";
import * as PanelAPI from "@foxglove-studio/app/PanelAPI";
import { getTopicsFromPaths } from "@foxglove-studio/app/components/MessagePathSyntax/parseRosPath";
import {
MessagePathDataItem,
useDecodeMessagePathsForMessagesByTopic,
} from "@foxglove-studio/app/components/MessagePathSyntax/useCachedGetMessagePathDataItems";
import { Message } from "@foxglove-studio/app/players/types";
import { useShallowMemo } from "@foxglove-studio/app/util/hooks";
export type MessageHistoryItem = {
queriedData: MessagePathDataItem[];
message: Message;
};
export type MessageHistoryItemsByPath = Readonly<{
[key: string]: ReadonlyArray<MessageHistoryItem>;
}>;
export type MessageHistoryData = {
itemsByPath: MessageHistoryItemsByPath;
startTime: Time;
};
type Props = {
children: (arg0: MessageHistoryData) => ReactElement;
paths: string[];
historySize?: number;
};
const ZERO_TIME = Object.freeze({ sec: 0, nsec: 0 });
// DEPRECATED in favor of PanelAPI.useMessagesByTopic and useCachedGetMessagePathDataItems.
//
// Be sure to pass in a new render function when you want to force a rerender.
// So you probably don't want to do `<MessageHistoryDEPRECATED>{this._renderSomething}</MessageHistoryDEPRECATED>`.
// This might be a bit counterintuitive but we do this since performance matters here.
export default React.memo<Props>(function MessageHistoryDEPRECATED({
// eslint-disable-next-line react/prop-types
children,
// eslint-disable-next-line react/prop-types
paths,
// eslint-disable-next-line react/prop-types
historySize,
}: Props) {
const { startTime } = PanelAPI.useDataSourceInfo();
const memoizedPaths: string[] = useShallowMemo<string[]>(paths);
const subscribeTopics = useMemo(() => getTopicsFromPaths(memoizedPaths), [memoizedPaths]);
const messagesByTopic = PanelAPI.useMessagesByTopic({
topics: subscribeTopics,
historySize: historySize || Infinity,
});
const decodeMessagePathsForMessagesByTopic = useDecodeMessagePathsForMessagesByTopic(
memoizedPaths,
);
const itemsByPath = useMemo(() => decodeMessagePathsForMessagesByTopic(messagesByTopic), [
decodeMessagePathsForMessagesByTopic,
messagesByTopic,
]);
return useMemo(() => children({ itemsByPath, startTime: startTime || ZERO_TIME }), [
children,
itemsByPath,
startTime,
]);
});