Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Reactor API #73

Closed
eNV25 opened this issue Jul 9, 2022 · 1 comment
Closed

Add Reactor API #73

eNV25 opened this issue Jul 9, 2022 · 1 comment

Comments

@eNV25
Copy link
Contributor

eNV25 commented Jul 9, 2022

From #22.

The last comment mentions using an interface in the old pull request. I liked the idea and I want to implement it.

Here's how I'm imagining it would look like:

// Reactor represents an MPD connection that reacts to events.
type Reactor struct{ ... }

// NewReactor connects to MPD and reacts to events. Set handlers and other options
// by passing ReactorOptions.
func NewReactor(network, addr, password string, options ...ReactorOption) *Reactor

// Subsystems restricts subsystems watched for.
func (r *Reactor) Subsystems(subsystems ...string)

// Interrupt stops watching for event temporarily and runs the interrupt handler with args.
func (r *Reactor) Interrupt(args ...interface{})

// ReactorOption
type ReactorOption func(r *Reactor)

// ReactorSubsystems restricts the subsystems watched for.
func ReactorSubsystems(subsystems ...string) ReactorOption

// ReactorEventHandler is run for each change in MPD. The first argument is the subsystem
// that changed. See the list of subsystems:
// https://mpd.readthedocs.io/en/stable/protocol.html#command-idle
func ReactorEventHandler(f func(c *Client, subsystem string)) ReactorOption

func ReactorErrorHandler(f func(c *Client, err error)) ReactorOption

func ReactorInterruptHandler(f func(c *Client, args ...interface{})) ReactorOption

Instead of an interface, this uses the functional options pattern. This makes the API easier to use and makes any or all functionality optional.

This also adds an interrupt system, which allows using Client for external events. Interrupt will simply call noidle and send args on a channel.

We could also add ReactorOptions that send on a channel, so Watcher could be implemented in terms of Reactor.

@eNV25 eNV25 mentioned this issue Jul 10, 2022
@eNV25
Copy link
Contributor Author

eNV25 commented Jul 10, 2022

#74

@eNV25 eNV25 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant