Documentation
¶
Overview ¶
package gfront is a package to build progressive web apps (PWA) with Go programming language and WebAssembly. It uses a declarative syntax that allows creating and dealing with HTML elements only by using Go, and without writing any HTML markup. The package also provides an http.Handler ready to serve all the required resources to run Go-based progressive web apps.
Index ¶
- Constants
- func Broadcast(s *State)
- func CopyBytesToGo(dst []byte, src Value) int
- func CopyBytesToJS(dst Value, src []byte) int
- func Encrypt(s *State)
- func GenerateStaticWebsite(dir string, h *Handler, pages ...string) error
- func Getenv(k string) string
- func Gopt_App_Main(index interface{})
- func HTMLString(ui UI) string
- func HTMLStringWithIndent(ui UI) string
- func Handle(actionName string, h ActionHandler)
- func KeepBodyClean() (close func())
- func Persist(s *State)
- func PrintHTML(w io.Writer, ui UI)
- func PrintHTMLWithIndent(w io.Writer, ui UI)
- func Route(path string, c Composer)
- func RouteWithRegexp(pattern string, c Composer)
- func RunWhenOnBrowser()
- func TestMatch(tree UI, d TestUIDescriptor) error
- func TestPath(p ...int) []int
- type Action
- type ActionHandler
- type AppInstaller
- type AppUpdater
- type BrowserStorage
- type BrowserWindow
- type ClientDispatcher
- type Compo
- type Composer
- type Condition
- type Context
- type Dismounter
- type Dispatch
- type DispatchMode
- type Dispatcher
- type Environment
- type Event
- type EventHandler
- type Func
- type HTMLA
- type HTMLAddress
- type HTMLArea
- type HTMLArticle
- type HTMLAside
- type HTMLAudio
- type HTMLB
- type HTMLBase
- type HTMLBdi
- type HTMLBdo
- type HTMLBlockquote
- type HTMLBody
- type HTMLBr
- type HTMLButton
- type HTMLCanvas
- type HTMLCaption
- type HTMLCite
- type HTMLCode
- type HTMLCol
- type HTMLColGroup
- type HTMLData
- type HTMLDataList
- type HTMLDd
- type HTMLDel
- type HTMLDetails
- type HTMLDfn
- type HTMLDialog
- type HTMLDiv
- type HTMLDl
- type HTMLDt
- type HTMLEm
- type HTMLEmbed
- type HTMLFieldSet
- type HTMLFigCaption
- type HTMLFigure
- type HTMLFooter
- type HTMLForm
- type HTMLH1
- type HTMLH2
- type HTMLH3
- type HTMLH4
- type HTMLH5
- type HTMLH6
- type HTMLHead
- type HTMLHeader
- type HTMLHr
- type HTMLHtml
- type HTMLI
- type HTMLIFrame
- type HTMLImg
- type HTMLInput
- type HTMLIns
- type HTMLKbd
- type HTMLLabel
- type HTMLLegend
- type HTMLLi
- type HTMLLink
- type HTMLMain
- type HTMLMap
- type HTMLMark
- type HTMLMeta
- type HTMLMeter
- type HTMLNav
- type HTMLNoScript
- type HTMLObject
- type HTMLOl
- type HTMLOptGroup
- type HTMLOption
- type HTMLOutput
- type HTMLP
- type HTMLParam
- type HTMLPicture
- type HTMLPre
- type HTMLProgress
- type HTMLQ
- type HTMLRp
- type HTMLRt
- type HTMLRuby
- type HTMLS
- type HTMLSamp
- type HTMLScript
- type HTMLSection
- type HTMLSelect
- type HTMLSmall
- type HTMLSource
- type HTMLSpan
- type HTMLStrong
- type HTMLStyle
- type HTMLSub
- type HTMLSummary
- type HTMLSup
- type HTMLTBody
- type HTMLTFoot
- type HTMLTHead
- type HTMLTable
- type HTMLTd
- type HTMLTemplate
- type HTMLTextarea
- type HTMLTh
- type HTMLTime
- type HTMLTitle
- type HTMLTr
- type HTMLU
- type HTMLUl
- type HTMLVar
- type HTMLVideo
- type HTMLWbr
- type Handler
- type Icon
- type Initializer
- type Kind
- type Mounter
- type MsgHandler
- type Navigator
- type Observer
- type Page
- type PreRenderCache
- type PreRenderedItem
- type PreRenderer
- type ProxyResource
- type RangeLoop
- type Resizer
- type ResourceProvider
- type ServerDispatcher
- type State
- type StateOption
- type Tag
- type Tagger
- type Tags
- type TestUIDescriptor
- type Type
- type UI
- type Updater
- type Value
- type Wrapper
Constants ¶
const ( // IsClient reports whether the code is running as a client in the // WebAssembly binary (app.wasm). IsClient = runtime.GOARCH == "wasm" && runtime.GOOS == "js" // IsServer reports whether the code is running on a server for // pre-rendering purposes. IsServer = runtime.GOARCH != "wasm" || runtime.GOOS != "js" )
const ( GopPackage = true Gop_game = "App" Gop_sprite = "Compo" )
Variables ¶
This section is empty.
Functions ¶
func Broadcast ¶
func Broadcast(s *State)
Broadcast is a state option that broadcasts a state to other browser tabs and windows from the same origin.
func CopyBytesToGo ¶
CopyBytesToGo copies bytes from the Uint8Array src to dst. It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
CopyBytesToGo panics if src is not an Uint8Array.
func CopyBytesToJS ¶
CopyBytesToJS copies bytes from src to the Uint8Array dst. It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
CopyBytesToJS panics if dst is not an Uint8Array.
func Encrypt ¶
func Encrypt(s *State)
Encrypt is a state option that encrypts a state before persisting it in local storage. Encryption is performed only when the Persist option is also set.
func GenerateStaticWebsite ¶
GenerateStaticWebsite generates the files to run a PWA built with go-app as a static website in the specified directory. Static websites can be used with hosts such as Github Pages.
Note that app.wasm must still be built separately and put into the web directory.
func Getenv ¶
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 Gopt_App_Main ¶
func Gopt_App_Main(index interface{})
func HTMLString ¶
HTMLString return an HTML string representation of the given UI element.
func HTMLStringWithIndent ¶
HTMLStringWithIndent return an indented HTML string representation of the given UI element.
func Handle ¶
func Handle(actionName string, h ActionHandler)
Handle registers the handler for the given action name. When an action occurs, the handler is executed on its own goroutine.
func KeepBodyClean ¶
func KeepBodyClean() (close func())
KeepBodyClean prevents third-party Javascript libraries to add nodes to the body element.
func Persist ¶
func Persist(s *State)
Persist is a state option that persists a state in local storage.
Be mindful to not use this option as a cache since local storage is limited to 5MB in a lot of web browsers.
func PrintHTMLWithIndent ¶
PrintHTMLWithIndent writes an idented HTML representation of the UI element into the given writer.
func Route ¶
Route associates the type of the given component to the given path.
When a page is requested and matches the route, a new instance of the given component is created before being displayed.
func RouteWithRegexp ¶
RouteWithRegexp associates the type of the given component to the given regular expression pattern.
Patterns use the Go standard regexp format.
When a page is requested and matches the pattern, a new instance of the given component is created before being displayed.
func RunWhenOnBrowser ¶
func RunWhenOnBrowser()
RunWhenOnBrowser starts the app, displaying the component associated with the current URL path.
This call is skipped when the program is not run on a web browser. This allows writing client and server-side code without separation or pre-compilation flags.
Eg:
func main() { // Define app routes. app.Route("/", myComponent{}) app.Route("/other-page", myOtherComponent{}) // Run the application when on a web browser (only executed on client side). app.RunWhenOnBrowser() // Launch the server that serves the app (only executed on server side): http.Handle("/", &app.Handler{Name: "My app"}) http.ListenAndServe(":8080", nil) }
func TestMatch ¶
func TestMatch(tree UI, d TestUIDescriptor) error
TestMatch looks for the element targeted by the descriptor in the given tree and reports whether it matches with the expected element.
Eg:
tree := app.Div().Body( app.H2().Body( app.Text("foo"), ), app.P().Body( app.Text("bar"), ), ) // Testing root: err := app.TestMatch(tree, app.TestUIDescriptor{ Path: TestPath(), Expected: app.Div(), }) // OK => err == nil // Testing h2: err := app.TestMatch(tree, app.TestUIDescriptor{ Path: TestPath(0), Expected: app.H3(), }) // KO => err != nil because we ask h2 to match with h3 // Testing text from p: err = app.TestMatch(tree, app.TestUIDescriptor{ Path: TestPath(1, 0), Expected: app.Text("bar"), }) // OK => err == nil
Types ¶
type Action ¶
Action represents a custom event that can be propagated across the app. It can contain a payload and be given additional context with tags.
type ActionHandler ¶
type ActionHandler = app.ActionHandler
ActionHandler represents a handler that is executed when an action is created with Context.NewAction().
type AppInstaller ¶
type AppInstaller = app.AppInstaller
AppInstaller is the interface that describes a component that is notified when the application installation state changes.
type AppUpdater ¶
type AppUpdater = app.AppUpdater
AppUpdater is the interface that describes a component that is notified when the application is updated.
type BrowserStorage ¶
type BrowserStorage = app.BrowserStorage
BrowserStorage is the interface that describes a web browser storage.
type BrowserWindow ¶
type BrowserWindow = app.BrowserWindow
BrowserWindow is the interface that describes the browser window.
type ClientDispatcher ¶
type ClientDispatcher = app.ClientDispatcher
ClientDispatcher is the interface that describes a dispatcher that emulates a client environment.
func NewClientTester ¶
func NewClientTester(n UI) ClientDispatcher
NewClientTester creates a testing dispatcher that simulates a client environment. The given UI element is mounted upon creation.
type Composer ¶
Composer is the interface that describes a customized, independent and reusable UI element.
Satisfying this interface is done by embedding app.Compo into a struct and implementing the Render function.
Example:
type Hello struct { app.Compo } func (c *Hello) Render() app.UI { return app.Text("hello") }
type Condition ¶
Condition represents a control structure that displays nodes depending on a given expression.
type Context ¶
Context is the interface that describes a context tied to a UI element.
A context provides mechanisms to deal with the browser, the current page, navigation, concurrency, and component communication.
It is canceled when its associated UI element is dismounted.
type Dismounter ¶
type Dismounter = app.Dismounter
Dismounter is the interface that describes a component that can perform additional actions when dismounted.
type DispatchMode ¶
type DispatchMode = app.DispatchMode
DispatchMode represents how a dispatch is processed.
const ( // A dispatch mode where the dispatched operation is enqueued to be executed // as soon as possible and its associated UI element is updated at the end // of the current update cycle. Update DispatchMode = iota // A dispatch mode that schedules the dispatched operation to be executed // after the current update frame. Defer // A dispatch mode where the dispatched operation is enqueued to be executed // as soon as possible. Next )
type Dispatcher ¶
type Dispatcher = app.Dispatcher
Dispatcher is the interface that describes an environment that synchronizes UI instructions and UI elements lifecycle.
type Environment ¶
type Environment = app.Environment
Environment describes the environment variables to pass to the progressive web app.
type EventHandler ¶
EventHandler represents a function that can handle HTML events. They are always called on the UI goroutine.
type Func ¶
Func is the interface that describes a wrapped Go function to be called by JavaScript.
func FuncOf ¶
FuncOf returns a wrapped function.
Invoking the JavaScript function will synchronously call the Go function fn with the value of JavaScript's "this" keyword and the arguments of the invocation. The return value of the invocation is the result of the Go function mapped back to JavaScript according to ValueOf.
A wrapped function triggered during a call from Go to JavaScript gets executed on the same goroutine. A wrapped function triggered by JavaScript's event loop gets executed on an extra goroutine. Blocking operations in the wrapped function will block the event loop. As a consequence, if one wrapped function blocks, other wrapped funcs will not be processed. A blocking function should therefore explicitly start a new goroutine.
Func.Release must be called to free up resources when the function will not be used any more.
type HTMLAddress ¶
type HTMLAddress = app.HTMLAddress
HTMLAddress is the interface that describes a <address> HTML element.
func Address ¶
func Address() HTMLAddress
Address returns an HTML element that defines contact information for the author/owner of a document.
type HTMLArticle ¶
type HTMLArticle = app.HTMLArticle
HTMLArticle is the interface that describes a <article> HTML element.
type HTMLBlockquote ¶
type HTMLBlockquote = app.HTMLBlockquote
HTMLBlockquote is the interface that describes a <blockquote> HTML element.
func Blockquote ¶
func Blockquote() HTMLBlockquote
Blockquote returns an HTML element that defines a section that is quoted from another source.
type HTMLButton ¶
type HTMLButton = app.HTMLButton
HTMLButton is the interface that describes a <button> HTML element.
func Button ¶
func Button() HTMLButton
Button returns an HTML element that defines a clickable button.
type HTMLCanvas ¶
type HTMLCanvas = app.HTMLCanvas
HTMLCanvas is the interface that describes a <canvas> HTML element.
func Canvas ¶
func Canvas() HTMLCanvas
Canvas returns an HTML element that is used to draw graphics on the fly.
type HTMLCaption ¶
type HTMLCaption = app.HTMLCaption
HTMLCaption is the interface that describes a <caption> HTML element.
func Caption ¶
func Caption() HTMLCaption
Caption returns an HTML element that defines a table caption.
type HTMLColGroup ¶
type HTMLColGroup = app.HTMLColGroup
HTMLColGroup is the interface that describes a <colgroup> HTML element.
func ColGroup ¶
func ColGroup() HTMLColGroup
ColGroup returns an HTML element that specifies a group of one or more columns in a table for formatting.
type HTMLDataList ¶
type HTMLDataList = app.HTMLDataList
HTMLDataList is the interface that describes a <datalist> HTML element.
func DataList ¶
func DataList() HTMLDataList
DataList returns an HTML element that specifies a list of pre-defined options for input controls.
type HTMLDetails ¶
type HTMLDetails = app.HTMLDetails
HTMLDetails is the interface that describes a <details> HTML element.
func Details ¶
func Details() HTMLDetails
Details returns an HTML element that defines additional details that the user can view or hide.
type HTMLDialog ¶
type HTMLDialog = app.HTMLDialog
HTMLDialog is the interface that describes a <dialog> HTML element.
func Dialog ¶
func Dialog() HTMLDialog
Dialog returns an HTML element that defines a dialog box or window.
type HTMLFieldSet ¶
type HTMLFieldSet = app.HTMLFieldSet
HTMLFieldSet is the interface that describes a <fieldset> HTML element.
func FieldSet ¶
func FieldSet() HTMLFieldSet
FieldSet returns an HTML element that groups related elements in a form.
type HTMLFigCaption ¶
type HTMLFigCaption = app.HTMLFigCaption
HTMLFigCaption is the interface that describes a <figcaption> HTML element.
func FigCaption ¶
func FigCaption() HTMLFigCaption
FigCaption returns an HTML element that defines a caption for a figure element.
type HTMLFigure ¶
type HTMLFigure = app.HTMLFigure
HTMLFigure is the interface that describes a <figure> HTML element.
func Figure ¶
func Figure() HTMLFigure
Figure returns an HTML element that specifies self-contained content.
type HTMLFooter ¶
type HTMLFooter = app.HTMLFooter
HTMLFooter is the interface that describes a <footer> HTML element.
func Footer ¶
func Footer() HTMLFooter
Footer returns an HTML element that defines a footer for a document or section.
type HTMLHeader ¶
type HTMLHeader = app.HTMLHeader
HTMLHeader is the interface that describes a <header> HTML element.
func Header ¶
func Header() HTMLHeader
Header returns an HTML element that defines a header for a document or section.
type HTMLIFrame ¶
type HTMLIFrame = app.HTMLIFrame
HTMLIFrame is the interface that describes a <iframe> HTML element.
type HTMLLegend ¶
type HTMLLegend = app.HTMLLegend
HTMLLegend is the interface that describes a <legend> HTML element.
func Legend ¶
func Legend() HTMLLegend
Legend returns an HTML element that defines a caption for a fieldset element.
type HTMLNoScript ¶
type HTMLNoScript = app.HTMLNoScript
HTMLNoScript is the interface that describes a <noscript> HTML element.
func NoScript ¶
func NoScript() HTMLNoScript
NoScript returns an HTML element that defines an alternate content for users that do not support client-side scripts.
type HTMLObject ¶
type HTMLObject = app.HTMLObject
HTMLObject is the interface that describes a <object> HTML element.
func Object ¶
func Object() HTMLObject
Object returns an HTML element that defines an embedded object.
type HTMLOptGroup ¶
type HTMLOptGroup = app.HTMLOptGroup
HTMLOptGroup is the interface that describes a <optgroup> HTML element.
func OptGroup ¶
func OptGroup() HTMLOptGroup
OptGroup returns an HTML element that defines a group of related options in a drop-down list.
type HTMLOption ¶
type HTMLOption = app.HTMLOption
HTMLOption is the interface that describes a <option> HTML element.
func Option ¶
func Option() HTMLOption
Option returns an HTML element that defines an option in a drop-down list.
type HTMLOutput ¶
type HTMLOutput = app.HTMLOutput
HTMLOutput is the interface that describes a <output> HTML element.
type HTMLPicture ¶
type HTMLPicture = app.HTMLPicture
HTMLPicture is the interface that describes a <picture> HTML element.
func Picture ¶
func Picture() HTMLPicture
Picture returns an HTML element that defines a container for multiple image resources.
type HTMLProgress ¶
type HTMLProgress = app.HTMLProgress
HTMLProgress is the interface that describes a <progress> HTML element.
func Progress ¶
func Progress() HTMLProgress
Progress returns an HTML element that represents the progress of a task.
type HTMLScript ¶
type HTMLScript = app.HTMLScript
HTMLScript is the interface that describes a <script> HTML element.
func Script ¶
func Script() HTMLScript
Script returns an HTML element that defines a client-side script.
type HTMLSection ¶
type HTMLSection = app.HTMLSection
HTMLSection is the interface that describes a <section> HTML element.
func Section ¶
func Section() HTMLSection
Section returns an HTML element that defines a section in a document.
type HTMLSelect ¶
type HTMLSelect = app.HTMLSelect
HTMLSelect is the interface that describes a <select> HTML element.
func Select ¶
func Select() HTMLSelect
Select returns an HTML element that defines a drop-down list.
type HTMLSource ¶
type HTMLSource = app.HTMLSource
HTMLSource is the interface that describes a <source> HTML element.
type HTMLStrong ¶
type HTMLStrong = app.HTMLStrong
HTMLStrong is the interface that describes a <strong> HTML element.
type HTMLSummary ¶
type HTMLSummary = app.HTMLSummary
HTMLSummary is the interface that describes a <summary> HTML element.
func Summary ¶
func Summary() HTMLSummary
Summary returns an HTML element that defines a visible heading for a details element.
type HTMLTemplate ¶
type HTMLTemplate = app.HTMLTemplate
HTMLTemplate is the interface that describes a <template> HTML element.
func Template ¶
func Template() HTMLTemplate
Template returns an HTML element that defines a template.
type HTMLTextarea ¶
type HTMLTextarea = app.HTMLTextarea
HTMLTextarea is the interface that describes a <textarea> HTML element.
func Textarea ¶
func Textarea() HTMLTextarea
Textarea returns an HTML element that defines a multiline input control (text area).
type Handler ¶
Handler is an HTTP handler that serves an HTML page that loads a Go wasm app and its resources.
type Icon ¶
Icon describes a square image that is used in various places such as application icon, favicon or loading icon.
type Initializer ¶
type Initializer = app.Initializer
Initializer is the interface that describes a component that performs initialization instruction before being pre-rendered or mounted.
type Kind ¶
Kind represents the specific kind of a user interface element.
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 )
type Mounter ¶
Mounter is the interface that describes a component that can perform additional actions when mounted.
type MsgHandler ¶
type MsgHandler = app.MsgHandler
MsgHandler represents a handler to listen to messages sent with Context.Post.
type Navigator ¶
Navigator is the interface that describes a component that can perform additional actions when navigated on.
type PreRenderCache ¶
type PreRenderCache = app.PreRenderCache
PreRenderCache is the interface that describes a cache that stores pre-rendered resources.
func NewPreRenderLRUCache ¶
func NewPreRenderLRUCache(size int, itemTTL time.Duration, onEvict ...func(path string, i PreRenderedItem)) PreRenderCache
NewPreRenderLRUCache creates an in memory LRU cache that stores items for the given duration. If provided, on eviction functions are called when item are evicted.
type PreRenderedItem ¶
type PreRenderedItem = app.PreRenderedItem
PreRenderedItem represent an item that is stored in a PreRenderCache.
type PreRenderer ¶
type PreRenderer = app.PreRenderer
PreRenderer is the interface that describes a component that performs instruction when it is server-side pre-rendered.
A pre-rendered component helps in achieving SEO friendly content.
type ProxyResource ¶
type ProxyResource = app.ProxyResource
ProxyResource is a proxy descriptor that maps a given resource to an URL path.
type RangeLoop ¶
RangeLoop represents a control structure that iterates within a slice, an array or a map.
type Resizer ¶
Resizer is the interface that describes a component that is notified when the app has been resized or a parent component calls the ResizeContent() method.
type ResourceProvider ¶
type ResourceProvider = app.ResourceProvider
ResourceProvider is the interface that describes a resource provider that tells the Handler how to locate and get the package and static resources.
Package resources are the resource required to operate go-app.
Static resources are resources such as app.wasm, CSS files, images.
The resource provider is used to serve static resources when it satisfies the http.Handler interface.
func CustomProvider ¶
func CustomProvider(path, prefix string) ResourceProvider
CustomProvider returns a resource provider that serves static resources from a local directory located at the given path and prefixes URL paths with the given prefix.
func GitHubPages ¶
func GitHubPages(repoName string) ResourceProvider
GitHubPages returns a resource provider that provides resources from GitHub pages. This provider must only be used to generate static websites with the GenerateStaticWebsite function.
func LocalDir ¶
func LocalDir(root string) ResourceProvider
LocalDir returns a resource provider that serves static resources from a local directory located at the given path.
func RemoteBucket ¶
func RemoteBucket(url string) ResourceProvider
RemoteBucket returns a resource provider that provides resources from a remote bucket such as Amazon S3 or Google Cloud Storage.
type ServerDispatcher ¶
type ServerDispatcher = app.ServerDispatcher
ServerDispatcher is the interface that describes a dispatcher that emulates a server environment.
func NewServerTester ¶
func NewServerTester(n UI) ServerDispatcher
NewServerTester creates a testing dispatcher that simulates a client environment.
type StateOption ¶
type StateOption = app.StateOption
StateOption represents an option applied when a state is set.
func ExpiresAt ¶
func ExpiresAt(t time.Time) StateOption
ExpiresAt returns a state option that sets a state value to its zero value at the given time.
Values persisted to local storage with the Persist option are removed from it.
func ExpiresIn ¶
func ExpiresIn(d time.Duration) StateOption
ExpiresIn returns a state option that sets a state value to its zero value after the given duration.
Values persisted to local storage with the Persist option are removed from it.
type Tagger ¶
Tagger is the interface that describes a collection of tags that gives context to something.
type TestUIDescriptor ¶
type TestUIDescriptor = app.TestUIDescriptor
TestUIDescriptor represents a descriptor that describes a UI element and its location from its parents.
type UI ¶
UI is the interface that describes a user interface element such as components and HTML elements.
func FilterUIElems ¶
FilterUIElems returns a filtered version of the given UI elements where selector elements such as If and Range are interpreted and removed. It also remove nil elements.
It should be used only when implementing components that can accept content with variadic arguments like HTML elements Body method.
type Updater ¶
Updater is the interface that describes a component that can do additional instructions when one of its exported fields is modified by its nearest parent component.
type Value ¶
Value is the interface that represents a JavaScript value. On wasm architecture, it wraps the Value from https://golang.org/pkg/syscall/js/ package.
func ValueOf ¶
func ValueOf(x interface{}) Value
ValueOf returns x as a JavaScript value:
| Go | JavaScript | | ---------------------- | ---------------------- | | js.Value | [its value] | | js.Func | function | | nil | null | | bool | boolean | | integers and floats | number | | string | string | | []interface{} | new array | | map[string]interface{} | new object |
Panics if x is not one of the expected types.