From c8739d8147bbb31a0e705f553483d1b70e8ac77f Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 16 Nov 2022 23:49:45 -0800 Subject: [PATCH] 2022-11-16 11:49:45PM --- cmd/d2/main.go | 10 +++++++++- cmd/d2/watch.go | 29 ++++++++++++++--------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/cmd/d2/main.go b/cmd/d2/main.go index cfca96ddf..61107d6ea 100644 --- a/cmd/d2/main.go +++ b/cmd/d2/main.go @@ -132,7 +132,15 @@ func run(ctx context.Context, ms *xmain.State) (err error) { return xmain.UsageErrorf("-w[atch] cannot be combined with reading input from stdin") } ms.Env.Setenv("LOG_TIMESTAMPS", "1") - w, err := newWatcher(ctx, ms, plugin, *themeFlag, *hostFlag, *portFlag, inputPath, outputPath) + + w, err := newWatcher(ctx, ms, watcherOpts{ + layoutPlugin: plugin, + themeID: *themeFlag, + host: *hostFlag, + port: *portFlag, + inputPath: inputPath, + outputPath: outputPath, + }) if err != nil { return err } diff --git a/cmd/d2/watch.go b/cmd/d2/watch.go index a5d22c42c..f4fef9a6d 100644 --- a/cmd/d2/watch.go +++ b/cmd/d2/watch.go @@ -34,19 +34,23 @@ var devMode = false //go:embed static var staticFS embed.FS -type watcher struct { - ctx context.Context - cancel context.CancelFunc - wg sync.WaitGroup - devMode bool - - ms *xmain.State +type watcherOpts struct { layoutPlugin d2plugin.Plugin themeID int64 host string port string inputPath string outputPath string +} + +type watcher struct { + ctx context.Context + cancel context.CancelFunc + wg sync.WaitGroup + devMode bool + + ms *xmain.State + watcherOpts compileCh chan struct{} @@ -71,7 +75,7 @@ type compileResult struct { SVG string `json:"svg"` } -func newWatcher(ctx context.Context, ms *xmain.State, layoutPlugin d2plugin.Plugin, themeID int64, host, port, inputPath, outputPath string) (*watcher, error) { +func newWatcher(ctx context.Context, ms *xmain.State, opts watcherOpts) (*watcher, error) { ctx, cancel := context.WithCancel(ctx) w := &watcher{ @@ -79,13 +83,8 @@ func newWatcher(ctx context.Context, ms *xmain.State, layoutPlugin d2plugin.Plug cancel: cancel, devMode: devMode, - ms: ms, - layoutPlugin: layoutPlugin, - themeID: themeID, - host: host, - port: port, - inputPath: inputPath, - outputPath: outputPath, + ms: ms, + watcherOpts: opts, compileCh: make(chan struct{}, 1), wsclients: make(map[*wsclient]struct{}),