Skip to content

Commit

Permalink
move global variables in a single ___location (#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Mar 4, 2024
1 parent 7748fa7 commit cc31cf2
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 76 deletions.
52 changes: 52 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"context"
"fmt"
"os"
"reflect"
"runtime"
)

Expand All @@ -27,6 +28,11 @@ const (
IsServer = runtime.GOARCH != "wasm" || runtime.GOOS != "js"
)

var (
routes = makeRouter()
window = newBrowserWindow()
)

// Getenv retrieves the value of the environment variable named by the key. It
// returns the value, which will be empty if the variable is not present.
func Getenv(k string) string {
Expand Down Expand Up @@ -114,3 +120,49 @@ func displayLoadError(err any) {
}
loadingLabel.setInnerText(fmt.Sprint(err))
}

// Route associates a given path with a function that generates a new Composer
// component. When a user navigates to the specified path, the function
// newComponent is invoked to create and mount the associated component.
//
// Example:
//
// Route("/home", func() Composer {
// return NewHomeComponent()
// })
func Route(path string, newComponent func() Composer) {
routes.route(path, newComponent)
}

// RouteWithRegexp associates a URL path pattern with a function that generates
// a new Composer component. When a user navigates to a URL path that matches
// the given regular expression pattern, the function newComponent is invoked to
// create and mount the associated component.
//
// Example:
//
// RouteWithRegexp("^/users/[0-9]+$", func() Composer {
// return NewUserComponent()
// })
func RouteWithRegexp(pattern string, newComponent func() Composer) {
routes.routeWithRegexp(pattern, newComponent)
}

// NewZeroComponentFactory returns a function that, when invoked, creates and
// returns a new instance of the same type as the provided component. The new
// instance is initialized with zero values for all its fields.
//
// The function uses reflection to determine the type of the provided Composer
// and to create new instances of that type.
//
// Example:
//
// componentFunc := NewZeroComponentFactory(MyComponent{})
// newComponent := componentFunc()
func NewZeroComponentFactory(c Composer) func() Composer {
componentType := reflect.TypeOf(c)

return func() Composer {
return reflect.New(componentType.Elem()).Interface().(Composer)
}
}
15 changes: 7 additions & 8 deletions pkg/app/js_nowasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ import (
"github.com/maxence-charriere/go-app/v9/pkg/errors"
)

var (
window = &browserWindow{}
errNoWasm = errors.New("unsupported instruction").
WithTag("required-architecture", "wasm").
WithTag("current-architecture", runtime.GOARCH)
)

type value struct{}

func (v value) Bool() bool {
Expand Down Expand Up @@ -97,7 +90,9 @@ func (v value) Truthy() bool {
}

func (v value) Type() Type {
panic(errNoWasm)
panic(errors.New("unsupported instruction").
WithTag("required-architecture", "wasm").
WithTag("current-architecture", runtime.GOARCH))
}

func (v value) Then(f func(Value)) {
Expand Down Expand Up @@ -172,6 +167,10 @@ type browserWindow struct {
value
}

func newBrowserWindow() *browserWindow {
return &browserWindow{}
}

func (w browserWindow) URL() *url.URL {
return &url.URL{}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/js_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"github.com/maxence-charriere/go-app/v9/pkg/errors"
)

var (
window = &browserWindow{value: value{jsValue: js.Global()}}
)

type value struct {
jsValue
}
Expand Down Expand Up @@ -252,6 +248,10 @@ type browserWindow struct {
cursorY int
}

func newBrowserWindow() *browserWindow {
return &browserWindow{value: value{jsValue: js.Global()}}
}

func (w *browserWindow) URL() *url.URL {
rawurl := w.
Get("___location").
Expand Down
51 changes: 0 additions & 51 deletions pkg/app/route.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,10 @@
package app

import (
"reflect"
"regexp"
"sync"
)

var (
routes = makeRouter()
)

// Route associates a given path with a function that generates a new Composer
// component. When a user navigates to the specified path, the function
// newComponent is invoked to create and mount the associated component.
//
// Example:
//
// Route("/home", func() Composer {
// return NewHomeComponent()
// })
func Route(path string, newComponent func() Composer) {
routes.route(path, newComponent)
}

// RouteWithRegexp associates a URL path pattern with a function that generates
// a new Composer component. When a user navigates to a URL path that matches
// the given regular expression pattern, the function newComponent is invoked to
// create and mount the associated component.
//
// Example:
//
// RouteWithRegexp("^/users/[0-9]+$", func() Composer {
// return NewUserComponent()
// })
func RouteWithRegexp(pattern string, newComponent func() Composer) {
routes.routeWithRegexp(pattern, newComponent)
}

// NewZeroComponentFactory returns a function that, when invoked, creates and
// returns a new instance of the same type as the provided component. The new
// instance is initialized with zero values for all its fields.
//
// The function uses reflection to determine the type of the provided Composer
// and to create new instances of that type.
//
// Example:
//
// componentFunc := NewZeroComponentFactory(MyComponent{})
// newComponent := componentFunc()
func NewZeroComponentFactory(c Composer) func() Composer {
componentType := reflect.TypeOf(c)

return func() Composer {
return reflect.New(componentType.Elem()).Interface().(Composer)
}
}

type router struct {
mu sync.RWMutex
routes map[string]func() Composer
Expand Down
4 changes: 4 additions & 0 deletions pkg/app/scripts_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ package app
const (
// The default template used to generate app-worker.js.
DefaultAppWorkerJS = ""

appJS = ""
manifestJSON = ""
appCSS = ""
)
15 changes: 2 additions & 13 deletions pkg/app/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,9 @@ import (
"github.com/maxence-charriere/go-app/v9/pkg/errors"
)

const (
appJS = ""
appWorkerJS = ""
manifestJSON = ""
appCSS = ""
)

var (
errBadInstruction = errors.New("unsupported instruction").
WithTag("architecture", runtime.GOARCH)
)

func GenerateStaticWebsite(dir string, h *Handler, pages ...string) error {
panic(errBadInstruction)
panic(errors.New("unsupported instruction").
WithTag("architecture", runtime.GOARCH))
}

func wasmExecJS() string {
Expand Down

0 comments on commit cc31cf2

Please sign in to comment.