Skip to content

Commit f0df35a

Browse files
committed
fix: update the code to handle multiple seek messages.
1 parent bdffc4f commit f0df35a

File tree

1 file changed

+23
-90
lines changed

1 file changed

+23
-90
lines changed

web/libs/editor/src/tags/object/TimeSeries.jsx

Lines changed: 23 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -592,107 +592,40 @@ const Model = types
592592
return;
593593
}
594594
if (data.initiator === self.name) return;
595-
const [minKey, maxKey] = self.keysRange;
596-
if (minKey === undefined || maxKey === undefined) {
597-
// console.warn("TimeSeries _handleSeek: keysRange not available.");
598-
return;
599-
}
600-
601-
// data.time is received in relative seconds from the start of the series.
602-
// Convert relative seconds to the native units of keysRange.
603-
let targetNative; // This will be the seek target in native units
604-
if (self.isDate) {
605-
// If data is date-based, native units are milliseconds.
606-
targetNative = minKey + (data.time * 1000);
607-
} else {
608-
// If data is numeric (seconds/indices), native units are the same as relative seconds (assuming minKey is the offset).
609-
targetNative = minKey + data.time;
610-
}
611-
612-
const timeRangeDurationNative = maxKey - minKey; // Duration in native units
613-
614-
if (self.canvasWidth <= 0 || timeRangeDurationNative <= 0 || data.time === undefined) {
615-
// console.warn("TimeSeries Sync: Essential data missing for seek operation.");
616-
return;
617-
}
618-
619-
// Validate targetNative against the native range
620-
if (targetNative < minKey || targetNative > maxKey) {
621-
// console.warn(`TimeSeries Sync: Target time ${targetNative} (from rel ${data.time}s) is outside the native range [${minKey}, ${maxKey}]. Ignoring.`);
595+
if (typeof data.time !== 'number' || isNaN(data.time)) {
596+
// console.error("TimeSeries _handleSeek: Invalid data.time received.", data.time);
622597
return;
623598
}
624599

625-
// All subsequent calculations use native units.
626-
const centerPx = ((targetNative - minKey) / timeRangeDurationNative) * self.canvasWidth;
627-
let currentBrushWidthNative = self.brushRange[1] - self.brushRange[0]; // brushRange is in native units
628-
629-
if (currentBrushWidthNative <= 0) {
630-
currentBrushWidthNative = timeRangeDurationNative * 0.1;
631-
if (currentBrushWidthNative <= 0) {
632-
// console.warn("TimeSeries Sync: Cannot determine valid native brush width.");
633-
return;
634-
}
635-
}
636-
637-
const currentPixelWidth = (currentBrushWidthNative / timeRangeDurationNative) * self.canvasWidth;
638-
if (currentPixelWidth <= 0) {
639-
// console.warn("TimeSeries Sync: Cannot determine valid pixel width for brush.");
600+
const [minKey] = self.keysRange; // Native unit
601+
if (minKey === undefined) {
602+
// console.warn("TimeSeries _handleSeek: minKey is undefined.");
640603
return;
641604
}
642605

643-
let newPixelStart = centerPx - currentPixelWidth / 2;
644-
let newPixelEnd = centerPx + currentPixelWidth / 2;
645-
646-
if (newPixelStart < 0) {
647-
const widthPx = newPixelEnd - newPixelStart;
648-
newPixelStart = 0;
649-
newPixelEnd = Math.min(widthPx, self.canvasWidth);
650-
}
651-
if (newPixelEnd > self.canvasWidth) {
652-
const widthPx = newPixelEnd - newPixelStart;
653-
newPixelEnd = self.canvasWidth;
654-
newPixelStart = Math.max(0, self.canvasWidth - widthPx);
655-
}
656-
newPixelStart = Math.max(0, newPixelStart);
657-
newPixelEnd = Math.min(self.canvasWidth, newPixelEnd);
658-
659-
if (newPixelStart >= newPixelEnd) {
660-
const defaultPixelWidth = Math.max(10, self.canvasWidth * 0.1);
661-
newPixelStart = centerPx - defaultPixelWidth / 2;
662-
newPixelEnd = centerPx + defaultPixelWidth / 2;
663-
if (newPixelStart < 0) { newPixelEnd -= newPixelStart; newPixelStart = 0; }
664-
if (newPixelEnd > self.canvasWidth) { newPixelStart -= newPixelEnd - self.canvasWidth; newPixelEnd = self.canvasWidth; }
665-
newPixelStart = Math.max(0, newPixelStart);
666-
newPixelEnd = Math.min(self.canvasWidth, newPixelEnd);
667-
if (newPixelStart >= newPixelEnd) {
668-
// console.error("TimeSeries Sync: Failed to calculate valid pixel range even with fallback.");
669-
return;
670-
}
606+
// Convert received relative seconds to native units for view update
607+
let targetNativeForSeek;
608+
if (self.isDate) {
609+
targetNativeForSeek = minKey + (data.time * 1000);
610+
} else {
611+
targetNativeForSeek = minKey + data.time;
671612
}
672613

673-
// Convert pixel range back to native time units
674-
let newTimeStartNative = minKey + (newPixelStart / self.canvasWidth) * timeRangeDurationNative;
675-
let newTimeEndNative = minKey + (newPixelEnd / self.canvasWidth) * timeRangeDurationNative;
676-
677-
// Fallback logic for native time range calculation
678-
if (!isFinite(newTimeStartNative) || !isFinite(newTimeEndNative) || newTimeStartNative >= newTimeEndNative) {
679-
// console.warn(`TimeSeries Sync: Calculated invalid native time range [${newTimeStartNative}, ${newTimeEndNative}]. Recalculating.`);
680-
newTimeStartNative = targetNative - currentBrushWidthNative / 2;
681-
newTimeEndNative = targetNative + currentBrushWidthNative / 2;
682-
if (newTimeStartNative < minKey) { newTimeEndNative -= newTimeStartNative - minKey; newTimeStartNative = minKey; }
683-
if (newTimeEndNative > maxKey) { newTimeStartNative -= newTimeEndNative - maxKey; newTimeEndNative = maxKey; }
684-
newTimeStartNative = Math.max(minKey, newTimeStartNative);
685-
newTimeEndNative = Math.min(maxKey, newTimeEndNative);
686-
if (!isFinite(newTimeStartNative) || !isFinite(newTimeEndNative) || newTimeStartNative >= newTimeEndNative) {
687-
// console.error("TimeSeries Sync: Failed to calculate fallback native time range.");
688-
return;
614+
// If we're currently playing, we need to restart the playback loop with the new time
615+
if (self.isPlaying) {
616+
// Cancel the current animation frame
617+
if (self.animationFrameId) {
618+
cancelAnimationFrame(self.animationFrameId);
619+
self.animationFrameId = null;
689620
}
621+
// Update the play start position to the new time
622+
self.playStartPosition = data.time;
623+
self.playStartTime = performance.now();
624+
// Restart the playback loop
625+
self.playbackLoop();
690626
}
691627

692-
if (isFinite(newTimeStartNative) && isFinite(newTimeEndNative)) {
693-
self.updateTR([newTimeStartNative, newTimeEndNative], self.scale); // updateTR expects native units
694-
self.seekTo = targetNative; // seekTo stores native units for internal overview update
695-
}
628+
self._updateViewForTime(targetNativeForSeek); // _updateViewForTime expects native units
696629
},
697630

698631
_handlePlay(data) { // data.time is received in relative seconds

0 commit comments

Comments
 (0)