Skip to content
Preview: packages are not published yet.

Contracts, provide and consume

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-contract command of the package, checks it and writes the TypeScript, Swift and Kotlin code for both sides.
  • The React side is written: useContract, consume, useValue, useProvide and provide.
  • 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 ContractError whose reason is runtime-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.

It carries no scope, no side and no props. The return type decides what each member is.

shop.contract.ts
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.

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 it
const 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), and consume(C) outside components, return a reference. Each call goes to whoever provides C at that moment.
  • useValue(ref) re-renders when the value changes.
  • useProvide(C, implementation, { enabled }) provides C while the component is mounted.
  • provide(C, implementation) provides C outside components and returns a handle with withdraw() and setEnabled(enabled).

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:

  1. The app named the surface. Use that surface’s enabled line. None: fail at once, nobody is called.
  2. The app named nobody. Of the enabled lines whose surface is active, call the one that became active last.
  3. No active surface provides it. Call the line that has no surface, the module-level provider.
  4. 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.