Code/Core/Graph.cs
namespace Nodebox;


public partial class Graph : DynamicDirectory<GraphElement>, IMeta, IValid {
    public Dictionary<string, object> Meta { get; private set; } = [];

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

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

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

    ~Graph() {
        Destroy();
    }
}