|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:collection'; |
| 6 | + |
| 7 | +/// Debug helper that tracks updates to a property. |
| 8 | +/// |
| 9 | +/// Use this in debugging to record stack traces for when a property is updated. |
| 10 | +/// For instance to track the field `bar` change |
| 11 | +/// |
| 12 | +/// class Foo { |
| 13 | +/// Bar bar; |
| 14 | +/// Foo(this.bar); |
| 15 | +/// } |
| 16 | +/// |
| 17 | +/// to |
| 18 | +/// |
| 19 | +/// class Foo { |
| 20 | +/// StackTraceValue<Bar> _bar; |
| 21 | +/// Foo(Bar bar) : _bar = StackTraceValue(bar); |
| 22 | +/// Bar get bar => _bar.value; |
| 23 | +/// void set bar(Bar value) { |
| 24 | +/// _bar.value = value; |
| 25 | +/// } |
| 26 | +/// } |
| 27 | +/// |
| 28 | +class StackTraceValue<T> { |
| 29 | + T _value; |
| 30 | + List<(T, StackTrace)> _stackTraces = []; |
| 31 | + |
| 32 | + StackTraceValue(this._value) { |
| 33 | + _stackTraces.add((_value, StackTrace.current)); |
| 34 | + } |
| 35 | + |
| 36 | + T get value => _value; |
| 37 | + |
| 38 | + void set value(T v) { |
| 39 | + _stackTraces.add((_value, StackTrace.current)); |
| 40 | + _value = v; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Debug helper that tracks updates to a list. |
| 45 | +/// |
| 46 | +/// Use this in debugging to record stack traces for when a list is updated. |
| 47 | +/// For instance to track the list `bars` change |
| 48 | +/// |
| 49 | +/// class Foo { |
| 50 | +/// List<Bar> bars; |
| 51 | +/// Foo(this.bars); |
| 52 | +/// } |
| 53 | +/// |
| 54 | +/// to |
| 55 | +/// |
| 56 | +/// class Foo { |
| 57 | +/// StackTraceList<Bar> _bars; |
| 58 | +/// Foo(List<Bar> bars) : _bars = StackTraceList(bars); |
| 59 | +/// List<Bar> get bars => _bars; |
| 60 | +/// void set bars(List<Bar> value) { |
| 61 | +/// _bars.value = value; |
| 62 | +/// } |
| 63 | +/// } |
| 64 | +/// |
| 65 | +class StackTraceList<T> with ListMixin<T> implements List<T> { |
| 66 | + List<T> _list; |
| 67 | + List<(String, Object?, StackTrace)> _stackTraces = []; |
| 68 | + |
| 69 | + StackTraceList(this._list) { |
| 70 | + _stackTraces.add(('init', _list, StackTrace.current)); |
| 71 | + } |
| 72 | + |
| 73 | + List<T> get value => _list; |
| 74 | + |
| 75 | + void set value(List<T> v) { |
| 76 | + _stackTraces.add(('value', v, StackTrace.current)); |
| 77 | + _list = v; |
| 78 | + } |
| 79 | + |
| 80 | + @override |
| 81 | + void add(T element) { |
| 82 | + _stackTraces.add(('add', element, StackTrace.current)); |
| 83 | + _list.add(element); |
| 84 | + } |
| 85 | + |
| 86 | + @override |
| 87 | + void addAll(Iterable<T> iterable) { |
| 88 | + _stackTraces.add(('add', iterable.toList(), StackTrace.current)); |
| 89 | + _list.addAll(iterable); |
| 90 | + } |
| 91 | + |
| 92 | + @override |
| 93 | + void clear() { |
| 94 | + _stackTraces.add(('clear', null, StackTrace.current)); |
| 95 | + _list.clear(); |
| 96 | + } |
| 97 | + |
| 98 | + @override |
| 99 | + int get length => _list.length; |
| 100 | + |
| 101 | + @override |
| 102 | + void set length(int newLength) { |
| 103 | + _stackTraces.add(('length=', newLength, StackTrace.current)); |
| 104 | + _list.length = newLength; |
| 105 | + } |
| 106 | + |
| 107 | + @override |
| 108 | + T operator [](int index) => _list[index]; |
| 109 | + |
| 110 | + @override |
| 111 | + void operator []=(int index, T value) { |
| 112 | + _stackTraces.add(('[$index]=', value, StackTrace.current)); |
| 113 | + _list[index] = value; |
| 114 | + } |
| 115 | + |
| 116 | + @override |
| 117 | + bool remove(Object? element) { |
| 118 | + _stackTraces.add(('remove', element, StackTrace.current)); |
| 119 | + return _list.remove(element); |
| 120 | + } |
| 121 | +} |
0 commit comments