Home

Are there any cleanup hooks for dumped items?

edited June 4

I created a little component that makes dumped content a little more interactive allowing you to toggle its state through code or clicking on it. It's not using Component to render the controls, just simple hyperlinqs and dump containers. In order to make it interactive, It requires registering change handlers to the component within the ToDump() method which was necessary to detect the changes and update. By registering event handlers within this ToDump() method on the main object, I fear I might be leaking memory and I want to be able to clean it up, particularly when I nest this component within itself.

    public interface ICollapsible<T>
    {
        T Object { get; }
        object Label { get; }
        bool IsExpanded { get; set; }
        void Toggle() => IsExpanded = !IsExpanded;
        void Expand() => IsExpanded = true;
        void Collapse() => IsExpanded = false;
    }

    private class _Collapsible<T> : ICollapsible<T>, INotifyPropertyChanged
    {
        object ToDump()
        {
            var container = new DumpContainer();
            PropertyChanged -= OnPropertyChanged;
            PropertyChanged += OnPropertyChanged;
            Update();
            return container;

            void Update()
            {
                container.Content = isExpanded
                    ? Util.VerticalRun(
                        Util.HorizontalRun(true, Label, new Hyperlinq(() => IsExpanded = false, "[collapse]")),
                        Object
                    )
                    : Util.VerticalRun(
                        Util.HorizontalRun(true, Label, new Hyperlinq(() => IsExpanded = true, "[expand]"))
                    );
            }
            void OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
            {
                switch (e.PropertyName)
                {
                    case nameof(IsExpanded):
                        Update();
                        break;
                }
            }
        }

        private bool isExpanded;
        public _Collapsible(T obj, object label, bool isExpanded = false)
        {
            Object = obj;
            Label = label ?? throw new ArgumentNullException(nameof(label));
            this.isExpanded = isExpanded;
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        public T Object { get; }
        public object Label { get; }
        public bool IsExpanded
        {
            get => isExpanded;
            set
            {
                var oldValue = isExpanded;
                if (oldValue != value)
                {
                    isExpanded = value;
                    OnPropertyChanged(nameof(IsExpanded));
                }
            }
        }

        private void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

Are there any hooks in linqpad to cleanup an object when it is removed from the results screen?

Comments

Sign In or Register to comment.