Native API: facade, runtime and hosts
What a native app calls: the facade generated for your artifact, then the runtime and the hosts of the base API, which are the same in every artifact.
The examples use an artifact named ShopReact with a surface PointsHistory. Your artifact has its own names; the shapes are the same.
The types these calls take and return are in Native API: types.
Modules and packages
Section titled “Modules and packages”| Platform | Generated facade | Base API |
|---|---|---|
| iOS | Module ShopReact (the artifact name) |
The same module. One import ShopReact gives you both. |
| Android | Package <group>.<name in lower case>, for example com.example.shopreact |
Package nitroui.brownfield |
If you integrate from source, link the NitroBrownfieldCore product and write import NitroBrownfield. From source you call the base API, for example SurfaceDescriptor and SurfaceViewController; the typed facade is generated into a packaged artifact by nuib package.
Artifact entry type
Section titled “Artifact entry type”Generated facade
One type named after the artifact. It starts the runtime with the content packaged in the artifact and lists the surfaces.
public enum ShopReact { public enum Surface: String, CaseIterable, Sendable { case pointsHistory = "PointsHistory" public var name: String }
public static let surfaces: [Surface]
@MainActor public static func start( policy: BootPolicy = .lazy)}object ShopReact { enum class Surface( val surfaceName: String) { POINTS_HISTORY("PointsHistory"), }
val surfaces: List<Surface>
fun start( application: Application, policy: BootPolicy = BootPolicy.LAZY)}| Member | What it does |
|---|---|
start |
Configures the runtime with the content packaged in the artifact and the given BootPolicy. Call it once, early: in application(_:didFinishLaunchingWithOptions:) on iOS and in Application.onCreate on Android. It is a shortcut for BrownfieldRuntime.configure, and the same rules apply, including what a second call does. |
surfaces |
Every surface of the artifact, in name order. |
Surface |
One case per surface. The registered name is name (and the raw value) in Swift and surfaceName in Kotlin. |
Surface types
Section titled “Surface types”Generated facade
One type per surface, named after the surface. Every function takes the surface’s props as typed parameters, shown here as userId, filter and compact.
public enum PointsHistory { public static let name = "PointsHistory"
public enum Filter: String, CaseIterable, Sendable { case lastMonth, all }
public static func props( userId: String, filter: Filter? = nil, compact: Bool? = nil) -> SurfaceProps
public static func descriptor( userId: String, filter: Filter? = nil, compact: Bool? = nil) -> SurfaceDescriptor
@MainActor public static func viewController( userId: String, filter: Filter? = nil, compact: Bool? = nil) -> SurfaceViewController
@MainActor public static func view( userId: String, filter: Filter? = nil, compact: Bool? = nil) -> SurfaceHostView}object PointsHistory { const val NAME: String = "PointsHistory"
enum class Filter(val value: String) { LAST_MONTH("lastMonth"), ALL("all"), }
fun props( userId: String, filter: Filter? = null, compact: Boolean? = null): Map<String, Any?>
fun descriptor( userId: String, filter: Filter? = null, compact: Boolean? = null): SurfaceDescriptor
fun fragment( userId: String, filter: Filter? = null, compact: Boolean? = null): SurfaceFragment
fun view( context: Context, userId: String, filter: Filter? = null, compact: Boolean? = null): SurfaceHostView}| Member | Returns | What it does |
|---|---|---|
name / NAME |
String |
The registered name of the surface. |
viewController(...) |
SurfaceViewController |
iOS only. A full-screen host with a new descriptor. Push or present it like any view controller. |
fragment(...) |
SurfaceFragment |
Android only. A full-screen host with a new descriptor. Add it with your own FragmentManager. |
view(...) |
SurfaceHostView |
An embedded host with a new descriptor. Place it in your layout. On Android the first parameter is a Context. |
descriptor(...) |
SurfaceDescriptor |
A descriptor with a new id and no host. Use it when you build the host yourself or want to keep the descriptor. |
props(...) |
SurfaceProps / Map<String, Any?> |
The props as JSON values, for updateProps on an open host. |
How props map to parameters
Section titled “How props map to parameters”Generated facade
Parameters keep the order of the props in the React component. Types nested in a surface, such as enums and value types, are declared inside the surface type.
| TypeScript prop | Swift | Kotlin |
|---|---|---|
string |
String |
String |
number |
Double |
Double |
boolean |
Bool |
Boolean |
Optional prop x?: T |
x: T? = nil. A nil value is left out of the props. |
x: T? = null. A null value is left out of the props. |
Literal union 'lastMonth' | 'all' |
Nested enum with String raw values: Filter.lastMonth |
Nested enum with a value: Filter.LAST_MONTH |
| Nested object | Nested struct with a memberwise init, Sendable and Equatable |
Nested data class |
Array T[] |
[T] |
List<T> |
BrownfieldRuntime
Section titled “BrownfieldRuntime”Base API
Owns the one JavaScript runtime of the process and decides when it boots. A singleton: BrownfieldRuntime.shared in Swift, the object BrownfieldRuntime in Kotlin.
@MainActorpublic final class BrownfieldRuntime { public static let shared: BrownfieldRuntime
public func configure( bundle: BundleSource, bootPolicy: BootPolicy = .lazy)
public private(set) var state: RuntimeState public private(set) var lastBootReport: BootReport? public weak var observer: BrownfieldRuntimeObserver?
public func restart() public func restartOnNextTurn() public func handleMemoryPressure()}object BrownfieldRuntime { fun configure( application: Application, bundle: BundleSource = BundleSource.Asset(), bootPolicy: BootPolicy = BootPolicy.LAZY)
val state: RuntimeState val lastBootReport: BootReport?
fun addListener(l: BrownfieldRuntimeListener) fun removeListener(l: BrownfieldRuntimeListener) var defaultSurfaceListener: SurfaceHostListener?
fun restart() fun handleMemoryPressure()}| Member | What it does |
|---|---|
configure |
Sets where the JavaScript comes from (BundleSource) and when the runtime boots (BootPolicy). With the eager policy it boots before it returns. A boot failure becomes state == failed, never a crash. Hosts opened before this call wait in mounting; the call then boots at once, whatever the policy, and mounts them or fails them.A later call replaces the configuration. If the runtime already booted or failed, it is destroyed and boots again from the new bundle, and hosts that were mounting or live mount again from their descriptors. On Android, call it in Application.onCreate so that hosts restored after process death find a configured runtime. |
state |
The current RuntimeState: notStarted, booting(stage), ready or failed(failure). Kotlin: NotStarted, Booting(stage), Ready, Failed(failure). The failure is a SurfaceFailure. |
lastBootReport |
The BootReport of the most recent boot, or nil / null before the first one. |
observer |
Swift. One weak BrownfieldRuntimeObserver. |
addListenerremoveListener |
Kotlin. Any number of BrownfieldRuntimeListener. |
defaultSurfaceListener |
Kotlin. The listener used by a host that has none of its own, for example a host the system restored before your code ran. See the lookup order. |
restart() |
Destroys the runtime and boots again with the same configuration. Hosts that were mounting or live go back to described, then mounting, and mount again from their descriptors. React state inside them is lost. Does nothing before the first configure. |
restartOnNextTurn() |
Swift only. The same restart, always on the next main run-loop turn and never inside the current call. |
handleMemoryPressure() |
Asks the JavaScript engine to collect garbage. Call it from your memory warning handler if you want to. |
BootPolicy
Section titled “BootPolicy”Base API
| Swift | Kotlin | What it does |
|---|---|---|
.lazy |
LAZY |
Default. Nothing runs until the first host opens. The runtime then boots and the surface mounts. |
.eager |
EAGER |
The runtime boots inside configure / start, before the call returns. The first surface only pays for its own mount. |
.idle |
IDLE |
Today it behaves exactly as lazy, and the boot report shows appliedPolicy as lazy. |
BundleSource
Section titled “BundleSource”Base API
Where the JavaScript comes from. You only need it when you call BrownfieldRuntime.configure yourself; the generated start picks the content packaged in the artifact.
public enum BundleSource: Sendable { case embedded( path: String = "main.hbc", bundle: Bundle = .main) case file(URL) case scripts([ScriptFile])}
public struct ScriptFile { public let file: URL public let sourceURL: URL public init(file: URL, sourceURL: URL)}sealed interface BundleSource { data class Asset( val path: String = "main.js") : BundleSource data class File( val file: java.io.File) : BundleSource data class Scripts( val scripts: List<Script>) : BundleSource data class Script( val file: java.io.File, val sourceURL: String)}| Case | What it does |
|---|---|
embedded / Asset |
A file shipped with the app: on iOS a path inside the given Bundle, on Android an asset path. |
file / File |
A file on disk. On Android it is copied to a private snapshot first, so rewriting the file does not change a running runtime. |
scripts / Scripts |
Several script files evaluated in order, each with the source URL that stack traces show. Used for development loading. |
Surface hosts
Section titled “Surface hosts”Base API
A host is the native container that shows one React surface. All hosts implement SurfaceHost. The generated surface types build them for you; the constructors below take a descriptor you made or restored yourself.
open class SurfaceViewController: UIViewController, SurfaceHost { public init( descriptor: SurfaceDescriptor, runtime: BrownfieldRuntime = .shared)}
public final class SurfaceHostView: UIView, SurfaceHost { public init( descriptor: SurfaceDescriptor, runtime: BrownfieldRuntime = .shared)}open class SurfaceFragment : Fragment(), SurfaceHost { var listener: SurfaceHostListener? companion object { @JvmStatic fun newInstance( descriptor: SurfaceDescriptor ): SurfaceFragment }}
class SurfaceHostView( context: Context, descriptor: SurfaceDescriptor) : FrameLayout(context), SurfaceHost { var listener: SurfaceHostListener?}| Host | Use | Opens when | Closes when |
|---|---|---|---|
SurfaceViewControlleriOS |
Full screen | viewDidLoad |
close(), or the controller is released. A release without close() still reports closed to the delegate. |
SurfaceHostViewiOS |
Embedded block | The first time it enters a window. It mounts once it has a size. | close(), or the view is released. Leaving the window does not close it. |
SurfaceFragmentAndroid |
Full screen or part of a screen | onViewCreated, the first time only |
close(), or the fragment is removed for good. A fragment on the back stack and a configuration change keep the same React tree. |
SurfaceHostViewAndroid |
Embedded block | One main-looper message after it is attached to a window | close(), or the Activity or Fragment view that owns it is destroyed for good. Detaching alone does not close it. |
The height of an embedded view comes from your own layout. init(coder:) is unavailable on iOS and there is no XML constructor on Android: a host always starts with a descriptor.
Host members
Section titled “Host members”Base API
@MainActorpublic protocol SurfaceHost: AnyObject { var descriptor: SurfaceDescriptor { get } var state: SurfaceState { get } var delegate: (any SurfaceHostDelegate)? { get set } var fallbackView: UIView? { get set }
func updateProps( _ props: SurfaceProps) throws func retry() func close()}interface SurfaceHost { val descriptor: SurfaceDescriptor val state: SurfaceState var fallbackViewFactory: ((Context, SurfaceFailure) -> View)?
fun updateProps(props: Map<String, Any?>) fun retry() fun close()}
// on SurfaceFragment and SurfaceHostViewvar listener: SurfaceHostListener?| Member | What it does | Notes |
|---|---|---|
descriptor |
The host’s SurfaceDescriptor. The id and name never change; the props are always the latest ones set. |
Props set from inside a callback show up on the next main-loop turn. |
state |
The current SurfaceState. |
|
delegatelistener |
Receives state changes and timings. See delegates and listeners. | Swift holds the delegate weakly. On Android the property is not restored after re-creation. |
fallbackView |
Swift. A native view shown while the surface is failed. |
nil leaves the container empty. |
fallbackViewFactory |
Kotlin. Builds the view shown while the surface is failed, from a Context and the SurfaceFailure. |
null leaves the container empty. Not restored when the system re-creates a host; set it again. |
updateProps(props) |
Replaces the props of the surface. A live surface renders again with them and stays live. Pass the result of the generated props(...). |
Validated at once: invalid props throw SurfaceDescriptorError (Swift) or IllegalArgumentException (Kotlin) and nothing changes. A failed surface keeps valid props for its next retry(). A closed surface ignores the call. |
retry() |
Mounts the same descriptor again: same id, latest props. Moves failed to mounting. |
Does nothing unless the surface is failed. After a boot cause, the failed runtime is discarded and a new one boots. After a duplicate id, it succeeds once the other host has closed. |
close() |
Unmounts the surface and moves it to closed. |
Safe to call more than once. Final: a closed host never opens again. |
Threading
Section titled “Threading”Base API
Everything on this page is main-thread only. In Swift the runtime, the hosts, the delegate protocols and the generated start, viewController and view are @MainActor. In Kotlin, calling BrownfieldRuntime or a host from another thread throws IllegalStateException.
Value types such as SurfaceDescriptor, SurfaceState, SurfaceFailure and the reports are Sendable in Swift and immutable in Kotlin, so you can pass them to other threads.