flushUICommand function

void flushUICommand()

Implementation

void flushUICommand() {
  Map<int, KrakenController?> controllerMap =
      KrakenController.getControllerMap();
  for (KrakenController? controller in controllerMap.values) {
    if (controller == null) continue;
    Pointer<Uint64> nativeCommandItems =
        _getUICommandItems(controller.view.contextId);
    int commandLength = _getUICommandItemSize(controller.view.contextId);

    if (commandLength == 0 || nativeCommandItems == nullptr) {
      continue;
    }

    if (kProfileMode) {
      PerformanceTiming.instance().mark(PERF_FLUSH_UI_COMMAND_START);
    }

    List<UICommand> commands = readNativeUICommandToDart(
        nativeCommandItems, commandLength, controller.view.contextId);

    SchedulerBinding.instance!.scheduleFrame();

    if (kProfileMode) {
      PerformanceTiming.instance().mark(PERF_FLUSH_UI_COMMAND_END);
    }

    Map<int, bool> pendingStylePropertiesTargets = {};

    // For new ui commands, we needs to tell engine to update frames.
    for (int i = 0; i < commandLength; i++) {
      UICommand command = commands[i];
      UICommandType commandType = command.type;
      int id = command.id;
      Pointer nativePtr = command.nativePtr;

      try {
        switch (commandType) {
          case UICommandType.createElement:
            controller.view.createElement(
                id, nativePtr.cast<NativeBindingObject>(), command.args[0]);
            break;
          case UICommandType.createTextNode:
            controller.view.createTextNode(
                id, nativePtr.cast<NativeBindingObject>(), command.args[0]);
            break;
          case UICommandType.createComment:
            controller.view
                .createComment(id, nativePtr.cast<NativeBindingObject>());
            break;
          case UICommandType.disposeEventTarget:
            controller.view.disposeEventTarget(id);
            break;
          case UICommandType.addEvent:
            controller.view.addEvent(id, command.args[0]);
            break;
          case UICommandType.removeEvent:
            controller.view.removeEvent(id, command.args[0]);
            break;
          case UICommandType.insertAdjacentNode:
            int childId = int.parse(command.args[0]);
            String position = command.args[1];
            controller.view.insertAdjacentNode(id, position, childId);
            break;
          case UICommandType.removeNode:
            controller.view.removeNode(id);
            break;
          case UICommandType.cloneNode:
            int newId = int.parse(command.args[0]);
            controller.view.cloneNode(id, newId);
            break;
          case UICommandType.setStyle:
            String key = command.args[0];
            String value = command.args[1];
            controller.view.setInlineStyle(id, key, value);
            pendingStylePropertiesTargets[id] = true;
            break;
          case UICommandType.setAttribute:
            String key = command.args[0];
            String value = command.args[1];
            controller.view.setAttribute(id, key, value);
            break;
          case UICommandType.removeAttribute:
            String key = command.args[0];
            controller.view.removeAttribute(id, key);
            break;
          case UICommandType.createDocumentFragment:
            controller.view.createDocumentFragment(
                id, nativePtr.cast<NativeBindingObject>());
            break;
          default:
            break;
        }
      } catch (e, stack) {
        print('$e\n$stack');
      }
    }

    // For pending style properties, we needs to flush to render style.
    for (int id in pendingStylePropertiesTargets.keys) {
      try {
        controller.view.flushPendingStyleProperties(id);
      } catch (e, stack) {
        print('$e\n$stack');
      }
    }
    pendingStylePropertiesTargets.clear();
  }
}