Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async _getMessages(start: Time, end: Time, topics: string[]): Promise {
// if our topics change we need to clear out the cached ranges, or if we're
// reading from before the first range.
if (
intersection(this._topics, topics).length !== topics.length ||
!this._current ||
TimeUtil.isLessThan(start, this._current.start)
) {
this._topics = topics;
this._current = undefined;
this._next = undefined;
}
let messages = [];
const currentMatches = this._current && this._current.overlaps(start, end);
const nextMatches = this._next && this._next.overlaps(start, end);
if (/*:: this._current && */ currentMatches) {
messages = messages.concat(await this._current.getMessages(start, end));
}
if (/*:: this._next && */ nextMatches) {
messages = messages.concat(await this._next.getMessages(start, end));
}
if ((!currentMatches && !nextMatches) || (this._next && TimeUtil.isGreaterThan(end, this._next.end))) {
let startTime = start;
if (this._isPlaying) {
return; // Only run once
}
this._isPlaying = true;
logger.info("AutomatedRunPlayer._run()");
await this._emitState([], this._providerResult.start);
let currentTime = this._providerResult.start;
this._client.start({
bagLengthMs: toMillis(subtractTimes(this._providerResult.end, this._providerResult.start), "round-up"),
});
const nsBagTimePerFrame = Math.round(this._msPerFrame * this._speed * 1000000);
let frameCount = 0;
while (TimeUtil.isLessThan(currentTime, this._providerResult.end)) {
if (this._error) {
return;
}
const end = TimeUtil.add(currentTime, { sec: 0, nsec: nsBagTimePerFrame });
this._client.markTotalFrameStart();
const messages = await this._getMessages(currentTime, end);
this._client.markFrameRenderStart();
await this._emitState(messages, end);
this._client.markTotalFrameEnd();
this._client.markFrameRenderEnd();
await this._client.onFrameFinished(frameCount);
currentTime = TimeUtil.add(end, { sec: 0, nsec: 1 });
frameCount++;
datatypes?: RosDatatypes,
messages?: Message[],
setSubscriptions?: (string, SubscribePayload[]) => void,
noActiveData?: boolean,
activeData?: $Shape,
capabilities?: string[],
store?: any,
seekPlayback?: (Time) => void,
startTime?: Time,
endTime?: Time,
|}) {
const storeRef = useRef(props.store || configureStore(createRootReducer(createMemoryHistory())));
const startTime = useRef();
let currentTime;
for (const message of props.messages || []) {
if (!startTime.current || TimeUtil.isLessThan(message.receiveTime, startTime.current)) {
startTime.current = message.receiveTime;
}
if (!currentTime || TimeUtil.isLessThan(currentTime, message.receiveTime)) {
currentTime = message.receiveTime;
}
}
const [allSubscriptions, setAllSubscriptions] = useState<{ [string]: SubscribePayload[] }>({});
const flattenedSubscriptions: SubscribePayload[] = useMemo(
() => flatten(Object.keys(allSubscriptions).map((k) => allSubscriptions[k])),
[allSubscriptions]
);
const setSubscriptions = useCallback((id, subs) => setAllSubscriptions((s) => ({ ...s, [id]: subs })), [
setAllSubscriptions,
]);
overlaps(start: Time, end: Time) {
return !TimeUtil.isLessThan(end, this.start) && !TimeUtil.isLessThan(this.end, start);
}
if (!this._topicNames.includes(topic)) {
this._error(
`Requested topic (${topic}) is not in the list of topics published by "initialize" (${JSON.stringify(
this._topicNames
)})`
);
}
}
const messages = await this._provider.getMessages(start, end, topics);
let lastTime: ?Time;
for (const message: DataProviderMessage of messages) {
if (!topics.includes(message.topic)) {
this._error(`message.topic (${message.topic}) was never requested (${JSON.stringify(topics)})`);
}
if (TimeUtil.isLessThan(message.receiveTime, start)) {
this._error(
`message.receiveTime (${formatTimeRaw(message.receiveTime)}) is before start (${formatTimeRaw(start)})`
);
}
if (TimeUtil.isLessThan(end, message.receiveTime)) {
this._error(`message.receiveTime (${formatTimeRaw(message.receiveTime)}) is after end (${formatTimeRaw(end)})`);
}
if (lastTime && TimeUtil.isLessThan(message.receiveTime, lastTime)) {
this._error(
`message.receiveTime (${formatTimeRaw(
message.receiveTime
)}) is before previous message receiveTime (${formatTimeRaw(lastTime)}) -- messages are not sorted by time`
);
}
lastTime = message.receiveTime;
}
const messages = await this._provider.getMessages(start, end, topics);
let lastTime: ?Time;
for (const message: DataProviderMessage of messages) {
if (!topics.includes(message.topic)) {
this._error(`message.topic (${message.topic}) was never requested (${JSON.stringify(topics)})`);
}
if (TimeUtil.isLessThan(message.receiveTime, start)) {
this._error(
`message.receiveTime (${formatTimeRaw(message.receiveTime)}) is before start (${formatTimeRaw(start)})`
);
}
if (TimeUtil.isLessThan(end, message.receiveTime)) {
this._error(`message.receiveTime (${formatTimeRaw(message.receiveTime)}) is after end (${formatTimeRaw(end)})`);
}
if (lastTime && TimeUtil.isLessThan(message.receiveTime, lastTime)) {
this._error(
`message.receiveTime (${formatTimeRaw(
message.receiveTime
)}) is before previous message receiveTime (${formatTimeRaw(lastTime)}) -- messages are not sorted by time`
);
}
lastTime = message.receiveTime;
}
return messages;
}
);
}
}
const messages = await this._provider.getMessages(start, end, topics);
let lastTime: ?Time;
for (const message: DataProviderMessage of messages) {
if (!topics.includes(message.topic)) {
this._error(`message.topic (${message.topic}) was never requested (${JSON.stringify(topics)})`);
}
if (TimeUtil.isLessThan(message.receiveTime, start)) {
this._error(
`message.receiveTime (${formatTimeRaw(message.receiveTime)}) is before start (${formatTimeRaw(start)})`
);
}
if (TimeUtil.isLessThan(end, message.receiveTime)) {
this._error(`message.receiveTime (${formatTimeRaw(message.receiveTime)}) is after end (${formatTimeRaw(end)})`);
}
if (lastTime && TimeUtil.isLessThan(message.receiveTime, lastTime)) {
this._error(
`message.receiveTime (${formatTimeRaw(
message.receiveTime
)}) is before previous message receiveTime (${formatTimeRaw(lastTime)}) -- messages are not sorted by time`
);
}
lastTime = message.receiveTime;
}
return messages;
}