Skip to content

Commit

Permalink
node manager make context
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Oct 3, 2023
1 parent a740ae0 commit d050bde
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ type uiContext struct {
appUpdateAvailable bool
page Page
disp Dispatcher

emit func(src UI, f func())
}

func (ctx uiContext) Src() UI {
Expand Down Expand Up @@ -263,6 +265,9 @@ func (ctx uiContext) After(d time.Duration, fn func(Context)) {
}

func (ctx uiContext) Emit(fn func()) {
if ctx.emit != nil {
ctx.emit(ctx.Src(), fn)
}
ctx.Dispatcher().Emit(ctx.Src(), fn)
}

Expand Down
48 changes: 46 additions & 2 deletions pkg/app/node.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package app

import (
"context"
"io"
"net/url"
"reflect"
"strings"
"sync"
Expand Down Expand Up @@ -142,9 +144,17 @@ func PrintHTMLWithIndent(w io.Writer, ui UI) {
// nodeManager orchestrates the lifecycle of UI elements, providing specialized
// mechanisms for mounting, dismounting, and updating nodes.
type nodeManager struct {
// Ctx serves as a base context, utilized to derive child contexts providing
// mechanisms to interact with the browser, manage pages, handle
// concurrency, and facilitate component communication.
Ctx context.Context

// ResolveURL is used to transform attributes that hold URL values.
ResolveURL attributeURLResolver

// Page represents a reference to the current web page.
Page Page

// EmitHTMLEvent is called when a specific HTML event occurs on a UI
// element. 'src' represents the source UI element triggering the event, and
// 'f' is the callback to be executed in response.
Expand All @@ -154,12 +164,31 @@ type nodeManager struct {
}

func (m *nodeManager) init() {
if m.Ctx == nil {
m.Ctx = context.Background()
}

if m.ResolveURL == nil {
m.ResolveURL = func(s string) string {
return s
}
}

if m.Page == nil {
url, _ := url.Parse("https://goapp.dev")
if IsServer {
m.Page = &requestPage{
url: url,
resolveStaticResource: m.ResolveURL,
}
} else {
m.Page = browserPage{
url: url,
resolveStaticResource: m.ResolveURL,
}
}
}

if m.EmitHTMLEvent == nil {
m.EmitHTMLEvent = func(u UI, f func()) {
f()
Expand Down Expand Up @@ -276,8 +305,7 @@ func (m *nodeManager) mountHTMLEventHandler(v HTML, handler eventHandler) eventH
if len(args) != 0 {
event := Event{Value: args[0]}
trackMousePosition(event)
handler.goHandler(nil, event)
panic("TODO: nodeManager make context")
handler.goHandler(m.MakeContext(v), event)
}
return nil
})
Expand Down Expand Up @@ -394,3 +422,19 @@ func (m *nodeManager) updateComponent(v, new Composer) (UI, error) {
func (m *nodeManager) updateRawHTML(v, new *raw) (UI, error) {
panic("not implemented")
}

// MakeContext creates and returns a new context derived from the nodeManager's
// base context (Ctx). The derived context is configured and tailored for the
// provided UI element 'v'.
func (m *nodeManager) MakeContext(v UI) Context {
m.initOnce.Do(m.init)

return uiContext{
Context: m.Ctx,
src: v,
jsSrc: v.JSValue(),
appUpdateAvailable: appUpdateAvailable,
page: m.Page,
emit: m.EmitHTMLEvent,
}
}
14 changes: 14 additions & 0 deletions pkg/app/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,17 @@ func TestNodeManagerUpdate(t *testing.T) {
require.Equal(t, "hello", greeting.(*text).value)
})
}

func TestNodeManagerMakeContext(t *testing.T) {
var m nodeManager

div, err := m.Mount(1, Div())
require.NoError(t, err)

ctx := m.MakeContext(div).(uiContext)
require.NotZero(t, ctx)
require.NotNil(t, ctx.src)
require.NotNil(t, ctx.jsSrc)
require.NotNil(t, ctx.page)
require.NotNil(t, ctx.emit)
}

0 comments on commit d050bde

Please sign in to comment.