Skip to content

Commit 8efb1dc

Browse files
authored
fix: stale prevLastEntry from old series on market switch (#468)
1 parent 4a0936f commit 8efb1dc

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

lib/src/deriv_chart/chart/data_visualization/chart_series/data_series.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,14 @@ abstract class DataSeries<T extends Tick> extends Series {
253253
oldSeries.entries != null &&
254254
entries!.last == oldSeries.entries!.last) {
255255
prevLastEntry = oldSeries.prevLastEntry as IndexedEntry<T>?;
256-
} else if (oldSeries.entries != null) {
256+
} else if (oldSeries.entries != null && _isSameSeries(oldSeries)) {
257257
prevLastEntry = IndexedEntry<T>(
258258
oldSeries.entries!.last as T,
259259
oldSeries.entries!.length - 1,
260260
);
261261
updated = true;
262+
} else {
263+
updated = true;
262264
}
263265
} else {
264266
initialize();
@@ -287,6 +289,24 @@ abstract class DataSeries<T extends Tick> extends Series {
287289
bool isOldDataAvailable(covariant DataSeries<Tick>? oldSeries) =>
288290
oldSeries?.entries?.isNotEmpty ?? false;
289291

292+
/// Returns true when [oldSeries] and this series share the same data source,
293+
/// i.e. this series is an incremental update of [oldSeries] rather than an
294+
/// entirely new dataset (e.g. a market/symbol switch).
295+
///
296+
/// Compares the first entries by epoch and quote: for an incremental update
297+
/// (new tick, new candle) the beginning of the data is unchanged, so the
298+
/// first entries are identical. For a market switch both datasets cover the
299+
/// same time window, but the prices differ, so the first entries will not
300+
/// match and [prevLastEntry] must not carry over.
301+
bool _isSameSeries(DataSeries<Tick> oldSeries) {
302+
if (input.isEmpty || (oldSeries.entries?.isEmpty ?? true)) {
303+
return false;
304+
}
305+
final T newFirst = input.first;
306+
final Tick oldFirst = oldSeries.entries!.first;
307+
return newFirst.epoch == oldFirst.epoch && newFirst.quote == oldFirst.quote;
308+
}
309+
290310
@override
291311
// ignore: avoid_renaming_method_parameters
292312
bool shouldRepaint(ChartData? oldDelegate) {

0 commit comments

Comments
 (0)