Skip to content

Commit

Permalink
Refactor engine (#752)
Browse files Browse the repository at this point in the history
* enginex dispatcher implementation

* fix tests

* engineX client/server dispatcher

* enginex cleanup

* frame backoff

* init and start once

* use engine x

* rename enginex to engine + unit tests

* try preventComponentUpdate

* Update component.go

* add remove component update

* Revert "add remove component update"

This reverts commit 185f470.

* context prevent update
  • Loading branch information
maxence-charriere committed Aug 11, 2022
1 parent bbecd66 commit a72603a
Show file tree
Hide file tree
Showing 14 changed files with 544 additions and 678 deletions.
Binary file modified docs/web/app.wasm
Binary file not shown.
554 changes: 292 additions & 262 deletions docs/web/documents/reference.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func RunWhenOnBrowser() {
staticResourcesResolver := newClientStaticResourceResolver(Getenv("GOAPP_STATIC_RESOURCES_URL"))

disp := engine{
UpdateRate: engineUpdateRate,
FrameRate: engineUpdateRate,
LocalStorage: newJSStorage("localStorage"),
SessionStorage: newJSStorage("sessionStorage"),
ResolveStaticResources: staticResourcesResolver,
StaticResourceResolver: staticResourcesResolver,
ActionHandlers: actionHandlers,
}
disp.Page = browserPage{dispatcher: &disp}
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (c *Compo) mount(d Dispatcher) error {
Tag("kind", c.Kind())
}

if initializer, ok := c.self().(Initializer); ok && !d.runsInServer() {
if initializer, ok := c.self().(Initializer); ok && !d.isServerSide() {
initializer.OnInit()
}

Expand All @@ -297,7 +297,7 @@ func (c *Compo) mount(d Dispatcher) error {
root.setParent(c.this)
c.root = root

if c.getDispatcher().runsInServer() {
if c.getDispatcher().isServerSide() {
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/app/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func TestPreRenderer(t *testing.T) {
d.PreRender()
d.Consume()
require.True(t, h.preRenderer)
require.Equal(t, "world", d.currentPage().Title())
require.Equal(t, "world", d.getCurrentPage().Title())
}

func TestNestedPreRenderer(t *testing.T) {
Expand All @@ -393,7 +393,7 @@ func TestNestedPreRenderer(t *testing.T) {
d.PreRender()
d.Consume()
require.True(t, h.preRenderer)
require.Equal(t, "world", d.currentPage().Title())
require.Equal(t, "world", d.getCurrentPage().Title())
}

func TestNestedInComponentPreRenderer(t *testing.T) {
Expand All @@ -403,7 +403,7 @@ func TestNestedInComponentPreRenderer(t *testing.T) {

d.PreRender()
d.Consume()
require.Equal(t, "bar", d.currentPage().Title())
require.Equal(t, "bar", d.getCurrentPage().Title())
}

func TestUpdater(t *testing.T) {
Expand Down
13 changes: 10 additions & 3 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ type Context interface {

// Returns the service to setup and display notifications.
Notifications() NotificationService

// Prevents the component that contains the context source to be updated.
PreventUpdate()
}

type uiContext struct {
Expand Down Expand Up @@ -289,11 +292,11 @@ func (ctx uiContext) ResolveStaticResource(path string) string {
}

func (ctx uiContext) LocalStorage() BrowserStorage {
return ctx.Dispatcher().localStorage()
return ctx.Dispatcher().getLocalStorage()
}

func (ctx uiContext) SessionStorage() BrowserStorage {
return ctx.Dispatcher().sessionStorage()
return ctx.Dispatcher().getSessionStorage()
}

func (ctx uiContext) ScrollTo(id string) {
Expand Down Expand Up @@ -367,6 +370,10 @@ func (ctx uiContext) Notifications() NotificationService {
return NotificationService{dispatcher: ctx.Dispatcher()}
}

func (ctx uiContext) PreventUpdate() {
ctx.Dispatcher().preventComponentUpdate(getComponent(ctx.src))
}

func (ctx uiContext) cryptoKey() string {
return strings.ReplaceAll(ctx.DeviceID(), "-", "")
}
Expand All @@ -377,7 +384,7 @@ func makeContext(src UI) Context {
src: src,
jsSrc: src.JSValue(),
appUpdateAvailable: appUpdateAvailable,
page: src.getDispatcher().currentPage(),
page: src.getDispatcher().getCurrentPage(),
disp: src.getDispatcher(),
}
}
19 changes: 13 additions & 6 deletions pkg/app/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ type Dispatcher interface {
Wait()

start(context.Context)
currentPage() Page
localStorage() BrowserStorage
sessionStorage() BrowserStorage
runsInServer() bool
getCurrentPage() Page
getLocalStorage() BrowserStorage
getSessionStorage() BrowserStorage
isServerSide() bool
resolveStaticResource(string) string
removeFromUpdates(Composer)
preventComponentUpdate(Composer)
}

// ClientDispatcher is the interface that describes a dispatcher that emulates a
Expand Down Expand Up @@ -131,7 +131,7 @@ type ServerDispatcher interface {
// client environment.
func NewServerTester(n UI) ServerDispatcher {
e := &engine{
RunsInServer: true,
IsServerSide: true,
ActionHandlers: actionHandlers,
}
e.init()
Expand All @@ -147,6 +147,13 @@ type Dispatch struct {
Function func(Context)
}

func (d Dispatch) do() {
if d.Source == nil || !d.Source.Mounted() || d.Function == nil {
return
}
d.Function(makeContext(d.Source))
}

// DispatchMode represents how a dispatch is processed.
type DispatchMode int

Expand Down
4 changes: 2 additions & 2 deletions pkg/app/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func testDispatcherAsyncWait(t *testing.T, d Dispatcher) {
func TestDispatcherLocalStorage(t *testing.T) {
d := NewClientTester(&hello{})
defer d.Close()
testBrowserStorage(t, d.localStorage())
testBrowserStorage(t, d.getLocalStorage())
}

func TestDispatcherSessionStorage(t *testing.T) {
d := NewClientTester(&hello{})
defer d.Close()
testBrowserStorage(t, d.sessionStorage())
testBrowserStorage(t, d.getSessionStorage())
}

0 comments on commit a72603a

Please sign in to comment.