SceneGraphNode.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /// A persistent representation of a scene that maintains its state even when
  2. /// the scene itself gets recomputed.
  3. ///
  4. /// This is required to store view graphs, widget handles, etc.
  5. ///
  6. /// Treat scenes as basic data structures that simply encode the structure of
  7. /// the app; the actual rendering and logic is handled by the node.
  8. @MainActor
  9. public protocol SceneGraphNode: AnyObject {
  10. /// The type of scene managed by this node.
  11. associatedtype NodeScene: Scene where NodeScene.Node == Self
  12. /// Creates a node from a corresponding scene.
  13. ///
  14. /// Should perform initial setup of any widgets required to display the
  15. /// scene (although ``SceneGraphNode/update(backend:environment:)`` is
  16. /// guaranteed to be called immediately after initialization).
  17. ///
  18. /// - Parameters:
  19. /// - scene: The scene to create the node from.
  20. /// - backend: The app's backend.
  21. /// - environment: The current root-level environment.
  22. init<Backend: BaseAppBackend>(
  23. from scene: NodeScene,
  24. backend: Backend,
  25. environment: EnvironmentValues
  26. )
  27. /// Updates the scene's node without committing anything to screen or
  28. /// propagating the update to child views.
  29. ///
  30. /// - Parameters:
  31. /// - newScene: The recomputed scene if the update is due to it being
  32. /// recomputed.
  33. /// - environment: The current root-level environment.
  34. /// - Returns: The result of updating the scene node.
  35. func updateNode(
  36. _ newScene: NodeScene?,
  37. environment: EnvironmentValues
  38. ) -> SceneNodeUpdateResult
  39. /// Updates the scene.
  40. ///
  41. /// Unlike views (which have state), scenes are only ever updated when
  42. /// they're recomputed or immediately after they're created.
  43. ///
  44. /// - Parameters:
  45. /// - backend: The app's backend.
  46. /// - environment: The current environment.
  47. func update<Backend: BaseAppBackend>(
  48. backend: Backend,
  49. environment: EnvironmentValues
  50. )
  51. }