From 7e682d6308712cb89f9d8afaeed289b3b200f926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 18 Sep 2024 13:28:44 +0200 Subject: [PATCH] Rework API --- tpl/js/batch.go | 94 ++++++++++++++------------------ tpl/js/batch_integration_test.go | 12 ++-- 2 files changed, 48 insertions(+), 58 deletions(-) diff --git a/tpl/js/batch.go b/tpl/js/batch.go index 7603fa7710d..7331ffb9b99 100644 --- a/tpl/js/batch.go +++ b/tpl/js/batch.go @@ -40,6 +40,42 @@ import ( "github.com/spf13/cast" ) +type Batcher interface { + UseScript(id string) BatcherScript + UseScriptMany(id string) BatcherScriptMany + Build() (*Package, error) +} + +type BatcherScript interface { + BatcherScriptOps + types.Closer +} + +type BatcherScriptOps interface { + ResourceGetSetter + AddInstance(id string, opts any) string +} + +type BatcherScriptMany interface { + BatcherScriptManyOps + types.Closer +} + +type BatcherScriptManyOps interface { + CallbackGetSetter + UseScript(id string) BatcherScript +} + +type CallbackGetSetter interface { + GetCallback() resource.Resource + SetCallback(r resource.Resource) string +} + +type ResourceGetSetter interface { + GetResource() resource.Resource + SetResource(r resource.Resource) string +} + func (ns *Namespace) Batch(id string, store *maps.Scratch) (Batcher, error) { key := path.Join(nsBundle, id) b := store.GetOrCreate(key, func() any { @@ -48,7 +84,7 @@ func (ns *Namespace) Batch(id string, store *maps.Scratch) (Batcher, error) { return b.(*batcher), nil } -func (b *batcher) UseScriptOne(id string) BatcherScriptOne { +func (b *batcher) UseScript(id string) BatcherScript { b.mu.Lock() one, found := b.scriptOnes[id] @@ -65,7 +101,7 @@ func (b *batcher) UseScriptOne(id string) BatcherScriptOne { var closeOnce sync.Once return struct { - BatcherScriptOneOps + BatcherScriptOps types.Closer }{ one, @@ -108,52 +144,6 @@ func (b *batcher) UseScriptMany(id string) BatcherScriptMany { } } -type Batcher interface { - UseScriptOne(id string) BatcherScriptOne - UseScriptMany(id string) BatcherScriptMany - Build() (*Package, error) -} - -type BatcherScriptOne interface { - BatcherScriptOneOps - types.Closer -} - -type BatcherScriptOneOps interface { - ResourceGetSetter - SetInstance(opts any) string -} - -type BatcherScriptMany interface { - BatcherScriptManyOps - types.Closer -} - -type BatcherScriptItem interface { - BatcherScriptItemOps - types.Closer -} - -type BatcherScriptItemOps interface { - ResourceGetSetter - AddInstance(id string, opts any) string -} - -type BatcherScriptManyOps interface { - GetCallback() resource.Resource - SetCallback(r resource.Resource) string - UseItem(id string) BatcherScriptItem -} - -type BundleCommonScriptOps interface { - ResourceGetSetter -} - -type ResourceGetSetter interface { - GetResource() resource.Resource - SetResource(r resource.Resource) string -} - type close func() error func (c close) Close() error { @@ -162,11 +152,11 @@ func (c close) Close() error { var ( _ Batcher = (*batcher)(nil) - _ BatcherScriptOneOps = (*scriptOne)(nil) + _ BatcherScriptOps = (*scriptOne)(nil) _ BatcherScriptManyOps = (*scriptMany)(nil) ) -func (b *scriptOne) SetInstance(opts any) string { +func (b *scriptOne) AddInstance(id string, opts any) string { panic("not implemented") /*if b.r == nil { panic("resource not set") @@ -208,7 +198,7 @@ func (b *scriptMany) SetCallback(r resource.Resource) string { return "" } -func (b *scriptMany) UseItem(id string) BatcherScriptItem { +func (b *scriptMany) UseScript(id string) BatcherScript { item, found := b.items[id] if !found { item = &scriptManyItem{id: id, instances: make(map[string]scriptInstance), client: b.client} @@ -222,7 +212,7 @@ func (b *scriptMany) UseItem(id string) BatcherScriptItem { var closeOnce sync.Once return struct { - BatcherScriptItemOps + BatcherScriptOps types.Closer }{ item, diff --git a/tpl/js/batch_integration_test.go b/tpl/js/batch_integration_test.go index 7882ebb4c3c..662b77fc4e2 100644 --- a/tpl/js/batch_integration_test.go +++ b/tpl/js/batch_integration_test.go @@ -89,30 +89,30 @@ console.log('main2.React', React) -- layouts/index.html -- Home. {{ $bundle := (js.Batch "mybundle" .Store) }} -{{ with $bundle.UseScriptOne "main1" }} +{{ with $bundle.UseScript "main1" }} {{ if not .GetResource }} {{ .SetResource (resources.Get "js/main1.js") }} {{ end }} - {{ .SetInstance (dict "title" "Main1 Instance") }} + {{ .AddInstance "foo" (dict "title" "Main1 Instance") }} {{ end }} - {{ with $bundle.UseScriptOne "main2" }} + {{ with $bundle.UseScript "main2" }} {{ if not .GetResource }} {{ .SetResource (resources.Get "js/main2.js") }} {{ end }} - {{ .SetInstance (dict "title" "Main2 Instance") }} + {{ .AddInstance "foo" (dict "title" "Main2 Instance") }} {{ end }} {{ with $bundle.UseScriptMany "reactbatch" }} {{ if not .GetCallback }} {{ .SetCallback (resources.Get "js/reactcallback.js") }} {{ end }} - {{ with .UseItem "r1" }} + {{ with .UseScript "r1" }} {{ if not .GetResource }} {{ .SetResource (resources.Get "js/react1.jsx") }} {{ end }} {{ .AddInstance "i1" (dict "title" "Instance 1") }} {{ .AddInstance "i2" (dict "title" "Instance 2") }} {{ end }} - {{ with .UseItem "r2" }} + {{ with .UseScript "r2" }} {{ if not .GetResource }} {{ .SetResource (resources.Get "js/react2.jsx") }} {{ end }}