Skip to content

Commit

Permalink
Run initial command in a go routine
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Jun 30, 2023
1 parent bd295b0 commit a489502
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions commands/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,20 @@ func watchLoop(cmd *cobra.Command, args []string, onChange func(cmd *cobra.Comma

bounce := debounce.New(1500 * time.Millisecond)

// An initial build is usually done on first load with
// live reloaders
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
canceller.Set(ctx, cancel)

if err := onChange(cmd, args, ctx); err != nil {
fmt.Println("Error rebuilding: ", err)
os.Exit(1)
}
go func() {
// An initial build is usually done on first load with
// live reloaders
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
canceller.Set(ctx, cancel)

if err := onChange(cmd, args, ctx); err != nil {
fmt.Println("Error rebuilding: ", err)
os.Exit(1)
}
}()

log.Printf("[Watch] Started")
for {
select {
case event, ok := <-watcher.Events:
Expand Down Expand Up @@ -161,11 +164,12 @@ func watchLoop(cmd *cobra.Command, args []string, onChange func(cmd *cobra.Comma
}

bounce(func() {
log.Printf("[Watch] Cancel previous build")
log.Printf("[Watch] Cancelling")

canceller.Cancel()
log.Printf("[Watch] Cancelled previous build")
ctx, cancel = context.WithCancel(context.Background())

log.Printf("[Watch] Cancelled")
ctx, cancel := context.WithCancel(context.Background())
canceller.Set(ctx, cancel)

// Assign --filter to "" for all functions if we can't determine the
Expand Down

0 comments on commit a489502

Please sign in to comment.