composeSceneAsync function

Future<SceneDocument> composeSceneAsync(
  1. SceneDocument document, {
  2. required AsyncPrefabLoader load,
})

Loads every prefab document document references (transitively, breadth first, each source loaded once) via load, then composes synchronously.

The async counterpart of composeScene: the asset loaders call this so a scene that references prefab files by source path is expanded before realizing. A reference that fails to load throws.

Implementation

Future<SceneDocument> composeSceneAsync(
  SceneDocument document, {
  required AsyncPrefabLoader load,
}) async {
  final loaded = <String, SceneDocument>{};
  final queue = [..._prefabRefs(document)];
  while (queue.isNotEmpty) {
    final ref = queue.removeLast();
    if (loaded.containsKey(ref.key)) continue;
    final prefab = await load(ref);
    loaded[ref.key] = prefab;
    queue.addAll(_prefabRefs(prefab));
  }
  return composeScene(
    document,
    resolve: (ref) {
      final prefab = loaded[ref.key];
      if (prefab == null) {
        throw FsceneFormatException('Unresolved prefab "${ref.key}"');
      }
      return prefab;
    },
  );
}