Skip to content

Function: reactNativeServiceEntrypoint() ​

说明

本页由源码中的注释生成,内容为英文。

reactNativeServiceEntrypoint(options?): EntrypointAdapter<ServiceContext<{ }>>

Create a service adapter of an entrypoint served to the user interface of a mobile application of React Native (Android, iOS, HarmonyOS), in the Node.js the native module of the application embeds and starts. The native module gives the Node.js the file descriptor of a socket (the environment variable LAKUTATA_NODE_FD); the adapter reads the messages of the host there and writes its responses and events, one JSON object per line. Any host speaking the same lines may use it (the socket option takes the connection).

The host sends {type: 'invoke', id, clientId?, data} (the action's pattern and input in data), {type: 'subscribe', clientId} or {type: 'unsubscribe', clientId}; the adapter sends {type: 'response', id, response} ({success, error, payload}, see ServiceResponseCodec: payload is the result, or error the whole error object when the action failed), {type: 'event', clientId, event, args} (an event pushed by Entrypoint.notify or Entrypoint.broadcast) or {type: 'subscribed', clientId} (the answer to a subscription, once the application listens). The lines the host writes before the application listens wait in the socket. A result JSON cannot hold (a bigint, a cycle) is answered with an error; the values JSON changes arrive changed (a Date as its string, a Buffer as {type, data}, undefined left out). Each user interface is a client, identified by the id it gives (a non-empty string of at most 200 characters), which its requests carry as ServiceContext.clientId; unsubscribing aborts its requests in progress (their abort signal fires).

{type: 'pause'} (the application going to the background) calls the pause option, then answers {type: 'paused'}; {type: 'resume'} calls the resume option. When the entrypoint stops, the new invocations are answered with an EntrypointStoppedException error; once the requests in progress end, it stops listening and closes the socket it opened.

Parameters ​

options? ​

ReactNativeServiceEntrypointOptions

The adapter options; by default the socket is the one on LAKUTATA_NODE_FD.

Returns ​

EntrypointAdapter<ServiceContext<{ }>>

The adapter, to give to the service option of buildEntrypoints.

Example ​

typescript
import {Application} from 'lakutata'
import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
import {ServiceAction} from 'lakutata/decorator/ctrl'
import {reactNativeServiceEntrypoint} from 'lakutata/adapter/react-native'

class EchoController extends Controller {
    @ServiceAction({cmd: 'echo'})
    public async echo(input: {text: string}): Promise<string> {
        return input.text
    }
}

//In the Node.js of the mobile application
Application.run({
    id: 'mobile.app',
    name: 'Mobile',
    components: {
        entrypoint: buildEntrypoints({
            controllers: [EchoController],
            service: reactNativeServiceEntrypoint()
        })
    },
    bootstrap: ['entrypoint']
})

//The host writes: {"type":"subscribe","clientId":"ui"}
//→ {"type":"subscribed","clientId":"ui"}
//The host writes: {"type":"invoke","id":1,"clientId":"ui","data":{"cmd":"echo","text":"hi"}}
//→ {"type":"response","id":1,"response":{"success":true,"error":null,"payload":"hi"}}

Released under the MIT License.