single
import "github.com/tangoui/tango/sys/single"本页由
site/tools/apigen从源码注释生成(按 darwin 构建标签选文件),请勿手改。
Package single keeps one instance of an app running per user.
Call Claim from main before App.Run. The first instance becomes the primary and listens for later launches; a later instance forwards its command-line arguments and working directory to the primary and should then exit:
app := tango.New(tango.Options{ID: "com.example.notes", URLSchemes: []string{"notes"}})
primary, err := single.Claim(app, single.Options{
OnSecondInstance: func(args []string, cwd string) { log.Println("relaunched with", args) },
})
if !primary {
return // forwarded to the running instance
}
if err != nil {
log.Println("single instance disabled:", err)
}
In the primary, a forwarded launch is handled on the main thread: deep links and files among its arguments are delivered like launch arguments (App.HandleArgs: EventOpenURL, EventOpenFiles), Options.OnSecondInstance and the handlers registered with OnSecondInstance run, and the first open surface is shown and focused. OnSecondInstance lets other packages (jsapi forwards launches to pages) listen without being handed the Options:
single.OnSecondInstance(app, func(args []string, cwd string) { ... })
Instances are told apart by app key and user: macOS and Linux use a lock file held with flock(2) plus a Unix-domain socket in a directory only the user can access ($XDG_RUNTIME_DIR on Linux, else a 0700 "tango-<uid>" directory in $TMPDIR or /tmp); Windows uses a named pipe, named after the user's SID, created with FILE_FLAG_FIRST_PIPE_INSTANCE and a DACL that admits only that user. Both decide the primary atomically, so two instances started together cannot both win. A crashed primary leaves nothing that blocks the next launch: the kernel drops the lock with the process, and a stale socket file is replaced.
Arguments are only forwarded to a primary run by the same user: a later instance checks the owner of the other end (peer credentials, the pipe server's process token) before sending anything. If the endpoint is held by someone else, or the directory is not private, Claim reports primary=true with an error: the app runs on its own and should log it.
On iOS and Android the system already runs one instance; Claim does nothing and reports primary.
变量
var ErrUnsupported = platform.ErrUnsupportedErrUnsupported is returned (with primary true) where the platform has no single-instance mechanism.
函数
func Claim
func Claim(a *tango.App, o Options) (primary bool, err error)Claim makes this process the app's primary instance, or forwards to the running primary. It reports primary=false only when the arguments were forwarded (exit then) or the primary exists but could not be reached (err says why; exiting is still the safe choice). With primary=true and a non-nil err, single-instance mode could not be set up and the app runs as if it were not used.
Call it once, before App.Run. The primary stops listening when the app terminates.
func OnSecondInstance
func OnSecondInstance(a *tango.App, fn func(args []string, cwd string))OnSecondInstance registers fn to run on the main thread of the primary instance each time a later launch forwards to it, after Options.OnSecondInstance. It may be called before or after Claim (from any goroutine) and has no effect unless Claim made this process the primary.
类型
Options
type Options struct {
// Key identifies the app: instances with the same Key, run by the same
// user, are one app. Empty means the app's ID (Options.ID, else Name).
Key string
// OnSecondInstance runs on the main thread of the primary when a later
// launch forwards to it, with that launch's arguments (without the
// program name) and working directory.
OnSecondInstance func(args []string, cwd string)
// NoFocus keeps the primary's main window where it is. By default the
// first open surface is shown and focused.
NoFocus bool
// Args are the arguments a later instance forwards; nil means
// os.Args[1:].
Args []string
// Timeout bounds how long a later instance tries to reach the primary;
// 0 means 3 seconds.
Timeout time.Duration
}Options configures Claim.