From 0b579312ecf9bdd5fcce30bc7da7f9b4a5a22efe Mon Sep 17 00:00:00 2001 From: Maxence Charriere Date: Sat, 30 Sep 2023 16:38:32 +0800 Subject: [PATCH] remove internal node context (#894) --- pkg/app/app.go | 18 ++++++------------ pkg/app/component.go | 11 ----------- pkg/app/condition.go | 5 ----- pkg/app/context.go | 2 +- pkg/app/html.go | 18 +++++------------- pkg/app/node.go | 2 -- pkg/app/node_test.go | 2 -- pkg/app/range.go | 5 ----- pkg/app/raw.go | 5 ----- pkg/app/text.go | 5 ----- 10 files changed, 12 insertions(+), 61 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index c9cd62b06..147f69d8a 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -196,26 +196,20 @@ func internalURLChecker() func(string) bool { } func newClientBody(d Dispatcher) *htmlBody { - ctx, cancel := context.WithCancel(context.Background()) body := &htmlBody{ htmlElement: htmlElement{ - tag: "body", - context: ctx, - contextCancel: cancel, - dispatcher: d, - jsElement: Window().Get("document").Get("body"), + tag: "body", + dispatcher: d, + jsElement: Window().Get("document").Get("body"), }, } body.setSelf(body) - ctx, cancel = context.WithCancel(context.Background()) content := &htmlDiv{ htmlElement: htmlElement{ - tag: "div", - context: ctx, - contextCancel: cancel, - dispatcher: d, - jsElement: body.JSValue().firstElementChild(), + tag: "div", + dispatcher: d, + jsElement: body.JSValue().firstElementChild(), }, } content.setSelf(content) diff --git a/pkg/app/component.go b/pkg/app/component.go index 031aaddf0..c5398e623 100644 --- a/pkg/app/component.go +++ b/pkg/app/component.go @@ -1,7 +1,6 @@ package app import ( - "context" "io" "reflect" "strings" @@ -148,8 +147,6 @@ type resize struct{} // Compo represents the base struct to use in order to build a component. type Compo struct { disp Dispatcher - ctx context.Context - ctxCancel func() parentElem UI root UI this Composer @@ -163,8 +160,6 @@ func (c *Compo) JSValue() Value { // Mounted reports whether the component is mounted. func (c *Compo) Mounted() bool { return c.getDispatcher() != nil && - c.ctx != nil && - c.ctx.Err() == nil && c.root != nil && c.root.Mounted() && c.self() != nil } @@ -240,10 +235,6 @@ func (c *Compo) setSelf(v UI) { c.this = nil } -func (c *Compo) getContext() context.Context { - return c.ctx -} - func (c *Compo) getDispatcher() Dispatcher { return c.disp } @@ -280,7 +271,6 @@ func (c *Compo) mount(d Dispatcher) error { } c.disp = d - c.ctx, c.ctxCancel = context.WithCancel(context.Background()) root := c.render() if err := mount(d, root); err != nil { @@ -306,7 +296,6 @@ func (c *Compo) mount(d Dispatcher) error { } func (c *Compo) dismount() { - c.ctxCancel() dismount(c.root) if dismounter, ok := c.this.(Dismounter); ok { diff --git a/pkg/app/condition.go b/pkg/app/condition.go index d0d08f80f..4a7c7395e 100644 --- a/pkg/app/condition.go +++ b/pkg/app/condition.go @@ -1,7 +1,6 @@ package app import ( - "context" "io" "github.com/maxence-charriere/go-app/v9/pkg/errors" @@ -106,10 +105,6 @@ func (c condition) self() UI { func (c condition) setSelf(UI) { } -func (c condition) getContext() context.Context { - return nil -} - func (c condition) getDispatcher() Dispatcher { return nil } diff --git a/pkg/app/context.go b/pkg/app/context.go index be4943822..8f8b47f94 100644 --- a/pkg/app/context.go +++ b/pkg/app/context.go @@ -380,7 +380,7 @@ func (ctx uiContext) cryptoKey() string { func makeContext(src UI) Context { return uiContext{ - Context: src.getContext(), + Context: context.Background(), src: src, jsSrc: src.JSValue(), appUpdateAvailable: appUpdateAvailable, diff --git a/pkg/app/html.go b/pkg/app/html.go index ad36f7789..a1f655b6b 100644 --- a/pkg/app/html.go +++ b/pkg/app/html.go @@ -1,7 +1,6 @@ package app import ( - "context" "io" "reflect" "strconv" @@ -18,11 +17,9 @@ type htmlElement struct { parent UI children []UI - context context.Context - contextCancel func() - dispatcher Dispatcher - jsElement Value - this UI + dispatcher Dispatcher + jsElement Value + this UI } func (e *htmlElement) JSValue() Value { @@ -30,7 +27,7 @@ func (e *htmlElement) JSValue() Value { } func (e *htmlElement) Mounted() bool { - return e.context != nil && e.context.Err() == nil + return e.jsElement != nil } func (e *htmlElement) name() string { @@ -45,10 +42,6 @@ func (e *htmlElement) setSelf(v UI) { e.this = v } -func (e *htmlElement) getContext() context.Context { - return e.context -} - func (e *htmlElement) getDispatcher() Dispatcher { return e.dispatcher } @@ -78,7 +71,6 @@ func (e *htmlElement) mount(d Dispatcher) error { return errors.New("html element is already mounted").WithTag("tag", e.tag) } - e.context, e.contextCancel = context.WithCancel(context.Background()) e.dispatcher = d jsElement, err := Window().createElement(e.tag, e.xmlns) @@ -117,7 +109,7 @@ func (e *htmlElement) dismount() { eh.Dismount() } - e.contextCancel() + e.jsElement = nil } func (e *htmlElement) canUpdateWith(v UI) bool { diff --git a/pkg/app/node.go b/pkg/app/node.go index 97a7ec08b..aeaccbb9e 100644 --- a/pkg/app/node.go +++ b/pkg/app/node.go @@ -1,7 +1,6 @@ package app import ( - "context" "io" "reflect" "strings" @@ -19,7 +18,6 @@ type UI interface { name() string self() UI setSelf(UI) - getContext() context.Context getDispatcher() Dispatcher getAttributes() attributes getEventHandlers() eventHandlers diff --git a/pkg/app/node_test.go b/pkg/app/node_test.go index 29146226d..583850c48 100644 --- a/pkg/app/node_test.go +++ b/pkg/app/node_test.go @@ -144,7 +144,6 @@ func testMounted(t *testing.T, n UI) { case *text, *raw: default: - require.NoError(t, n.getContext().Err()) require.NotNil(t, n.self()) } @@ -162,7 +161,6 @@ func testDismounted(t *testing.T, n UI) { case *text, *raw: default: - require.Error(t, n.getContext().Err()) require.Nil(t, n.self()) } diff --git a/pkg/app/range.go b/pkg/app/range.go index 50ecb2b99..bd15012e9 100644 --- a/pkg/app/range.go +++ b/pkg/app/range.go @@ -1,7 +1,6 @@ package app import ( - "context" "io" "reflect" "sort" @@ -105,10 +104,6 @@ func (r rangeLoop) self() UI { func (r rangeLoop) setSelf(UI) { } -func (r rangeLoop) getContext() context.Context { - return nil -} - func (r rangeLoop) getDispatcher() Dispatcher { return nil } diff --git a/pkg/app/raw.go b/pkg/app/raw.go index 37506dd20..fce95ae3b 100644 --- a/pkg/app/raw.go +++ b/pkg/app/raw.go @@ -1,7 +1,6 @@ package app import ( - "context" "io" "strings" @@ -54,10 +53,6 @@ func (r *raw) self() UI { func (r *raw) setSelf(UI) { } -func (r *raw) getContext() context.Context { - return nil -} - func (r *raw) getDispatcher() Dispatcher { return r.disp } diff --git a/pkg/app/text.go b/pkg/app/text.go index 7c47e31d8..5ff645024 100644 --- a/pkg/app/text.go +++ b/pkg/app/text.go @@ -1,7 +1,6 @@ package app import ( - "context" "fmt" "html" "io" @@ -45,10 +44,6 @@ func (t *text) self() UI { func (t *text) setSelf(n UI) { } -func (t *text) getContext() context.Context { - return context.TODO() -} - func (t *text) getDispatcher() Dispatcher { return t.disp }