Import functions and types from the generated client, using the same name as the .api.ts file:
import { AsyncValue, LiveValue } from 'arkapi'
import { addTodo, listTodos, Todo } from './todos.api'
@Entry
@Component
struct TodosPage {
@State todos: LiveValue<Todo[]> = new LiveValue(listTodos)
@State added: AsyncValue<number> = new AsyncValue()
@State draft: string = ''
build() {
Column({ space: 12 }) {
ForEach(this.todos.data ?? [], (todo: Todo) => {
Text(todo.title)
}, (todo: Todo) => `${todo.id}`)
TextInput({ text: $$this.draft })
Button('Add')
.enabled(!this.added.loading)
.onClick(() => { this.added.run(addTodo(this.draft)) })
Text(this.todos.error ?? this.added.error ?? '')
}
}
}
Plain calls#
Every generated function returns a Promise. await addTodo('Ship it') works anywhere. A failed call rejects with ApiError from arkapi, which has a code and a message.
AsyncValue for actions#
run(promise) sets loading, then data, or error and errorCode. Use it for buttons and forms.
LiveValue for data on screen#
new LiveValue(listTodos) subscribes to a query over one shared connection. Its fields are data (null until the first value), loaded, error, errorCode and connected. loaded turns true with the first answer, so a screen can tell "still loading" from a query that returned null.
The server re-runs live queries after every mutation, HTTP route, upload and job. It sends only results that changed; a growing list sends only its new items. So after addTodo there is nothing to refresh: every screen showing listTodos, on every device, updates.
Nothing re-runs just because time passes. For text that depends on the clock, such as "5 minutes ago", return the timestamp and compute the text on the device.
- For a query with arguments, use
new LiveValue(() => getTodo(id)). The arguments are fixed; for others, create a new LiveValue. - When the connection drops, it reconnects by itself and keeps the last data.
connectedturns false after about three seconds. stop()ends the subscription and keeps the last data. A screen that creates its own LiveValue calls it inaboutToDisappear. Keep LiveValues in@Stateand do not recreate them on every render.
Events for moments#
A LiveValue shows state. An event is a moment the backend pushes to chosen devices: someone is typing, a reaction, "your order is here". Export it from an api file and send it to device ids:
import { caller, event, online } from 'arkapi/server'
export interface Typing { name: string }
export const typing = event<Typing>()
export function setTyping(name: string): void {
const me = caller().deviceId
typing.send(online().filter((device) => device !== me), { name })
}
The generated client has a typed listener, on plus the event name. Start it when the screen appears and stop it when it goes away:
import { Listening } from 'arkapi'
import { onTyping, Typing } from './chat.api'
// inside the @Component struct
private listening: Listening | null = null
aboutToAppear(): void { this.listening = onTyping((event: Typing) => { this.typist = event.name }) }
aboutToDisappear(): void { this.listening?.stop() }
Events are not stored. A device that is offline or has the app closed when an event is sent never gets it, so anything it must see later belongs in the database and a LiveValue.
The library declares the INTERNET permission itself.