implemented Quit function
This commit is contained in:
		
							
								
								
									
										40
									
								
								commands.go
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								commands.go
									
									
									
									
									
								
							@ -1,5 +1,11 @@
 | 
			
		||||
package mpvipc
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"net"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type LoadFileFlag string
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@ -17,3 +23,37 @@ 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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,6 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"net"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"git.milar.in/milarin/channel"
 | 
			
		||||
)
 | 
			
		||||
@ -28,34 +27,6 @@ type Event[T any] struct {
 | 
			
		||||
	Reason string `json:"reason"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SendCommand[T any](socket string, cmd *Command) (*Response[T], error) {
 | 
			
		||||
	conn, err := net.Dial("unix", socket)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user