Compare commits

..

No commits in common. "main" and "v0.0.1" have entirely different histories.
main ... v0.0.1

6 changed files with 8 additions and 89 deletions

View File

@ -1,11 +1,5 @@
package mpvipc
import (
"context"
"net"
"time"
)
type LoadFileFlag string
const (
@ -23,37 +17,3 @@ func LoadFile(socket string, file string, flags LoadFileFlag) error {
_, err := SendCommand[any](socket, cmd)
return err
}
func Quit(socket string) error {
cmd := &Command{Command: []interface{}{"quit"}}
_, err := SendCommand[any](socket, cmd)
return err
}
func IsReady(ctx context.Context, socket string) <-chan bool {
out := make(chan bool, 1)
go func() {
defer close(out)
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
conn, err := net.Dial("unix", socket)
if err == nil {
defer conn.Close()
out <- true
return
}
case <-ctx.Done():
out <- false
return
}
}
}()
return out
}

4
go.mod
View File

@ -1,5 +1,5 @@
module git.tordarus.net/tordarus/mpvipc
module git.milar.in/milarin/mpvipc
go 1.21.5
require git.tordarus.net/tordarus/channel v0.1.1
require git.milar.in/milarin/channel v0.1.1

View File

@ -17,10 +17,6 @@ func IsPaused(socket string) (bool, error) {
return GetProperty[bool](socket, "pause")
}
func GetVolume(socket string) (float64, error) {
return GetProperty[float64](socket, "volume")
}
func GetFilename(socket string) (string, error) {
return GetProperty[string](socket, "filename")
}
@ -58,15 +54,6 @@ func GetDuration(socket string) (time.Duration, error) {
return time.Duration(durationInSeconds * float64(time.Second)), nil
}
func GetDemuxerCacheTime(socket string) (time.Duration, error) {
cacheInSeconds, err := GetProperty[float64](socket, "demuxer-cache-time")
if err != nil {
return 0, err
}
return time.Duration(cacheInSeconds * float64(time.Second)), nil
}
func GetPercentPos(socket string) (float64, error) {
return GetProperty[float64](socket, "percent-pos")
}

View File

@ -7,18 +7,6 @@ import (
"git.milar.in/milarin/channel"
)
func ObserveEndOfFile(ctx context.Context, socket string) (<-chan string, error) {
ch, err := ObserveEvent[any](ctx, socket, "end-file")
if err != nil {
return nil, err
}
getReason := func(e Event[any]) string { return e.Reason }
notEmpty := func(str string) bool { return str != "" }
reasons := channel.Filter(channel.MapSuccessive(ch, getReason), notEmpty)
return reasons, nil
}
func ObserveDisplayNames(ctx context.Context, socket string) (<-chan []string, error) {
return ObserveProperty[[]string](ctx, socket, "display-names")
}

View File

@ -17,7 +17,3 @@ func SetTimePos(socket string, timePos time.Duration) error {
func SetPercentPos(socket string, percentPos float64) error {
return SetProperty[float64](socket, "percent-pos", percentPos)
}
func SetVolume(socket string, volume float64) error {
return SetProperty[float64](socket, "volume", volume)
}

View File

@ -5,8 +5,6 @@ import (
"encoding/json"
"errors"
"net"
"git.milar.in/milarin/channel"
)
type Command struct {
@ -20,11 +18,10 @@ type Response[T any] struct {
}
type Event[T any] struct {
Data T `json:"data"`
ID int `json:"id"`
Name string `json:"name"`
Error string `json:"error"`
Reason string `json:"reason"`
Data T `json:"data"`
ID int `json:"id"`
Name string `json:"name"`
Error string `json:"error"`
}
func SendCommand[T any](socket string, cmd *Command) (*Response[T], error) {
@ -75,16 +72,7 @@ func SetProperty[T any](socket string, propertyName string, propertyValue T) err
}
func ObserveProperty[T any](ctx context.Context, socket string, propertyName string) (<-chan T, error) {
ch, err := ObserveEvent[T](ctx, socket, propertyName)
if err != nil {
return nil, err
}
return channel.MapSuccessive(ch, func(e Event[T]) T { return e.Data }), nil
}
func ObserveEvent[T any](ctx context.Context, socket string, propertyName string) (<-chan Event[T], error) {
out := make(chan Event[T], 10)
out := make(chan T, 10)
conn, err := net.Dial("unix", socket)
if err != nil {
@ -125,7 +113,7 @@ func ObserveEvent[T any](ctx context.Context, socket string, propertyName string
if err := dec.Decode(&event); err != nil {
break
}
out <- event
out <- event.Data
}
}()