Skip to content

Commit

Permalink
remove node kind
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Sep 28, 2023
1 parent df458a7 commit e793bf3
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 176 deletions.
22 changes: 6 additions & 16 deletions pkg/app/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ type Compo struct {
this Composer
}

// Kind returns the ui element kind.
func (c *Compo) Kind() Kind {
return Component
}

// JSValue returns the javascript value of the component root.
func (c *Compo) JSValue() Value {
return c.root.JSValue()
Expand Down Expand Up @@ -277,8 +272,7 @@ func (c *Compo) mount(d Dispatcher) error {
if c.Mounted() {
return errors.New("mounting component failed").
WithTag("reason", "already mounted").
WithTag("name", c.name()).
WithTag("kind", c.Kind())
WithTag("name", c.name())
}

if initializer, ok := c.self().(Initializer); ok {
Expand All @@ -292,7 +286,6 @@ func (c *Compo) mount(d Dispatcher) error {
if err := mount(d, root); err != nil {
return errors.New("mounting component failed").
WithTag("name", c.name()).
WithTag("kind", c.Kind()).
Wrap(err)
}
root.setParent(c.this)
Expand Down Expand Up @@ -322,9 +315,7 @@ func (c *Compo) dismount() {
}

func (c *Compo) canUpdateWith(v UI) bool {
return c.Mounted() &&
c.Kind() == v.Kind() &&
c.name() == v.name()
return c.Mounted() && c.name() == v.name()
}

func (c *Compo) updateWith(v UI) error {
Expand Down Expand Up @@ -401,22 +392,21 @@ func (c *Compo) replaceRoot(v UI) error {

if err := mount(c.getDispatcher(), new); err != nil {
return errors.New("replacing component root failed").
WithTag("kind", c.Kind()).
WithTag("name", c.name()).
WithTag("root-kind", old.Kind()).
WithTag("root-name", old.name()).
WithTag("new-root-kind", new.Kind()).
WithTag("new-root-name", new.name()).
Wrap(err)
}

var parent UI
for parent = c.getParent(); parent != nil && parent.Kind() != HTML; parent = parent.getParent() {
for parent = c.getParent(); parent != nil; parent = parent.getParent() {
if _, isCompo := parent.(Composer); !isCompo {
break
}
}

if parent == nil {
return errors.New("replacing component root failed").
WithTag("kind", c.Kind()).
WithTag("name", c.name()).
WithTag("reason", "coponent does not have html element parents")
}
Expand Down
12 changes: 2 additions & 10 deletions pkg/app/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ func (c condition) ElseSlice(elems func() []UI) Condition {
return c.ElseIfSlice(true, elems)
}

func (c condition) Kind() Kind {
return Selector
}

func (c condition) JSValue() Value {
return nil
}
Expand Down Expand Up @@ -138,9 +134,7 @@ func (c condition) getChildren() []UI {
}

func (c condition) mount(Dispatcher) error {
return errors.New("condition is not mountable").
WithTag("name", c.name()).
WithTag("kind", c.Kind())
return errors.New("condition is not mountable").WithTag("name", c.name())
}

func (c condition) dismount() {
Expand All @@ -151,9 +145,7 @@ func (c condition) canUpdateWith(UI) bool {
}

func (c condition) updateWith(UI) error {
return errors.New("condition cannot be updated").
WithTag("name", c.name()).
WithTag("kind", c.Kind())
return errors.New("condition cannot be updated").WithTag("name", c.name())
}

func (c condition) onComponentEvent(any) {
Expand Down
24 changes: 7 additions & 17 deletions pkg/app/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ type htmlElement struct {
this UI
}

func (e *htmlElement) Kind() Kind {
return HTML
}

func (e *htmlElement) JSValue() Value {
return e.jsElement
}
Expand Down Expand Up @@ -79,9 +75,7 @@ func (e *htmlElement) getChildren() []UI {

func (e *htmlElement) mount(d Dispatcher) error {
if e.Mounted() {
return errors.New("html element is already mounted").
WithTag("tag", e.tag).
WithTag("kind", e.Kind())
return errors.New("html element is already mounted").WithTag("tag", e.tag)
}

e.context, e.contextCancel = context.WithCancel(context.Background())
Expand All @@ -90,7 +84,6 @@ func (e *htmlElement) mount(d Dispatcher) error {
jsElement, err := Window().createElement(e.tag, e.xmlns)
if err != nil {
return errors.New("mounting js element failed").
WithTag("kind", e.Kind()).
WithTag("tag", e.tag).
WithTag("xmlns", e.xmlns).
Wrap(err)
Expand All @@ -105,7 +98,6 @@ func (e *htmlElement) mount(d Dispatcher) error {
return errors.New("mounting child failed").
WithTag("index", i).
WithTag("child", c.name()).
WithTag("child-kind", c.Kind()).
Wrap(err)
}

Expand All @@ -129,9 +121,7 @@ func (e *htmlElement) dismount() {
}

func (e *htmlElement) canUpdateWith(v UI) bool {
return e.Mounted() &&
e.Kind() == v.Kind() &&
e.name() == v.name()
return e.Mounted() && e.name() == v.name()
}

func (e *htmlElement) updateWith(v UI) error {
Expand Down Expand Up @@ -223,12 +213,9 @@ func (e *htmlElement) replaceChildAt(idx int, new UI) error {
if err := mount(e.getDispatcher(), new); err != nil {
return errors.New("replacing child failed").
WithTag("name", e.name()).
WithTag("kind", e.Kind()).
WithTag("index", idx).
WithTag("old-name", old.name()).
WithTag("old-kind", old.Kind()).
WithTag("new-name", new.name()).
WithTag("new-kind", new.Kind()).
Wrap(err)
}

Expand Down Expand Up @@ -352,8 +339,11 @@ func (e *htmlElement) htmlWithIndent(w io.Writer, indent int) {
return
}

hasNewLineChildren := (len(e.children) == 1 && e.children[0].Kind() != SimpleText) ||
len(e.children) > 1
var hasNewLineChildren bool
if len(e.children) > 0 {
_, isText := e.children[0].(*text)
hasNewLineChildren = len(e.children) > 1 || !isText
}

for _, c := range e.children {
if hasNewLineChildren {
Expand Down
49 changes: 0 additions & 49 deletions pkg/app/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
// UI is the interface that describes a user interface element such as
// components and HTML elements.
type UI interface {
// Kind represents the specific kind of a UI element.
Kind() Kind

// JSValue returns the javascript value linked to the element.
JSValue() Value

Expand All @@ -38,52 +35,6 @@ type UI interface {
htmlWithIndent(w io.Writer, indent int)
}

// Kind represents the specific kind of a user interface element.
type Kind uint

func (k Kind) String() string {
switch k {
case SimpleText:
return "text"

case HTML:
return "html"

case Component:
return "component"

case Selector:
return "selector"

case RawHTML:
return "raw"

default:
return "undefined"
}
}

const (
// UndefinedElem represents an undefined UI element.
UndefinedElem Kind = iota

// SimpleText represents a simple text element.
SimpleText

// HTML represents an HTML element.
HTML

// Component represents a customized, independent and reusable UI element.
Component

// Selector represents an element that is used to select a subset of
// elements within a given list.
Selector

// RawHTML represents an HTML element obtained from a raw HTML code snippet.
RawHTML
)

// FilterUIElems processes and returns a filtered list of the provided UI
// elements.
//
Expand Down
46 changes: 8 additions & 38 deletions pkg/app/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestKindString(t *testing.T) {
utests := []struct {
kind Kind
expectedString string
}{
{
kind: UndefinedElem,
expectedString: "undefined",
},
{
kind: SimpleText,
expectedString: "text",
},
{
kind: HTML,
expectedString: "html",
},
{
kind: Component,
expectedString: "component",
},
{
kind: Selector,
expectedString: "selector",
},
}

for _, u := range utests {
t.Run(u.expectedString, func(t *testing.T) {
require.Equal(t, u.expectedString, u.kind.String())
})
}
}

func TestFilterUIElems(t *testing.T) {
t.Run("filter empty elements returns nil", func(t *testing.T) {
require.Nil(t, FilterUIElems())
Expand Down Expand Up @@ -174,8 +140,10 @@ func testMounted(t *testing.T, n UI) {
require.NotNil(t, n.getDispatcher())
require.True(t, n.Mounted())

switch n.Kind() {
case HTML, Component:
switch n.(type) {
case *text, *raw:

default:
require.NoError(t, n.getContext().Err())
require.NotNil(t, n.self())
}
Expand All @@ -190,8 +158,10 @@ func testDismounted(t *testing.T, n UI) {
require.NotNil(t, n.getDispatcher())
require.False(t, n.Mounted())

switch n.Kind() {
case HTML, Component:
switch n.(type) {
case *text, *raw:

default:
require.Error(t, n.getContext().Err())
require.Nil(t, n.self())
}
Expand Down
12 changes: 2 additions & 10 deletions pkg/app/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ func (r rangeLoop) Map(f func(string) UI) RangeLoop {
return r
}

func (r rangeLoop) Kind() Kind {
return Selector
}

func (r rangeLoop) JSValue() Value {
return nil
}
Expand Down Expand Up @@ -137,9 +133,7 @@ func (r rangeLoop) getChildren() []UI {
}

func (r rangeLoop) mount(Dispatcher) error {
return errors.New("range loop is not mountable").
WithTag("name", r.name()).
WithTag("kind", r.Kind())
return errors.New("range loop is not mountable").WithTag("name", r.name())
}

func (r rangeLoop) dismount() {
Expand All @@ -150,9 +144,7 @@ func (r rangeLoop) canUpdateWith(UI) bool {
}

func (r rangeLoop) updateWith(UI) error {
return errors.New("range loop cannot be updated").
WithTag("name", r.name()).
WithTag("kind", r.Kind())
return errors.New("range loop cannot be updated").WithTag("name", r.name())
}

func (r rangeLoop) onComponentEvent(any) {
Expand Down
8 changes: 1 addition & 7 deletions pkg/app/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ type raw struct {
value string
}

func (r *raw) Kind() Kind {
return RawHTML
}

func (r *raw) JSValue() Value {
return r.jsvalue
}
Expand Down Expand Up @@ -90,8 +86,7 @@ func (r *raw) mount(d Dispatcher) error {
if r.Mounted() {
return errors.New("mounting raw html element failed").
WithTag("reason", "already mounted").
WithTag("name", r.name()).
WithTag("kind", r.Kind())
WithTag("name", r.name())
}

r.disp = d
Expand All @@ -112,7 +107,6 @@ func (r *raw) mount(d Dispatcher) error {
return errors.New("mounting raw html element failed").
WithTag("reason", "converting raw html to html elements returned nil").
WithTag("name", r.name()).
WithTag("kind", r.Kind()).
WithTag("raw-html", r.value)
}
wrapper.removeChild(value)
Expand Down
Loading

0 comments on commit e793bf3

Please sign in to comment.