Skip to content

Commit

Permalink
Add missing GoDoc
Browse files Browse the repository at this point in the history
And some other GoLint fixes.
  • Loading branch information
bep committed May 10, 2016
1 parent 106e211 commit 7da0240
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 18 deletions.
4 changes: 2 additions & 2 deletions attr/generate.go
@@ -1,3 +1,5 @@
// +build ignore

/*
Copyright 2016 Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> All rights reserved.
Expand All @@ -14,8 +16,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// +build ignore

package main

import (
Expand Down
4 changes: 2 additions & 2 deletions el/generate.go
@@ -1,3 +1,5 @@
// +build ignore

/*
Copyright 2016 Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> All rights reserved.
Expand All @@ -14,8 +16,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// +build ignore

// Portions Copyright (c) 2016 The Vecty Authors. All rights reserved.
// See https://github.com/gopherjs/vecty for the origin of this clever
// code generator.
Expand Down
9 changes: 8 additions & 1 deletion element.go
Expand Up @@ -43,6 +43,8 @@ var (
}
)

// Element represents a builder for a ReactElement.
// An Element can be a simple text node or a HTML element with children, attributes etc.
type Element struct {
tag string
properties map[string]interface{}
Expand All @@ -62,26 +64,31 @@ type Element struct {
*This
}

// NewElement creates a new Element with the given tag.
func NewElement(tag string) *Element {
return &Element{tag: tag, properties: Props{}, elFactory: defaultElementFactory}
}

// Create an Element from a ready-to-use React element.
// NewPreparedElement creates an Element from a ready-to-use React element.
func NewPreparedElement(o *js.Object) *Element {
return &Element{element: o, elFactory: returnStoredElement}
}

// Node returns the resulting ReactElement.
func (e *Element) Node() *js.Object {
e.element = e.elFactory(e)
return e.element
}

// Modify implements the Modifier interface.
func (e *Element) Modify(in *Element) {
in.children = append(in.children, e)
}

// Modifiers is used to Modify a list of elements (children).
type Modifiers []Modifier

// Modify implements the Modifier interface.
func (mods Modifiers) Modify(e *Element) {
for _, m := range mods {
if m != nil {
Expand Down
4 changes: 2 additions & 2 deletions evt/generate.go
@@ -1,3 +1,5 @@
// +build ignore

/*
Copyright 2016 Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> All rights reserved.
Expand All @@ -14,8 +16,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// +build ignore

// Portions Copyright (c) 2016 The Vecty Authors. All rights reserved.
// See https://github.com/gopherjs/vecty for the origin of this clever
// code generator.
Expand Down
2 changes: 1 addition & 1 deletion examples/ajax/main.go
Expand Up @@ -24,7 +24,7 @@ func main() {
type gist struct {
URL string `json:"url"`
ID string `json:"id"`
HtmlUrl string `json:"html_url"`
URL string `json:"html_url"`
CreatedAt string `json:"created_at"`
Description string `json:"description"`
}
Expand Down
2 changes: 1 addition & 1 deletion examples/basic-click-counter/main.go
Expand Up @@ -46,7 +46,7 @@ func (c clickCounter) onClick(this *gr.This, event *gr.Event) {
}

// Implements the ShouldComponentUpdate interface.
func (e clickCounter) ShouldComponentUpdate(
func (c clickCounter) ShouldComponentUpdate(
this *gr.This, nextProps gr.Props, nextState gr.State) bool {

return this.State().HasChanged(nextState, "counter")
Expand Down
12 changes: 7 additions & 5 deletions examples/helpers.go
Expand Up @@ -39,8 +39,8 @@ func Example(title string, mods ...gr.Modifier) *gr.Element {

func exampleListItem(title, href, text string) gr.Modifier {
var (
itemStatus gr.Modifier = gr.Discard
loc = gr.Location()
itemStatus = gr.Discard
loc = gr.Location()
)

if !strings.HasSuffix(href, "/") {
Expand Down Expand Up @@ -68,16 +68,16 @@ func Alert(classifier string, body gr.Modifier) *gr.Element {
return e
}

// Some reusable components to use in composition examples.
// ClickCounter is a reusable components to use in composition examples.
// This is just copy-paste from the click counter example. Consider making something else.
type ClickCounter int

// Implements the StateInitializer interface.
// GetInitialState implements the StateInitializer interface.
func (c ClickCounter) GetInitialState(this *gr.This) gr.State {
return gr.State{"counter": 0}
}

// Implements the Renderer interface.
// Render implements the Renderer interface.
func (c ClickCounter) Render(this *gr.This) gr.Component {
counter := this.State()["counter"]
message := fmt.Sprintf(" Click me! Number of clicks: %v", counter)
Expand All @@ -93,12 +93,14 @@ func (c ClickCounter) onClick(this *gr.This, event *gr.Event) {
this.SetState(gr.State{"counter": this.State().Int("counter") + 1})
}

// ShouldComponentUpdate implements the ShouldComponentUpdate interface.
func (c ClickCounter) ShouldComponentUpdate(
this *gr.This, nextProps gr.Props, nextState gr.State) bool {

return this.State().HasChanged(nextState, "counter")
}

// ComponentDidMount implements the ComponentDidMount interface.
func (c ClickCounter) ComponentDidMount(this *gr.This) {
println("ClickCounter: ComponentDidMount")
}
2 changes: 1 addition & 1 deletion examples/router/main.go
Expand Up @@ -95,7 +95,7 @@ func (c clickCounter) onClick(this *gr.This, event *gr.Event) {
}

// Implements the ShouldComponentUpdate interface.
func (e clickCounter) ShouldComponentUpdate(
func (c clickCounter) ShouldComponentUpdate(
this *gr.This, nextProps gr.Props, nextState gr.State) bool {

return this.State().HasChanged(nextState, "counter")
Expand Down
2 changes: 2 additions & 0 deletions helpers.go
Expand Up @@ -35,12 +35,14 @@ import (
}
}*/

// UnmountComponentAtNode unmounts the DOM element at the given ID.
func UnmountComponentAtNode(elementID string) bool {
// TODO(bep) maybe incorporate this DOM element into the component
container := js.Global.Get("document").Call("getElementById", elementID)
return reactDOM.Call("unmountComponentAtNode", container).Bool()
}

// HostInfo represents the location info from the browser window.
type HostInfo struct {
Path string
Port int
Expand Down
2 changes: 2 additions & 0 deletions render.go
Expand Up @@ -26,6 +26,8 @@ const (
defaultWaitTime = 1000 / defaultFramesPerSecond
)

// RenderLoop runs the given render func in a loop at the given interval.
// It can be stopped by closing the returned channel.
func RenderLoop(render func(), interval ...time.Duration) chan struct{} {

renderInterval := defaultWaitTime * time.Millisecond
Expand Down
26 changes: 23 additions & 3 deletions tests/grt/test_helpers.go
@@ -1,3 +1,6 @@
// Package grt contains utilities used to test React components.
// TODO(bep)
// Move this to its own repo maybe when it is more mature.
package grt

import (
Expand All @@ -10,13 +13,15 @@ import (
"github.com/gopherjs/gopherjs/js"
)

// Equal is a test assertion used to check equality.
func Equal(t *testing.T, expected, actual interface{}) {
if expected != actual {
// TODO(bep) figure a way to get caller info
t.Errorf("Assert mismatch:\n%v\n%v", expected, actual)
}
}

// NotNil is a test assertion that checks for both nil values and js.Undefined.
func NotNil(t *testing.T, val interface{}) {
if isNil(val) {
Fail(t, fmt.Sprintf("Got <nil> for %T", val))
Expand All @@ -26,10 +31,12 @@ func NotNil(t *testing.T, val interface{}) {
}
}

// Fail fails the test with the given message.
func Fail(t *testing.T, args ...interface{}) {
t.Fatal(args)
}

// ShallowRenderWithContext performs a shallow render with the given context.
func ShallowRenderWithContext(c gr.Component, ctx gr.Context) *RenderedTree {
if _, ok := c.(gr.Factory); ok {
panic("Cannot render factories, create an Element first")
Expand All @@ -38,21 +45,23 @@ func ShallowRenderWithContext(c gr.Component, ctx gr.Context) *RenderedTree {
return &RenderedTree{Object: tree, context: ctx}
}

// ShallowRender performs a shallow render of the given component.
func ShallowRender(c gr.Component) *RenderedTree {
return ShallowRenderWithContext(c, gr.Context{})
}

// ReRender rerenders a component with new properties.
func (t *RenderedTree) ReRender(props gr.Props) {
t.reRender(props, t.context)
}

// Dive can be used to render a sub-component.
func (t *RenderedTree) Dive(path ...string) *RenderedTree {
tree := t.dive(path, t.context)
return &RenderedTree{Object: tree, context: t.context}
}

// TODO(bep)
// Move this to its own repo maybe when it is more mature.
// RenderedTree represents a shallow render.
type RenderedTree struct {
*js.Object
reRender func(gr.Props, gr.Context) `js:"reRender"`
Expand All @@ -71,27 +80,37 @@ type RenderedTree struct {
context gr.Context
}

// Props represents the properties set on a React element.
// This is the same as gr.Props, but reimplemented here so we can add
// test methods to it.
type Props map[string]interface{}

// Matcher used to find a component in the component tree.
type Matcher struct {
key, value string
}

// CallEventListener is a convenience func to simulate button clicks etc.
// by calling the listener methods by name.
func (p Props) CallEventListener(name string, args ...interface{}) *js.Object {
return p[name].(func(...interface{}) *js.Object)(name, args)
}

// NewMatcher creates a new matcher.
func NewMatcher(key, value string) Matcher {
return Matcher{key: key, value: value}
}

var renderStringReplacers = strings.NewReplacer(" ", "", "\n", "")

// String represents a shallow rendered component.
// This is in heavy use in assertions, so we try to keep it stable.
func (t *RenderedTree) String() string {
s := t.toString()
return renderStringReplacers.Replace(s)
}

// Sub returns a sub component matching the matchers.
func (t *RenderedTree) Sub(selector string, matchers ...Matcher) *RenderedTree {

m := make(map[string]interface{})
Expand All @@ -100,7 +119,7 @@ func (t *RenderedTree) Sub(selector string, matchers ...Matcher) *RenderedTree {
m[matcher.key] = matcher.value
}

var args []interface{} = []interface{}{selector}
var args = []interface{}{selector}

if len(m) > 0 {
args = append(args, m)
Expand All @@ -115,6 +134,7 @@ func (t *RenderedTree) Sub(selector string, matchers ...Matcher) *RenderedTree {
return &RenderedTree{Object: subTree}
}

// This returns the this context of a shallow render.
func (t *RenderedTree) This() *gr.This {
return gr.NewThis(t.GetMountedInstance())
}
Expand Down
16 changes: 16 additions & 0 deletions this.go
Expand Up @@ -22,10 +22,14 @@ import (
"github.com/gopherjs/gopherjs/js"
)

// This is named for what it represents: The this context representation from the
// JavaScript side of the fence.
type This struct {
this *js.Object
}

// Props returns the properties set; what you would expect to find in
// this.properties in React.
func (t *This) Props() Props {

// TODO(bep) cache?
Expand All @@ -50,6 +54,8 @@ func (t *This) Props() Props {
return props
}

// Context returns the context set; what you would expect to find in
// this.context in React.
func (t *This) Context() Context {
c := t.this.Get("context")

Expand All @@ -70,6 +76,8 @@ func (t *This) Component(name string) Modifier {
return Discard
}

// State returns the state; what you would expect to find in
// this.properties in React.
func (t *This) State() State {
state := t.this.Get("state").Interface()
if state == nil {
Expand All @@ -78,23 +86,31 @@ func (t *This) State() State {
return state.(map[string]interface{})
}

// Int is concenience method to lookup a int value from state.
func (s State) Int(key string) int {
if val, ok := s[key]; ok {
return int(val.(float64))
}
return 0
}

// SetState is a way of setting the state.
func (t *This) SetState(s State) {
t.this.Call("setState", s)
}

// NewThis creates a new This based on a JavaScript object representation.
func NewThis(that *js.Object) *This {
return &This{this: that}
}

// Context holds the React context.
type Context map[string]interface{}

// Props holds the React properties.
type Props map[string]interface{}

// State holds the React state.
type State map[string]interface{}

// HasChanged reports whether the value of the property with any of the given keys has changed.
Expand Down

0 comments on commit 7da0240

Please sign in to comment.