render method

RenderedView render(
  1. UiNode tree, {
  2. String? replaceViewId,
  3. String? hint,
})

Render a component tree (FR-001).

tree — the component tree to render. replaceViewId — optional id of an existing view to replace. When set, the previous view is marked inactive and a UiRenderEventReplace is emitted; the returned view has the same id as the previous one. When the id does not match any known view, ViewNotFoundException is thrown (spec Edge Cases). hint — optional presentation hint (FR-001).

Throws NoActiveMissionException when no mission is active. Throws UiRenderValidationException when validation fails (FR-002). Throws ViewNotFoundException when replaceViewId is unknown.

Implementation

RenderedView render(UiNode tree, {String? replaceViewId, String? hint}) {
  final missionType = _activeMissionType;
  if (missionType == null) {
    throw NoActiveMissionException();
  }

  // Resolve the active (possibly narrowed) vocabulary (FR-005).
  final schema = vocabularyNarrowing(
    missionType,
    baseSchema,
    config: narrowingConfig,
  );

  // Validate against the active schema (FR-002).
  final result = schema.validate(tree);
  if (!result.valid) {
    channel.emitError(UiRenderValidationException(result));
    throw UiRenderValidationException(result);
  }

  // Compute content hash (FR-008).
  final contentHash = computeContentHash(tree);

  // Resolve the view id: either reuse the replaced view's id or mint a new
  // one. Mark any prior view with the same id as inactive (last-write-wins
  // for rapid render calls — Edge Cases).
  String viewId;
  if (replaceViewId != null) {
    final existing = _views[replaceViewId];
    if (existing == null) {
      throw ViewNotFoundException(replaceViewId);
    }
    viewId = replaceViewId;
    existing.isActive = false;
  } else {
    viewId = _idGenerator();
  }

  final view = RenderedView(
    viewId: viewId,
    tree: tree,
    schemaVersion: schema.schemaVersion,
    contentHash: contentHash,
    hint: hint,
  );
  _views[viewId] = view;

  // Record in mission trace (FR-008).
  recorder.record(view, missionType: missionType);

  // Emit the render event (FR-001, FR-003). For replace calls, emit a
  // `UiRenderEventReplace`; otherwise emit a `UiRenderEventRender`.
  if (replaceViewId != null) {
    channel.emitReplace(replaceViewId, view);
  } else {
    channel.emitRender(view, isPartial: false);
  }
  channel.emitDone(viewId);

  return view;
}