Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Mar 18, 2023
1 parent 9666485 commit edf1668
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
7 changes: 3 additions & 4 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,16 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketc
opts.FontFamily = go2.Pointer(d2fonts.HandDrawn)
}

kill := background.DoEveryX(func() {
cancel := background.Repeat(func() {
ms.Log.Info.Printf("compiling & running layout algorithms...")
}, time.Second*5)

defer kill()
defer cancel()

diagram, g, err := d2lib.Compile(ctx, string(input), opts)
if err != nil {
return nil, false, err
}
kill()
cancel()

pluginInfo, err := plugin.Info(ctx)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions lib/background/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ package background

import "time"

func DoEveryX(do func(), x time.Duration) (kill func()) {
stopped := false
t := time.NewTicker(x)
stop := make(chan struct{})
func Repeat(do func(), interval time.Duration) (cancel func()) {
t := time.NewTicker(interval)
done := make(chan struct{})

go func() {
defer t.Stop()
for {
select {
case <-t.C:
do()
case <-stop:
case <-done:
return
}
}
}()

stopped := false
return func() {
if !stopped {
stopped = true
t.Stop()
close(stop)
close(done)
}
}
}
5 changes: 2 additions & 3 deletions lib/png/png.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ var genPNGScript string
const pngPrefix = "data:image/png;base64,"

func ConvertSVG(ms *xmain.State, page playwright.Page, svg []byte) ([]byte, error) {
kill := background.DoEveryX(func() {
cancel := background.Repeat(func() {
ms.Log.Info.Printf("converting to PNG...")
}, time.Second*5)

defer kill()
defer cancel()

encodedSVG := base64.StdEncoding.EncodeToString(svg)
pngInterface, err := page.Evaluate(genPNGScript, "data:image/svg+xml;charset=utf-8;base64,"+encodedSVG)
Expand Down

0 comments on commit edf1668

Please sign in to comment.