Documentation
¶
Overview ¶
Package plugin calls native code: a method of a plugin registered in the iOS or Android host, written in Swift or Kotlin. Arguments and results travel as JSON, so a plugin needs no generated code on either side.
type token struct{ Value string `json:"value"` }
t, err := plugin.Call[token](ctx, "secureStorage", "get", map[string]string{"key": "token"})
Call blocks until the native side answers, so call it where blocking is fine: in ui.UseQuery, ui.UseAction, an effect or your own goroutine, not in Render. The host runs every plugin method on its main thread.
Plugins are registered in the host:
Plugins.register("battery") { call in call.resolve(UIDevice.current.batteryLevel) } // Swift
Plugins.register("battery") { call -> call.resolve(level) } // Kotlin
In a browser the plugin's JavaScript answers instead (see RegisterWeb). The call finds the browser through its ctx, so on the web pass the ctx that ui.UseQuery, ui.UseAction or an effect gives you.
Index ¶
- Constants
- Variables
- func Call[Resp any](ctx context.Context, name, method string, args any) (Resp, error)
- func CallJSON(ctx context.Context, name, method string, args any) (json.RawMessage, error)
- func Deliver(r *render.PluginResult)
- func InBrowser(ctx context.Context) bool
- func IsCode(err error, code string) bool
- func RegisterWeb(name, js string)
- func RunBackground(name string)
- func WebSource(name string) (string, bool)
- func WithBrowser(ctx context.Context, call func(req []byte) ([]byte, error)) context.Context
- type Error
Constants ¶
const ( Cancelled = "cancelled" Denied = "denied" NotFound = "not_found" Failed = "failed" )
Codes of Error.
Variables ¶
var OnBackground func(name string)
OnBackground is called with the name of each background task the host starts; plugins/backgroundtasks sets it.
Functions ¶
func Call ¶
Call runs method on the named plugin with args (any JSON-encodable value, or nil) and decodes the answer into Resp. Use struct{} or any for a method with no useful answer.
func Deliver ¶
func Deliver(r *render.PluginResult)
Deliver hands a plugin's outcome to the waiting Call. The UI runtime calls it for every PluginResult the host sends.
func InBrowser ¶ added in v0.0.14
InBrowser reports whether plugin calls with ctx run in a browser, for plugins that work differently there.
func RegisterWeb ¶ added in v0.0.14
func RegisterWeb(name, js string)
RegisterWeb gives a plugin its browser side: JavaScript that the page loads the first time the plugin is called, and that registers the plugin's methods with NN.plugin:
NN.plugin("clipboard", {
set: async ({ text }) => { await navigator.clipboard.writeText(text); },
get: async () => ({ text: await navigator.clipboard.readText() }),
});
A method returns its answer (any JSON value) or throws; NN.fail(code, message) throws with one of the Error codes. Plugins call RegisterWeb from init, with the source embedded.
func RunBackground ¶ added in v0.0.13
func RunBackground(name string)
RunBackground starts a background task. The UI runtime calls it.
func WithBrowser ¶ added in v0.0.14
WithBrowser returns a context whose plugin calls go to a browser through call, which takes an encoded render.PluginCall and answers with an encoded render.PluginAck; the result comes later through Deliver. The web host gives each tab's contexts one.