Skip to content

Commit

Permalink
Callbacks => Runners etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Oct 19, 2024
1 parent 7545e08 commit a07f8f9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 103 deletions.
17 changes: 0 additions & 17 deletions tpl/js/batch-esm-callback.gotmpl

This file was deleted.

16 changes: 16 additions & 0 deletions tpl/js/batch-esm-runner.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{ range $i, $e := .Scripts -}}
{{ printf "import { %s as Script%d } from %q;" .Export $i .Import }}
{{ end -}}
{{ range $i, $e := .Runners }}
{{ printf "import { %s as Run%d } from %q;" .Export $i .Import }}
{{ end }}
{{/* */}}
let scripts = [];
{{ range $i, $e := .Scripts -}}
scripts.push({{ .RunnerJSON $i }});
{{ end -}}
{{/* */}}
{{ range $i, $e := .Runners }}
{{ $id := printf "Run%d" $i }}
{{ $id }}(scripts);
{{ end }}
169 changes: 100 additions & 69 deletions tpl/js/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Batcher interface {
}

type BatcherGroup interface {
Callback(id string) OptionsSetter
Runner(id string) OptionsSetter
Script(id string) OptionsSetter
Instance(sid, iid string) OptionsSetter
}
Expand All @@ -75,9 +75,9 @@ type ScriptOptions struct {
// Note that we will always fall back to the resource's own import context.
ImportContext resource.ResourceGetter

// The export name to use for this script's group's callback
// The export name to use for this script's group's runners (if any).
// If not set, the default export will be used.
CallbackExport string
Export string

// Params marshaled to JSON.
Params json.RawMessage
Expand Down Expand Up @@ -150,7 +150,7 @@ func (b *batcher) Group(id string) BatcherGroup {
id: id, client: b.client,
scriptsOptions: make(map[string]*options),
instancesOptions: make(map[instanceID]*options),
callbacksOptions: make(map[string]*options),
runnersOptions: make(map[string]*options),
}
b.scriptGroups[id] = group
}
Expand All @@ -161,29 +161,49 @@ func (b *batcher) Group(id string) BatcherGroup {
var _ Batcher = (*batcher)(nil)

type batchTemplateContext struct {
keyPath string
ID string
Callbacks []string
Modules []batchTemplateExecutionsContext
keyPath string
ID string
Runners []scriptRunnerTemplateContext
Scripts []scriptBatchTemplateContext
}

type batchTemplateExecutionsContext struct {
ID string `json:"id"`
ImportPath string `json:"-"`
CallbackExport string `json:"-"`
Instances []batchTemplateExecution `json:"instances"`
type scriptBatchTemplateContext struct {
*script
Import string
Instances []scriptInstanceBatchTemplateContext
}

func (c scriptBatchTemplateContext) MarshalJSON() (b []byte, err error) {
return json.Marshal(&struct {
ID string `json:"id"`
Instances []scriptInstanceBatchTemplateContext `json:"instances"`
}{
ID: c.ID,
Instances: c.Instances,
})
}

type scriptRunnerTemplateContext struct {
*script
Import string
}

r resource.Resource
func (c scriptRunnerTemplateContext) MarshalJSON() (b []byte, err error) {
return json.Marshal(&struct {
ID string `json:"id"`
}{
ID: c.ID,
})
}

func (b batchTemplateExecutionsContext) CallbackJSON(i int) string {
mod := fmt.Sprintf("Mod%d", i)
func (b scriptBatchTemplateContext) RunnerJSON(i int) string {
script := fmt.Sprintf("Script%d", i)

v := struct {
Mod string `json:"mod"`
batchTemplateExecutionsContext
Script string `json:"script"`
scriptBatchTemplateContext
}{
mod,
script,
b,
}

Expand All @@ -193,14 +213,27 @@ func (b batchTemplateExecutionsContext) CallbackJSON(i int) string {
}
s := string(bb)

s = strings.ReplaceAll(s, fmt.Sprintf("%q", mod), mod)
s = strings.ReplaceAll(s, fmt.Sprintf("%q", script), script)

return s
}

type batchTemplateExecution struct {
ID string `json:"id"`
Params json.RawMessage `json:"params"`
type scriptInstanceBatchTemplateContext struct {
*instance
}

func (c scriptInstanceBatchTemplateContext) ID() string {
return c.instanceID.instanceID
}

func (c scriptInstanceBatchTemplateContext) MarshalJSON() (b []byte, err error) {
return json.Marshal(&struct {
ID string `json:"id"`
Params json.RawMessage `json:"params"`
}{
ID: c.instanceID.instanceID,
Params: c.Params,
})
}

type scriptGroup struct {
Expand All @@ -212,12 +245,12 @@ type scriptGroup struct {

scriptsOptions map[string]*options
instancesOptions map[instanceID]*options
callbacksOptions map[string]*options
runnersOptions map[string]*options

// Compiled.
scripts scriptMap
instances instanceMap
callbacks scriptMap
runners scriptMap
}

func (g *scriptGroup) Reset() {
Expand All @@ -227,7 +260,7 @@ func (g *scriptGroup) Reset() {
for _, v := range g.instancesOptions {
v.Reset()
}
for _, v := range g.callbacksOptions {
for _, v := range g.runnersOptions {
v.Reset()
}
}
Expand Down Expand Up @@ -290,7 +323,7 @@ func (s *scriptGroup) compile() error {
// TODO1 lock?
s.scripts = make(map[string]*ScriptOptions)
s.instances = make(map[instanceID]*ParamsOptions)
s.callbacks = make(map[string]*ScriptOptions)
s.runners = make(map[string]*ScriptOptions)

for k, v := range s.scriptsOptions {
compiled, err := compileScriptOptions(v)
Expand All @@ -308,12 +341,12 @@ func (s *scriptGroup) compile() error {
s.instances[k] = compiled
}

for k, v := range s.callbacksOptions {
for k, v := range s.runnersOptions {
compiled, err := compileScriptOptions(v)
if err != nil {
return err
}
s.callbacks[k] = compiled
s.runners[k] = compiled
}

return nil
Expand Down Expand Up @@ -345,14 +378,14 @@ func (s *scriptGroup) Instance(sid, iid string) OptionsSetter {
return s.instancesOptions[id].Get()
}

func (s *scriptGroup) Callback(id string) OptionsSetter {
func (s *scriptGroup) Runner(id string) OptionsSetter {
s.mu.Lock()
defer s.mu.Unlock()
if v, found := s.callbacksOptions[id]; found {
if v, found := s.runnersOptions[id]; found {
return v.Get()
}
s.callbacksOptions[id] = newOptions()
return s.callbacksOptions[id].Get()
s.runnersOptions[id] = newOptions()
return s.runnersOptions[id].Get()
}

type scriptGroups map[string]*scriptGroup
Expand Down Expand Up @@ -510,10 +543,10 @@ func compileParamsOptions(o *options) (*ParamsOptions, error) {

func compileScriptOptions(o *options) (*ScriptOptions, error) {
v := struct {
Resource resource.Resource
ImportContext any
CallbackExport string
Params map[string]any
Resource resource.Resource
ImportContext any
Export string
Params map[string]any
}{}

m := o.commit().opts
Expand All @@ -531,15 +564,15 @@ func compileScriptOptions(o *options) (*ScriptOptions, error) {
}
}

if v.CallbackExport == "" {
v.CallbackExport = "default"
if v.Export == "" {
v.Export = "default"
}

compiled := &ScriptOptions{
Resource: v.Resource,
CallbackExport: v.CallbackExport,
ImportContext: resource.NewResourceGetter(v.ImportContext),
Params: paramsJSON,
Resource: v.Resource,
Export: v.Export,
ImportContext: resource.NewResourceGetter(v.ImportContext),
Params: paramsJSON,
}

return compiled, nil
Expand Down Expand Up @@ -660,7 +693,7 @@ func (p *Package) forEeachStaleInfo(f func(si resource.StaleInfo) bool) {
}
}

for _, vv := range v.callbacksOptions {
for _, vv := range v.runnersOptions {
if check(vv) {
return true
}
Expand Down Expand Up @@ -771,18 +804,18 @@ func (b *batcher) doBuild() (*Package, error) {
for k, v := range b.scriptGroups {
keyPath := keyPath + "_" + k

var callbacks []string
for _, vv := range v.callbacks.Sorted() {
callbackKeyPath := keyPath + "_" + vv.ID
callbackImpPath := paths.AddLeadingSlash(callbackKeyPath + "_callback" + vv.Resource.MediaType().FirstSuffix.FullSuffix)
callbacks = append(callbacks, callbackImpPath)
addResource(k, callbackImpPath, vv.Resource, false)
var runners []scriptRunnerTemplateContext
for _, vv := range v.runners.Sorted() {
runnerKeyPath := keyPath + "_" + vv.ID
runnerImpPath := paths.AddLeadingSlash(runnerKeyPath + "_runner" + vv.Resource.MediaType().FirstSuffix.FullSuffix)
runners = append(runners, scriptRunnerTemplateContext{script: vv, Import: runnerImpPath})
addResource(k, runnerImpPath, vv.Resource, false)
}

t := &batchTemplateContext{
keyPath: keyPath,
ID: v.id,
Callbacks: callbacks,
keyPath: keyPath,
ID: v.id,
Runners: runners,
}

instances := v.instances.Sorted()
Expand All @@ -803,24 +836,22 @@ func (b *batcher) doBuild() (*Package, error) {
scriptOptions: opts,
})

bt := batchTemplateExecutionsContext{
ID: vv.ID,
r: vv.Resource,
CallbackExport: vv.CallbackExport,
ImportPath: impPath,
bt := scriptBatchTemplateContext{
script: vv,
Import: impPath,
}
state.importResource.Set(bt.ImportPath, vv.Resource)
state.importResource.Set(bt.Import, vv.Resource)
for _, vvv := range instances.ByScriptID(vv.ID) {
bt.Instances = append(bt.Instances, batchTemplateExecution{ID: vvv.instanceID.instanceID, Params: vvv.Params})
bt.Instances = append(bt.Instances, scriptInstanceBatchTemplateContext{instance: vvv})
sort.Slice(bt.Instances, func(i, j int) bool {
return bt.Instances[i].ID < bt.Instances[j].ID
return bt.Instances[i].ID() < bt.Instances[j].ID()
})
}
t.Modules = append(t.Modules, bt)
t.Scripts = append(t.Scripts, bt)
}

sort.Slice(t.Modules, func(i, j int) bool {
return t.Modules[i].ID < t.Modules[j].ID
sort.Slice(t.Scripts, func(i, j int) bool {
return t.Scripts[i].ID < t.Scripts[j].ID
})

r, s, err := b.client.buildBatch(t)
Expand Down Expand Up @@ -1028,7 +1059,7 @@ const nsBundle = "__hugo-js-bundle"

func (ns *Namespace) buildBatch(t *batchTemplateContext) (resource.Resource, string, error) {
var buf bytes.Buffer
if err := batchEsmCallbackTemplate.Execute(&buf, t); err != nil {
if err := batchEsmRunnerTemplate.Execute(&buf, t); err != nil {
return nil, "", err
}

Expand All @@ -1041,12 +1072,12 @@ func (ns *Namespace) buildBatch(t *batchTemplateContext) (resource.Resource, str
return r, s, nil
}

//go:embed batch-esm-callback.gotmpl
var batchEsmCallbackTemplateString string
var batchEsmCallbackTemplate *template.Template
//go:embed batch-esm-runner.gotmpl
var batchEsmRunnerTemplateString string
var batchEsmRunnerTemplate *template.Template

func init() {
batchEsmCallbackTemplate = template.Must(template.New("batch-esm-callback").Parse(batchEsmCallbackTemplateString))
batchEsmRunnerTemplate = template.Must(template.New("batch-esm-runner").Parse(batchEsmRunnerTemplateString))
}

func fromJSONToMeta(cwd, s string) esBuildResultMeta {
Expand Down
Loading

0 comments on commit a07f8f9

Please sign in to comment.