Contracts, provide and consume
Coming soon
Section titled “Coming soon”Typed communication between the native app and React ships in its own package, nitro-ui-brownfield-contract, after the base library. It is not released, and nothing on this page is part of Nitro UI Brownfield today. This page describes what its code already does:
- A contract is a TypeScript interface. A generator, the
nitro-ui-contractcommand of the package, checks it and writes the TypeScript, Swift and Kotlin code for both sides. - The React side is written:
useContract,consume,useValue,useProvideandprovide. - The Swift and Kotlin sides are written, with the entry point
Contracts(Contracts.provide,Contracts.consume). - The package is not yet connected to the base library, and it has not run on a device. Until it is connected, every call fails with a
ContractErrorwhose reason isruntime-gone.
The rest of this page shows how the API is shaped.
A contract is a typed channel in both directions. Either side can offer something and either side can call it. There are no events and no subscriptions to manage.
To see it move, follow one call step by step.
A contract is a TypeScript interface
Section titled “A contract is a TypeScript interface”It carries no scope, no side and no props. The return type decides what each member is.
import type { Contract, Value } from 'nitro-ui-brownfield-contract'
export interface Shop extends Contract { translate(key: string): string getAccessToken(): Promise<string> openCheckout(order: Order): Promise<Outcome> trackEvent(event: AnalyticsEvent): void session: Value<Session>}The file is shared by both sides. Order, Outcome, AnalyticsEvent and Session are types defined in the same file.
- A call returns a value, a Promise or nothing. A late result is the call’s own result.
- A value is a current value that can change, such as the session.
Provide and consume
Section titled “Provide and consume”Either side may provide any contract. A provider lives as long as whoever provided it.
import { useContract, useValue, useProvide } from 'nitro-ui-brownfield-contract'import { Shop } from './shop.contract.generated'import { Orders } from './orders.contract.generated'
const shop = useContract(Shop)const session = useValue(shop.session) // undefined until someone provides itconst outcome = await shop.openCheckout(order)
useProvide(Orders, { showOrder(id) { nav.push('Order', { id }) return true },})Shop and Orders here are not the interfaces: they are tokens from the TypeScript half that nitro-ui-contract generate writes, one <file>.generated.ts per contract file in the directory given to --js. The interface is only a type, so import the generated token, from wherever your --js directory is.
useContract(C), andconsume(C)outside components, return a reference. Each call goes to whoever providesCat that moment.useValue(ref)re-renders when the value changes.useProvide(C, implementation, { enabled })providesCwhile the component is mounted.provide(C, implementation)providesCoutside components and returns a handle withwithdraw()andsetEnabled(enabled).
final class ShopHost: ShopProvider { let session = ProvidedValue(Session(userId: "", signedIn: false)) func translate(key: String) -> String { … } func getAccessToken() async throws -> String { … } func openCheckout(order: Order) async throws -> Outcome { … } func trackEvent(event: AnalyticsEvent) { … }}let provision = try Contracts.provide(Shop.self, ShopHost())
let shown = try Contracts .consume(Orders.self) .showOrder(id: "A-42")ShopProvider, Order, Outcome and the other types are generated from the contract. Kotlin has the same shape.
Who receives a call
Section titled “Who receives a call”Covered surfaces stay mounted, so “the last one registered” could answer for something the person cannot see. The library keeps one list on the native side and picks exactly one line. No React code runs until that single call.
| Contract | Provided by | Surface | Surface is | Enabled |
|---|---|---|---|---|
Orders |
OrdersScreen | orders-1 | covered | yes |
Orders |
OrdersSheet | orders-2 | active, last | yes |
BackHandler |
EditProfile | profile-1 | active | no |
Loyalty |
module level | none | yes |
In this list, a call to Orders that names no surface goes to the second line.
The library applies four rules in order:
- The app named the surface. Use that surface’s enabled line. None: fail at once, nobody is called.
- The app named nobody. Of the enabled lines whose surface is active, call the one that became active last.
- No active surface provides it. Call the line that has no surface, the module-level provider.
- No line at all. Fail at once with a cause. The app decides what to do.
One call goes to one provider. It is never a broadcast.
Threads
Section titled “Threads”- Board: a call in each direction
- Board: the life of a surface