Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
162 lines
3.8 KiB
Go
162 lines
3.8 KiB
Go
package mobile
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/egoist/mygo/internal/mobile/wire"
|
|
)
|
|
|
|
// A host is the native side of the app: the Swift host on iOS, the Kotlin
|
|
// host on Android. It runs calls on the UI thread, where the system's
|
|
// views live, and sends events. Go runs on a thread of its own, its main
|
|
// thread (the one the core calls the main thread), so that it can wait
|
|
// (Step) while the UI thread goes on.
|
|
type host interface {
|
|
// call runs an encoded call and returns its encoded reply, nil for a
|
|
// call not waited for. UI thread only.
|
|
call(msg []byte) []byte
|
|
// post asks the UI thread to call transport.drain soon. Any thread.
|
|
post()
|
|
}
|
|
|
|
// transport carries calls from Go's threads to the UI thread, which runs
|
|
// them in order: when it next drains them (post), or at once while it
|
|
// waits for Go to answer an event, so that neither side waits for the
|
|
// other: an event handler the UI thread waits for may make calls.
|
|
type transport struct {
|
|
h host
|
|
|
|
mu sync.Mutex
|
|
pending []request
|
|
posted bool
|
|
// waiting counts the events the UI thread waits in, nested; kick
|
|
// wakes it to run calls meanwhile.
|
|
waiting int
|
|
kick chan struct{}
|
|
}
|
|
|
|
type request struct {
|
|
msg []byte
|
|
// fn, when set, runs on the UI thread in place of a call of the host:
|
|
// a native module's main-thread method, or its view.
|
|
fn func()
|
|
reply chan []byte // nil for a call not waited for
|
|
}
|
|
|
|
func newTransport(h host) *transport {
|
|
return &transport{h: h, kick: make(chan struct{}, 1)}
|
|
}
|
|
|
|
// message is a call of package wire.
|
|
type message interface {
|
|
Encode(w *wire.Writer)
|
|
Wait() bool
|
|
}
|
|
|
|
// call sends m to the UI thread, and when m is waited for, decodes its
|
|
// reply into reply. Any thread but the UI thread.
|
|
func (t *transport) call(m message, reply interface{ Decode(*wire.Reader) }) {
|
|
var w wire.Writer
|
|
m.Encode(&w)
|
|
req := request{msg: w.Buf}
|
|
if m.Wait() {
|
|
req.reply = make(chan []byte, 1)
|
|
}
|
|
b := t.send(req)
|
|
if b != nil && reply != nil {
|
|
reply.Decode(&wire.Reader{Buf: *b})
|
|
}
|
|
}
|
|
|
|
// run runs fn on the UI thread, in order with the calls, and when wait,
|
|
// waits for it. Any thread but the UI thread when wait.
|
|
func (t *transport) run(fn func(), wait bool) {
|
|
req := request{fn: fn}
|
|
if wait {
|
|
req.reply = make(chan []byte, 1)
|
|
}
|
|
t.send(req)
|
|
}
|
|
|
|
// send queues req for the UI thread and waits for its reply, if it has
|
|
// one to wait for.
|
|
func (t *transport) send(req request) *[]byte {
|
|
t.mu.Lock()
|
|
t.pending = append(t.pending, req)
|
|
post := false
|
|
if t.waiting > 0 {
|
|
select {
|
|
case t.kick <- struct{}{}:
|
|
default:
|
|
}
|
|
} else if !t.posted {
|
|
t.posted, post = true, true
|
|
}
|
|
t.mu.Unlock()
|
|
if post {
|
|
t.h.post()
|
|
}
|
|
if req.reply == nil {
|
|
return nil
|
|
}
|
|
b := <-req.reply
|
|
return &b
|
|
}
|
|
|
|
// drain runs the calls sent so far. UI thread only.
|
|
func (t *transport) drain() {
|
|
for {
|
|
// One at a time: a call may wait for Go (wait), which runs the calls
|
|
// after it meanwhile, one of which Go may be waiting for.
|
|
t.mu.Lock()
|
|
if len(t.pending) == 0 {
|
|
t.posted = false
|
|
t.mu.Unlock()
|
|
return
|
|
}
|
|
r := t.pending[0]
|
|
t.pending = t.pending[1:]
|
|
t.mu.Unlock()
|
|
var b []byte
|
|
if r.fn != nil {
|
|
r.fn()
|
|
} else {
|
|
b = t.h.call(r.msg)
|
|
}
|
|
if r.reply != nil {
|
|
r.reply <- b
|
|
}
|
|
}
|
|
}
|
|
|
|
// wait waits for done, an answer of Go's main thread to an event of the
|
|
// UI thread, running the calls Go makes meanwhile. UI thread only.
|
|
func (t *transport) wait(done chan []byte) []byte {
|
|
t.mu.Lock()
|
|
t.waiting++
|
|
t.mu.Unlock()
|
|
defer func() {
|
|
t.mu.Lock()
|
|
t.waiting--
|
|
// A call that came as it stopped waiting only kicked it.
|
|
post := t.waiting == 0 && len(t.pending) > 0 && !t.posted
|
|
if post {
|
|
t.posted = true
|
|
}
|
|
t.mu.Unlock()
|
|
if post {
|
|
t.h.post()
|
|
}
|
|
}()
|
|
for {
|
|
t.drain()
|
|
select {
|
|
case b := <-done:
|
|
// Calls sent before the answer run before the UI thread goes on.
|
|
t.drain()
|
|
return b
|
|
case <-t.kick:
|
|
}
|
|
}
|
|
}
|