-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
40 lines (33 loc) · 1.15 KB
/
rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"context"
"fmt"
"strconv"
"sync/atomic"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
var rpcResponseId int32 = 0
func callRPC(ctx context.Context, name string, args ...interface{}) interface{} {
fmt.Printf("GO -> JS: %s(%v)\n", name, args)
responseId := strconv.Itoa(int(atomic.AddInt32(&rpcResponseId, 1)))
responseChan := make(chan interface{})
responseHandler := func(data ...interface{}) {
fmt.Printf("GO (%v)\n", data)
responseChan <- data[0]
}
fmt.Printf(responseId)
runtime.EventsOnce(ctx, "response-backend-"+responseId, responseHandler)
data := append([]interface{}{responseId}, args...)
runtime.EventsEmit(ctx, name+"-request", data)
response := <-responseChan
fmt.Printf("GO -> JS: %s(%v) -> %v\n", name, args, response)
return response
}
func registerHandler(ctx context.Context, name string, handler func(...interface{}) interface{}) {
runtime.EventsOn(ctx, name+"-request", func(data ...interface{}) {
responseId := data[0].(string)
response := handler(data[1:]...)
debugf("JS -> GO: %s(%v) -> %v\n", name, data[1:], response)
runtime.EventsEmit(ctx, "response-frontend-"+responseId, response)
})
}