Some fixes, thx vim-go

This commit is contained in:
Marcel Otte 2021-11-15 22:04:59 +01:00
parent 86a99224ac
commit 1e4f78be3b
1 changed files with 13 additions and 10 deletions

View File

@ -8,14 +8,16 @@ import (
var ls net.Listener var ls net.Listener
var done <-chan struct{} var done <-chan struct{}
var callbacks := make([]func(map[string]string), 3) var callbacks []func(map[string]string)
// Init initializes the unix socket. // Init initializes the unix socket.
func Init(socketPath string) { func Init(socketPath string) {
ls, err = net.Listen(socketPath) var err error
ls, err = net.Listen("unix", socketPath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
callbacks = make([]func(map[string]string), 3)
} }
// Listen starts the event loop. // Listen starts the event loop.
@ -28,13 +30,13 @@ func Listen() {
} }
// RegisterCallback adds a function to the list of callback functions for processing of events. // RegisterCallback adds a function to the list of callback functions for processing of events.
func RegisterCallback(cb func(map[string]string)){ func RegisterCallback(cb func(map[string]string)) {
append(callbacks, cb) callbacks = append(callbacks, cb)
} }
// process processes each and every unix socket event, Unmarshals the json data and calls the list of callbacks. // process processes each and every unix socket event, Unmarshals the json data and calls the list of callbacks.
func process() { func process() {
client, err = ls.Accept() client, err := ls.Accept()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -45,17 +47,18 @@ func process() {
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
panic(err) panic(err)
} }
append(data, buf[0:nr]) app := buf[0:nr]
data = append(data, app)
if err == io.EOF { if err == io.EOF {
break break
} }
} }
env := map[string]string{} env := map[string]string{}
err := json.Unmarshal(data, &env) errjson := json.Unmarshal(data, &env)
if err != nil { if errjson != nil {
panic(err) panic(errjson)
} }
for _, v = range(callbacks) { for _, v := range callbacks {
v(env) v(env)
} }
} }