createNode method

SimpleNode createNode(
  1. String path, [
  2. bool init = true
])

Creates a node at path. If a node already exists at this path, an exception is thrown. If init is false, onCreated() is not called.

Implementation

SimpleNode createNode(String path, [bool init = true]) {
  var p = Path(path);
  var existing = nodes[path];

  if (existing != null) {
    if (existing is SimpleNode) {
      if (existing._stub != true) {
        throw Exception('Node at $path already exists.');
      } else {
        existing._stub = false;
      }
    } else {
      throw Exception('Node at $path already exists.');
    }
  }

  var node =
      existing == null ? SimpleNode(path, this) : existing as SimpleNode;
  nodes[path] = node;

  if (init) {
    node.onCreated();
  }

  SimpleNode? pnode;

  if (p.parentPath != '') {
    pnode = getNode(p.parentPath) as SimpleNode;
  }

  if (pnode != null) {
    pnode.children[p.name] = node;
    pnode.onChildAdded(p.name, node);
    pnode.updateList(p.name);
  }

  return node;
}