Native API: types
The types of the base API: descriptors, states, failures, the delegate and listener protocols, and the timing reports.
The examples use an artifact named ShopReact with a surface PointsHistory. Your artifact has its own names; the shapes are the same.
The facade, the runtime and the hosts that use these types are in Native API: facade, runtime and hosts.
SurfaceDescriptor
Section titled “SurfaceDescriptor”Base API
Everything a host needs to build or rebuild a surface: a stable id, the registered name and JSON props. Keep it and you can reopen the same surface later.
public struct SurfaceDescriptor: Sendable, Equatable, Codable { public static let maxPropsBytes = 16_384
public let id: String public let name: String public internal(set) var props: SurfaceProps
public init( name: String, props: SurfaceProps = [:]) throws public init( id: String, name: String, props: SurfaceProps) throws
public func encoded() -> Data public static func decode( _ data: Data) throws -> SurfaceDescriptor}class SurfaceDescriptor { val id: String val name: String val props: Map<String, Any?>
fun toJson(): String fun toBundle(): Bundle fun withProps( props: Map<String, Any?>): SurfaceDescriptor
companion object { const val KEY_DESCRIPTOR = "nitroui.brownfield.descriptor" const val MAX_PROPS_BYTES = 16_384
fun create( name: String, props: Map<String, Any?> = emptyMap() ): SurfaceDescriptor fun create( id: String, name: String, props: Map<String, Any?> ): SurfaceDescriptor fun fromJson(json: String): SurfaceDescriptor fun fromBundle(bundle: Bundle): SurfaceDescriptor }}| Member | What it does |
|---|---|
id |
A lowercase UUID v4. Generated when you create a descriptor without one. One id is open in one host at a time. |
name |
The registered surface name. Starts with a letter, then letters, digits, _, . or -, 64 characters at most. |
props |
A JSON object. Change it through updateProps on the host. |
init(...) / create(...) |
Without an id: a descriptor with a new id. With an id: a descriptor you are restoring. |
encoded() / toJson() |
The descriptor as compact JSON: UTF-8 Data in Swift, a String in Kotlin. |
decode(_:) / fromJson(json) |
Reads that JSON back. Rejects a version other than 1. |
toBundle() / fromBundle(bundle)withProps(props) |
Kotlin. The JSON as one String entry under KEY_DESCRIPTOR; a copy with the same id and name and new props. |
maxPropsBytesMAX_PROPS_BYTES |
16384. The largest size of the props as compact UTF-8 JSON. |
Serialized form:
{"v":1,"id":"3f0c0d3e-6a55-4a8f-9c43-0c7e3b7f2a10","name":"PointsHistory","props":{"userId":"42"}}The JSON is compact, with no whitespace. Do not rely on the order of the keys.
Props values
Section titled “Props values”typealias SurfaceProps = [String: SurfaceValue]
public enum SurfaceValue { case null, bool(Bool), number(Double) case string(String) case array([SurfaceValue]) case object([String: SurfaceValue])}// literals work: ["userId": "42"]// Map<String, Any?> holding only// null, Boolean, String// Number (finite)// List<*> of the same// Map<String, *> of the same
val props = mapOf("userId" to "42")| Rule | Swift error (SurfaceDescriptorError) |
Kotlin error |
|---|---|---|
| The name matches the pattern above | .invalidName(String) |
IllegalArgumentException |
| The id is a lowercase UUID v4 | .invalidId(String) |
IllegalArgumentException |
| Props hold only JSON values and every number is finite. On Android, also no cycles and no integer beyond 253 − 1. | .invalidProps(reason: String) |
IllegalArgumentException |
| Props are at most 16384 bytes as compact UTF-8 JSON | .propsTooLarge(bytes: Int) |
IllegalArgumentException |
| The serialized version is 1 | .unsupportedVersion(Int) |
IllegalArgumentException |
These errors go to the caller. They never change the state of a surface.
SurfaceState
Section titled “SurfaceState”Base API
public enum SurfaceState: Sendable, Equatable { case described case mounting case live case failed(SurfaceFailure) case closed case suspended}sealed interface SurfaceState { data object Described data object Mounting data object Live data class Failed(val failure: SurfaceFailure) data object Closed data object Suspended}| State | Meaning | Leaves it when |
|---|---|---|
described |
The host exists with a descriptor. Nothing has been asked of the runtime. The first state, also after a restore. | The host opens (see Opens when): mounting. |
mounting |
The host asked to open. The runtime boots if it has to, then React mounts the surface. A host opened before configure waits here. |
The first render is on screen: live. Something fails: failed. |
live |
The surface is on screen. updateProps keeps it live. Being covered by another screen, going to the background or sitting on the back stack does not change it. |
A render throws: failed. The runtime restarts: described, then mounting. |
failed |
A contained failure with a typed cause. The container is empty or shows your fallback. | retry(): mounting. |
closed |
Final. The surface is unmounted and the host has released it. | Never. |
suspended |
Present in the type, never entered today. Include it in an exhaustive switch or when and do nothing. |
close() moves any state to closed. When the runtime fails to boot, every surface that is mounting fails with the same cause.
SurfaceFailure and SurfaceFailureReason
Section titled “SurfaceFailure and SurfaceFailureReason”Base API
A failure reaches the app as a state, never as a crash.
public struct SurfaceFailure: Error, Sendable, Equatable { public let cause: SurfaceFailureReason public let message: String public let stage: BootStage?}
public enum SurfaceFailureReason: Sendable, Hashable { case bundleMissing case fingerprintMismatch case evaluationError case surfaceNotRegistered case renderException case duplicateSurfaceId(id: String)
public var isBootCause: Bool { get } public var stringValue: String { get }}data class SurfaceFailure( val cause: SurfaceFailureReason, val message: String, val stage: BootStage?)
sealed interface SurfaceFailureReason { data object BundleMissing data object FingerprintMismatch data object EvaluationError data object SurfaceNotRegistered data object RenderException data class DuplicateSurfaceId(val id: String)
val isBootCause: Boolean val serialized: String}message is human-readable text from the underlying error, for logs and not for users. stage is the BootStage that failed, for boot causes, and nil / null otherwise. stringValue (Swift, also its description) and serialized (Kotlin) give the serialized string of the cause.
Failure causes
Section titled “Failure causes”| Cause (Swift / Kotlin) | Serialized string | Boot cause | When it happens | What retry() does |
|---|---|---|---|---|
bundleMissingBundleMissing |
bundle-missing |
Yes | The JavaScript file is not where the bundle source points. | Boots a new runtime. Fails again until the file exists. |
fingerprintMismatchFingerprintMismatch |
fingerprint-mismatch |
Yes | The JavaScript content was built for a different native framework than the one in the app. | Boots a new runtime. Fails again with the same content. |
evaluationErrorEvaluationError |
evaluation-error |
Yes | Any other error while the runtime starts or the JavaScript content loads, such as an exception at the top level of the app code. | Boots a new runtime. |
surfaceNotRegisteredSurfaceNotRegistered |
surface-not-registered |
No | The JavaScript content has no surface with the descriptor’s name. | Mounts again in the same runtime. |
renderExceptionRenderException |
render-exception |
No | A component of this surface threw while rendering, before or after it went live. Only this surface fails. | Mounts again in the same runtime. |
duplicateSurfaceId(id:)DuplicateSurfaceId(id) |
duplicate-surface-id |
No | Another host already has this descriptor id open. Only the second host fails; the first is not affected. The id is not part of the serialized string. | Succeeds once the other host has closed. |
Delegates and listeners
Section titled “Delegates and listeners”Base API
@MainActorpublic protocol SurfaceHostDelegate: AnyObject { func surfaceHost(_ host: any SurfaceHost, didChange state: SurfaceState) func surfaceHost(_ host: any SurfaceHost, didReport timings: SurfaceTimings)}
@MainActorpublic protocol BrownfieldRuntimeObserver: AnyObject { func runtime(_ runtime: BrownfieldRuntime, didChange state: RuntimeState) func runtime(_ runtime: BrownfieldRuntime, didFinishBoot report: BootReport)}interface SurfaceHostListener { fun onSurfaceStateChanged( host: SurfaceHost, state: SurfaceState) fun onSurfaceTimings( host: SurfaceHost, timings: SurfaceTimings) { }}
interface BrownfieldRuntimeListener { fun onRuntimeStateChanged( state: RuntimeState) { } fun onBootFinished( report: BootReport) { }}| Callback | Called when |
|---|---|
surfaceHost(_:didChange:)onSurfaceStateChanged |
The host’s state changes. |
surfaceHost(_:didReport:)onSurfaceTimings |
Once per host, when it first reaches live or failed, with its SurfaceTimings. |
runtime(_:didChange:)onRuntimeStateChanged |
The runtime’s state changes. |
runtime(_:didFinishBoot:)onBootFinished |
A boot ends, with or without a failure. Also after a retry or a restart. |
In Swift both methods of each protocol are required. In Kotlin only onSurfaceStateChanged is; the others have empty defaults.
An Android host uses the first listener it finds: its listener property, then (for a SurfaceFragment) a parent fragment that implements SurfaceHostListener, then the Activity if it implements it, then BrownfieldRuntime.defaultSurfaceListener.
Timing and boot reports
Section titled “Timing and boot reports”Base API
public enum BootStage: String, Sendable { case createHermes, prepareBytecode, evaluate, mount}
public enum StageOutcome: Sendable, Equatable { case ran(durationMs: Double) case alreadyDone case mergedInto(BootStage) case failed(durationMs: Double) case notReached}
public struct StageTiming { public let stage: BootStage public let outcome: StageOutcome}
public struct BootReport { public let requestedPolicy: BootPolicy public let appliedPolicy: BootPolicy public let stages: [StageTiming]}
public struct SurfaceTimings { public let surfaceId: String public let boot: [StageTiming] public let mount: StageTiming}enum class BootStage { CREATE_HERMES, PREPARE_BYTECODE, EVALUATE, MOUNT; val serialized: String}
sealed interface StageOutcome { data class Ran(val durationMs: Double) data object AlreadyDone data class MergedInto(val stage: BootStage) data class Failed(val durationMs: Double) data object NotReached}
data class StageTiming( val stage: BootStage, val outcome: StageOutcome)
data class BootReport( val requestedPolicy: BootPolicy, val appliedPolicy: BootPolicy, val stages: List<StageTiming>)
data class SurfaceTimings( val surfaceId: String, val boot: List<StageTiming>, val mount: StageTiming)Stages
Section titled “Stages”| Stage | Name in logs | What runs | How often |
|---|---|---|---|
createHermesCREATE_HERMES |
createHermes |
The JavaScript engine is created. | Once per runtime |
prepareBytecodePREPARE_BYTECODE |
prepareBytecode |
The JavaScript content is prepared for evaluation. On Android, and on iOS for the file and scripts sources, it is not a separate step and is reported as merged into evaluate. |
Once per runtime |
evaluateEVALUATE |
evaluate |
The JavaScript content runs and registers the surfaces. | Once per runtime |
mountMOUNT |
mount |
One surface mounts, from the request to its first render on screen. | Once per host that opens |
The name in logs is the raw value of the Swift case and serialized in Kotlin.
Outcomes
Section titled “Outcomes”| Outcome | Meaning |
|---|---|
ran(durationMs) |
The stage ran in this boot and took this long. |
alreadyDone |
The runtime was already past this stage. A surface that opens on a ready runtime gets this for the three boot stages. |
mergedInto(stage) |
The stage cannot be measured on its own here. Its time is counted in the named stage. |
failed(durationMs) |
The stage threw after this long. |
notReached |
An earlier stage failed. |
Reports
Section titled “Reports”A BootReport holds the policy you passed, the policy that was used (it differs only for idle, which is applied as lazy) and one StageTiming each for createHermes, prepareBytecode and evaluate, in that order.
A SurfaceTimings holds the descriptor id, the three boot stages as this surface saw them (the surface that caused the boot gets ran, later ones get alreadyDone) and its own mount stage.
Durations are milliseconds from a monotonic clock, with a fractional part. One BootReport is delivered per boot and is also kept in BrownfieldRuntime.lastBootReport. One SurfaceTimings is delivered per host.
Stage lines in the log:
[brownfield] stage=<name> ms=<value># iOS the release artifact logs only when launched with -BrownfieldLogging YES,# in every build configuration of the app; the dev variant always logs# Android debuggable builds log; otherwise adb shell setprop log.tag.brownfield DEBUG