Skip to content

Commit

Permalink
remove isServerSide from engine and dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Sep 28, 2023
1 parent 326e537 commit f2ebf02
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/app/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ func (c *Compo) mount(d Dispatcher) error {
root.setParent(c.this)
c.root = root

if mounter, ok := c.self().(Mounter); !c.getDispatcher().isServerSide() && ok {
if mounter, ok := c.self().(Mounter); IsClient && ok {
c.dispatch(mounter.OnMount)
return nil
}

if preRenderer, ok := c.self().(PreRenderer); c.getDispatcher().isServerSide() && ok {
if preRenderer, ok := c.self().(PreRenderer); IsServer && ok {
c.dispatch(preRenderer.OnPreRender)
return nil
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/app/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ func TestPreRenderer(t *testing.T) {
defer d.Close()

d.Consume()
if IsClient {
require.False(t, h.preRenderer)
require.Empty(t, d.getCurrentPage().Title())
return
}

require.True(t, h.preRenderer)
require.Equal(t, "world", d.getCurrentPage().Title())
}
Expand All @@ -390,6 +396,12 @@ func TestNestedPreRenderer(t *testing.T) {
defer d.Close()

d.Consume()

if IsClient {
require.False(t, h.preRenderer)
require.Empty(t, d.getCurrentPage().Title())
return
}
require.True(t, h.preRenderer)
require.Equal(t, "world", d.getCurrentPage().Title())
}
Expand All @@ -400,6 +412,11 @@ func TestNestedInComponentPreRenderer(t *testing.T) {
defer d.Close()

d.Consume()

if IsClient {
require.Empty(t, d.getCurrentPage().Title())
return
}
require.Equal(t, "bar", d.getCurrentPage().Title())
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/app/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type Dispatcher interface {
getCurrentPage() Page
getLocalStorage() BrowserStorage
getSessionStorage() BrowserStorage
isServerSide() bool
resolveStaticResource(string) string
removeComponentUpdate(Composer)
preventComponentUpdate(Composer)
Expand Down Expand Up @@ -138,7 +137,6 @@ type ServerDispatcher interface {
// client environment.
func NewServerTester(n UI) ServerDispatcher {
e := &engine{
IsServerSide: true,
ActionHandlers: actionHandlers,
}
e.init()
Expand Down
7 changes: 0 additions & 7 deletions pkg/app/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ type engine struct {
// The page.
Page Page

// Reports whether the engine runs on server-side.
IsServerSide bool

// The storage use as local storage.
LocalStorage BrowserStorage

Expand Down Expand Up @@ -286,10 +283,6 @@ func (e *engine) getSessionStorage() BrowserStorage {
return e.SessionStorage
}

func (e *engine) isServerSide() bool {
return e.IsServerSide
}

func (e *engine) resolveStaticResource(path string) string {
return e.StaticResourceResolver(path)
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,6 @@ func (h *Handler) servePage(w http.ResponseWriter, r *http.Request) {

disp := engine{
Page: &page,
IsServerSide: true,
StaticResourceResolver: h.resolveStaticPath,
ActionHandlers: actionHandlers,
}
Expand All @@ -740,7 +739,6 @@ func (h *Handler) servePage(w http.ResponseWriter, r *http.Request) {
)
if err := mount(&disp, body); err != nil {
panic(errors.New("mounting pre-rendering container failed").
WithTag("server-side", disp.isServerSide()).
WithTag("body-type", reflect.TypeOf(disp.Body)).
Wrap(err))
}
Expand Down

0 comments on commit f2ebf02

Please sign in to comment.