Skip to content

Commit

Permalink
Refactor component events (#751)
Browse files Browse the repository at this point in the history
* add onLifecyleEvent to UI interface

* refactor app update

* refactor app install change

* refactor resize

* refactor nav

* rename onLifecycleEvent to onComponentEvent
  • Loading branch information
maxence-charriere committed Aug 1, 2022
1 parent 62f0c31 commit bbecd66
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 134 deletions.
93 changes: 39 additions & 54 deletions pkg/app/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"context"
"io"
"net/url"
"reflect"
"strings"

Expand Down Expand Up @@ -103,10 +102,6 @@ type Navigator interface {
OnNav(Context)
}

type deprecatedNavigator interface {
OnNav(Context, *url.URL)
}

// Updater is the interface that describes a component that can do additional
// instructions when one of its exported fields is modified by its nearest
// parent component.
Expand Down Expand Up @@ -143,9 +138,11 @@ type Resizer interface {
OnResize(Context)
}

type deprecatedResizer interface {
OnAppResize(Context)
}
// Component events.
type nav struct{}
type appUpdate struct{}
type appInstallChange struct{}
type resize struct{}

// Compo represents the base struct to use in order to build a component.
type Compo struct {
Expand Down Expand Up @@ -207,7 +204,7 @@ func (c *Compo) Update() {
// implement the Resizer interface.
func (c *Compo) ResizeContent() {
c.dispatch(func(Context) {
c.root.onResize()
c.root.onComponentEvent(resize{})
})
}

Expand Down Expand Up @@ -437,71 +434,59 @@ func (c *Compo) render() UI {
return elems[0]
}

func (c *Compo) onNav(u *url.URL) {
defer c.root.onNav(u)
func (c *Compo) preRender(p Page) {
c.root.preRender(p)

if nav, ok := c.self().(Navigator); ok {
c.dispatch(nav.OnNav)
return
if initializer, ok := c.self().(Initializer); ok {
initializer.OnInit()
}

if nav, ok := c.self().(deprecatedNavigator); ok {
Log(errors.New("a deprecated component interface is in use").
Tag("component", reflect.TypeOf(c.self())).
Tag("interface", "app.Navigator").
Tag("method-current", "OnNav(app.Context, *url.URL)").
Tag("method-fix", "OnNav(app.Context)").
Tag("how-to-fix", "refactor component to use the right method"))
c.dispatch(func(ctx Context) {
nav.OnNav(ctx, u)
})
if preRenderer, ok := c.self().(PreRenderer); ok {
c.dispatch(preRenderer.OnPreRender)
}
}

func (c *Compo) onAppUpdate() {
c.root.onAppUpdate()
func (c *Compo) onComponentEvent(le any) {
switch le := le.(type) {
case nav:
c.onNav(le)

if updater, ok := c.self().(AppUpdater); ok {
c.dispatch(updater.OnAppUpdate)
}
}
case appUpdate:
c.onAppUpdate(le)

func (c *Compo) onAppInstallChange() {
c.root.onAppInstallChange()
case appInstallChange:
c.onAppInstallChange(le)

if installer, ok := c.self().(AppInstaller); ok {
c.dispatch(installer.OnAppInstallChange)
case resize:
c.onResize(le)
}
}

func (c *Compo) onResize() {
defer c.root.onResize()
c.root.onComponentEvent(le)
}

if resizer, ok := c.self().(Resizer); ok {
c.dispatch(resizer.OnResize)
func (c *Compo) onNav(n nav) {
if nav, ok := c.self().(Navigator); ok {
c.dispatch(nav.OnNav)
return
}
}

if resizer, ok := c.self().(deprecatedResizer); ok {
Log(errors.New("a deprecated component interface is in use").
Tag("component", reflect.TypeOf(c.self())).
Tag("interface", "app.Resizer").
Tag("method-current", "OnAppResize(app.Context)").
Tag("method-fix", "OnResize(app.Context)").
Tag("how-to-fix", "refactor component to use the right method"))
c.dispatch(resizer.OnAppResize)
func (c *Compo) onAppUpdate(au appUpdate) {
if updater, ok := c.self().(AppUpdater); ok {
c.dispatch(updater.OnAppUpdate)
}
}

func (c *Compo) preRender(p Page) {
c.root.preRender(p)

if initializer, ok := c.self().(Initializer); ok {
initializer.OnInit()
func (c *Compo) onAppInstallChange(ai appInstallChange) {
if installer, ok := c.self().(AppInstaller); ok {
c.dispatch(installer.OnAppInstallChange)
}
}

if preRenderer, ok := c.self().(PreRenderer); ok {
c.dispatch(preRenderer.OnPreRender)
func (c *Compo) onResize(r resize) {
if resizer, ok := c.self().(Resizer); ok {
c.dispatch(resizer.OnResize)
return
}
}

Expand Down
14 changes: 2 additions & 12 deletions pkg/app/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"context"
"io"
"net/url"

"github.com/maxence-charriere/go-app/v9/pkg/errors"
)
Expand Down Expand Up @@ -126,19 +125,10 @@ func (c condition) updateWith(UI) error {
Tag("kind", c.Kind())
}

func (c condition) onNav(*url.URL) {
}

func (c condition) onAppUpdate() {
}

func (c condition) onAppInstallChange() {
}

func (c condition) onResize() {
func (c condition) preRender(Page) {
}

func (c condition) preRender(Page) {
func (c condition) onComponentEvent(any) {
}

func (c condition) html(w io.Writer) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (e *engine) Nav(u *url.URL) {
Mode: Update,
Source: e.Body,
Function: func(ctx Context) {
ctx.Src().onNav(u)
ctx.Src().onComponentEvent(nav{})
},
})
}
Expand All @@ -249,7 +249,7 @@ func (e *engine) AppUpdate() {
Mode: Update,
Source: e.Body,
Function: func(ctx Context) {
ctx.Src().onAppUpdate()
ctx.Src().onComponentEvent(appUpdate{})
},
})
}
Expand All @@ -259,7 +259,7 @@ func (e *engine) AppInstallChange() {
Mode: Update,
Source: e.Body,
Function: func(ctx Context) {
ctx.Src().onAppInstallChange()
ctx.Src().onComponentEvent(appInstallChange{})
},
})
}
Expand All @@ -269,7 +269,7 @@ func (e *engine) AppResize() {
Mode: Update,
Source: e.Body,
Function: func(ctx Context) {
ctx.Src().onResize()
ctx.Src().onComponentEvent(resize{})
},
})
}
Expand Down
27 changes: 4 additions & 23 deletions pkg/app/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"context"
"io"
"net/url"
"reflect"

"github.com/maxence-charriere/go-app/v9/pkg/errors"
Expand Down Expand Up @@ -295,33 +294,15 @@ func (e *htmlElement) setChildren(v ...UI) {
e.children = FilterUIElems(v...)
}

func (e *htmlElement) onNav(u *url.URL) {
for _, c := range e.getChildren() {
c.onNav(u)
}
}

func (e *htmlElement) onAppUpdate() {
for _, c := range e.getChildren() {
c.onAppUpdate()
}
}

func (e *htmlElement) onAppInstallChange() {
for _, c := range e.getChildren() {
c.onAppInstallChange()
}
}

func (e *htmlElement) onResize() {
func (e *htmlElement) preRender(p Page) {
for _, c := range e.getChildren() {
c.onResize()
c.preRender(p)
}
}

func (e *htmlElement) preRender(p Page) {
func (e *htmlElement) onComponentEvent(le any) {
for _, c := range e.getChildren() {
c.preRender(p)
c.onComponentEvent(le)
}
}

Expand Down
6 changes: 1 addition & 5 deletions pkg/app/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"context"
"io"
"net/url"
"reflect"
"strings"
)
Expand Down Expand Up @@ -34,11 +33,8 @@ type UI interface {
dismount()
canUpdateWith(UI) bool
updateWith(UI) error
onNav(*url.URL)
onAppUpdate()
onAppInstallChange()
onResize()
preRender(Page)
onComponentEvent(any)
html(w io.Writer)
htmlWithIndent(w io.Writer, indent int)
}
Expand Down
14 changes: 2 additions & 12 deletions pkg/app/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"context"
"io"
"net/url"
"reflect"
"sort"

Expand Down Expand Up @@ -156,19 +155,10 @@ func (r rangeLoop) updateWith(UI) error {
Tag("kind", r.Kind())
}

func (r rangeLoop) onNav(*url.URL) {
}

func (r rangeLoop) onAppUpdate() {
}

func (r rangeLoop) onAppInstallChange() {
}

func (r rangeLoop) onResize() {
func (r rangeLoop) preRender(Page) {
}

func (r rangeLoop) preRender(Page) {
func (r rangeLoop) onComponentEvent(any) {
}

func (r rangeLoop) html(w io.Writer) {
Expand Down
14 changes: 2 additions & 12 deletions pkg/app/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"context"
"io"
"net/url"
"strings"

"github.com/maxence-charriere/go-app/v9/pkg/errors"
Expand Down Expand Up @@ -136,19 +135,10 @@ func (r *raw) updateWith(n UI) error {
return nil
}

func (r *raw) onNav(*url.URL) {
}

func (r *raw) onAppUpdate() {
}

func (r *raw) onAppInstallChange() {
}

func (r *raw) onResize() {
func (r *raw) preRender(Page) {
}

func (r *raw) preRender(Page) {
func (r *raw) onComponentEvent(any) {
}

func (r *raw) html(w io.Writer) {
Expand Down
14 changes: 2 additions & 12 deletions pkg/app/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"html"
"io"
"net/url"

"github.com/maxence-charriere/go-app/v9/pkg/errors"
)
Expand Down Expand Up @@ -109,19 +108,10 @@ func (t *text) updateWith(n UI) error {
return nil
}

func (t *text) onNav(*url.URL) {
}

func (t *text) onAppUpdate() {
}

func (t *text) onAppInstallChange() {
}

func (t *text) onResize() {
func (t *text) preRender(Page) {
}

func (t *text) preRender(Page) {
func (t *text) onComponentEvent(any) {
}

func (t *text) html(w io.Writer) {
Expand Down

0 comments on commit bbecd66

Please sign in to comment.