Skip to content

Commit

Permalink
Rework API
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Sep 18, 2024
1 parent bb0ddae commit 7e682d6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 58 deletions.
94 changes: 42 additions & 52 deletions tpl/js/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand All @@ -65,7 +101,7 @@ func (b *batcher) UseScriptOne(id string) BatcherScriptOne {
var closeOnce sync.Once

return struct {
BatcherScriptOneOps
BatcherScriptOps
types.Closer
}{
one,
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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}
Expand All @@ -222,7 +212,7 @@ func (b *scriptMany) UseItem(id string) BatcherScriptItem {
var closeOnce sync.Once

return struct {
BatcherScriptItemOps
BatcherScriptOps
types.Closer
}{
item,
Expand Down
12 changes: 6 additions & 6 deletions tpl/js/batch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down

0 comments on commit 7e682d6

Please sign in to comment.