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

Added parallel execution of props #21

Closed
wants to merge 9 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package gonertia
import (
"bytes"
"context"
"errors"
"fmt"
"html/template"
"net/http"
"strings"
"sync"
)

// TemplateData are data that will be available in the root template.
Expand Down Expand Up @@ -153,6 +155,7 @@ func (i *Inertia) buildPage(r *http.Request, component string, props Props) (*pa
}

func (i *Inertia) prepareProps(r *http.Request, component string, props Props) (Props, error) {
mut := sync.Mutex{}
result := make(Props)

{
Expand Down Expand Up @@ -204,17 +207,35 @@ func (i *Inertia) prepareProps(r *http.Request, component string, props Props) (
delete(result, key)
}
}

// Resolve props values.
wg := new(sync.WaitGroup)
errch := make(chan error, len(result))
for key, val := range result {
var err error
val, err = resolvePropVal(val)
if err != nil {
return nil, fmt.Errorf("resolve prop value: %w", err)
}
result[key] = val
wg.Add(1)
go func() {
var err error
val, err = resolvePropVal(val)
if err != nil {
errch <- fmt.Errorf("resolve prop value: %w", err)
wg.Done()
return
}
mut.Lock()
result[key] = val
mut.Unlock()
wg.Done()
}()
}
wg.Wait()
close(errch)
allerr := make([]error, 0)
for e := range errch {
allerr = append(allerr, e)
}
err := errors.Join(allerr...)
if err != nil {
return nil, err
}

return result, nil
}

Expand Down
Loading