Code/Core/GraphElement.cs
namespace Nodebox;

public abstract class GraphElement : IValid, DynamicDirectory<GraphElement>.IElement {
    public abstract Type DirectoryType { get; }

    private bool _destroying = false;
    private bool _destroyed = false;
    public virtual bool IsValid => !_destroyed;

    public GraphElement() { }

    public Action<GraphElement> OnDestroy { get; set; }
    public Action<GraphElement> OnDestroyed { get; set; }

    public void Destroy() {
        if (_destroying) { return; }
        _destroying = true;
        OnDestroy?.Invoke(this);
        Term();
        _destroyed = true;
        OnDestroyed?.Invoke(this);
    }

    protected virtual void Term() { }
}