updateElement method

void updateElement(
  1. Map<String, Object> oldAttributes,
  2. Map<String, Object> newAttributes,
  3. Map<String, void Function(Event event)> oldListeners,
  4. Map<String, void Function(Event event)> newListeners,
)

Implementation

void updateElement(
    Map<String, Object> oldAttributes,
    Map<String, Object> newAttributes,
    final Map<String, void Function(Event event)> oldListeners,
    final Map<String, void Function(Event event)> newListeners) {
  final node1 = node as Element;
  if (oldAttributes.isNotEmpty || newAttributes.isNotEmpty) {
    final keys = {...oldAttributes.keys, ...newAttributes.keys};
    for (final key in keys) {
      final oldValue = oldAttributes[key];
      final newValue = newAttributes[key];
      if (oldValue != newValue) {
        if (newValue == null || newValue == false) {
          node1.removeAttribute(key);
          oldAttributes.remove(key);
        } else {
          node1.setAttribute(key, newValue);
          oldAttributes[key] = newValue;
        }
      }
    }
  }

  if (oldListeners.isNotEmpty) {
    for (final key in oldListeners.keys) {
      final listener = oldListeners[key];
      final wrapper = wrapListener(listener);
      node1.removeEventListener(key, wrapper);
    }

    oldListeners.clear();
  }

  if (newListeners.isNotEmpty) {
    for (final key in newListeners.keys) {
      final listener = newListeners[key];
      final wrapper = wrapListener(listener);
      node1.addEventListener(key, wrapper);
    }

    oldListeners.addAll(newListeners);
  }
}