Documentation
¶
Index ¶
- Constants
- func After(task tasker.Task) error
- func Canvas() *jse.Element
- func Client() *craterhttp.Client
- func Dequeue(task string) error
- func Enqueue(task tasker.Task) error
- func ErrorMessage(d time.Duration, s ...any)
- func Exit(err error)
- func GetGlobal[T any](key string) (ret T, ok bool)
- func GlobalExists(name string) bool
- func GlobalJS() js.Value
- func GlobalJSName(name string)
- func HandleEndpoint(path string, r craterhttp.RequestFunc, h PageFunc)
- func HandlePath(path string)
- func HideLoader()
- func InfoMessage(d time.Duration, s ...any)
- func LogDebug(s ...any)
- func LogDebugf(format string, v ...interface{})
- func LogError(s ...any)
- func LogErrorf(format string, v ...interface{})
- func LogInfo(s ...any)
- func LogInfof(format string, v ...interface{})
- func Mux() *mux.Mux
- func New(c *Config)
- func OpenSock(url string, options *SockOpts)
- func Redirect(path string)
- func RegisterHook(name string, hook func(any) error)
- func Run() error
- func SendHook(name string, v any) error
- func SetGlobal(key string, value interface{}, setGLobal bool)
- func SetGlobalFunc(name string, f func(args ...interface{}) Marshaller) (js.Func, error)
- func SetLogLevel(level logger.LogLevel)
- func SetTemplate(name string, f func(args ...interface{}) Marshaller)
- func ShowLoader()
- func Socket() *websocket.WebSocket
- func SuccessMessage(d time.Duration, s ...any)
- func WarningMessage(d time.Duration, s ...any)
- func WithEmbed(f func(pageCtx context.Context, page *jse.Element) *jse.Element)
- func WithFlags(flags CraterFlags)
- func WithLoader(l Loader)
- func WithLogger(l Logger)
- func WithMessenger(m Messenger)
- func WithNotFoundHandler(h PageFunc)
- func WithOnResponseError(f func(error))
- func WithoutTemplate(name string)
- type Config
- type CraterFlags
- type Decoder
- type FullPage
- type Initter
- type Loader
- type Logger
- type Marshaller
- type Messenger
- type NullMarshaller
- type OnClickFunc
- type Page
- type PageFunc
- type Preloader
- type Route
- type SockConfigurator
- type SockOpts
- type Templater
Constants ¶
const ( // SignalRun is sent when the application starts. SignalRun = "crater.Run" // SignalExit is sent when the application exits. // // The value sent is the error. SignalExit = "crater.Exit" // SignalPageChange is sent when a page is changed. SignalPageChange = "crater.PageChange" // SignalPageRendered is sent when a page is rendered. // // The value sent is the page. SignalPageRendered = "crater.PageRendered" // SignalSockConnected is sent when a websocket is connected. // // The value sent is the websocket. SignalSockConnected = "crater.SockConnected" // SignalClientResponse is sent when the client receives a response. // // The value sent is the client. SignalClientResponse = "crater.ClientResponse" // SignalHandlerAdded is sent when a handler is added. // // The value sent is the handler. SignalHandlerAdded = "crater.HandlerAdded" )
If the value returned is unspecified, it is safe to assume the value to be nil.
Variables ¶
This section is empty.
Functions ¶
func After ¶ added in v1.6.2
Execute a task after the duration has passed, or immediately if the duration is 0. If the task name is provided, the task will be reset to the new duration. If the task name is not provided, the task will be executed once after the duration has passed.
func Client ¶ added in v1.8.2
func Client() *craterhttp.Client
Client returns the application's http client.
func Dequeue ¶ added in v1.6.2
Dequeue a task by name. If there is an error, it will be of types: - ErrNoNameSpecified - ErrNotFound
func Enqueue ¶ added in v1.6.2
Enqueue a task periodically by name. If there is an error, it will be of type: - ErrNoNameSpecified
func ErrorMessage ¶
An error message to be shown to the user.
This function will spin up a goroutine to log the message.
func GetGlobal ¶ added in v1.7.9
Get global data for the application.
This function will look in the javascript global scope for the data if it is not found in the application's data.
This means it must supported by jsext.ToGo()
If T implements jsext.Unmarshaller, it will be used to unmarshal the javascript value.
func GlobalExists ¶ added in v1.8.4
Check if a global value or function exists.
func GlobalJSName ¶ added in v1.7.7
func GlobalJSName(name string)
func HandleEndpoint ¶
func HandleEndpoint(path string, r craterhttp.RequestFunc, h PageFunc)
Handle a path with a page function.
The page passed to this function will have acess to page.DecodeResponse and page.Response fields.
The page function will be called when the path is visited.
func InfoMessage ¶
An info message to be shown to the user.
This function will spin up a goroutine to log the message.
func LogDebugf ¶
func LogDebugf(format string, v ...interface{})
Log a debug message in Sprintf format.
This function will spin up a goroutine to log the message.
func LogErrorf ¶
func LogErrorf(format string, v ...interface{})
Log an error in Sprintf format.
This function will spin up a goroutine to log the message.
func LogInfof ¶
func LogInfof(format string, v ...interface{})
Log an info message in Sprintf format.
This function will spin up a goroutine to log the message.
func New ¶
func New(c *Config)
Initialize a new application
The config parameter is optional, if nil, the default config will be used
func RegisterHook ¶ added in v1.8.4
RegisterHook registers a hook with the application.
func Run ¶
func Run() error
Run the application.
This function will block until the application exits.
func SetGlobal ¶ added in v1.7.9
Set global data for the application, and javascript global scope If specified, but this means it must supported by jsext.ValueOf()
func SetGlobalFunc ¶ added in v1.7.9
func SetGlobalFunc(name string, f func(args ...interface{}) Marshaller) (js.Func, error)
Add a global function to the application.
This function will be available to all pages, and in the global javascript scope.
Arguments (if any) are limited to the types supported by jsext.ToGo()
func SetTemplate ¶
func SetTemplate(name string, f func(args ...interface{}) Marshaller)
SetTemplate sets the application's template.
func SuccessMessage ¶
A success message to be shown to the user.
This function will spin up a goroutine to log the message.
func WarningMessage ¶
A warning message to be shown to the user.
This function will spin up a goroutine to log the message.
func WithEmbed ¶
WithEmbed sets the application's embed function.
This can be used to embed the page element, useful for navbars, footers etc.
func WithMessenger ¶
func WithMessenger(m Messenger)
WithMessenger sets the application's messenger.
func WithNotFoundHandler ¶
func WithNotFoundHandler(h PageFunc)
WithNotFoundHandler sets the application's not found handler.
func WithOnResponseError ¶
func WithOnResponseError(f func(error))
WithOnResponseError sets the application's OnResponseError function.
func WithoutTemplate ¶
func WithoutTemplate(name string)
WithoutTemplate removes a template from the application.
Types ¶
type Config ¶
type Config struct { // The function which will be called when a page is not found NotFoundHandler PageFunc `jsc:"-"` // The function which will be called when an error occurs in HandleEndpoint() OnResponseError func(error) `jsc:"-"` // The application's loader. Loader Loader `jsc:"-"` // The application's logger. Logger Logger `jsc:"-"` // The application's messenger. // // This will display messages to the user. Messenger Messenger `jsc:"-"` // The root element of the application. // // This will be passed to the Page struct, and will be used to render the page. RootElement jsext.Element `jsc:"-"` // Optional flags to change the behavior of the application. Flags CraterFlags `jsc:"-"` // The initial page URL. InitialPageURL string `jsc:"-"` // HttpClientTimeout is the timeout for the http client. HttpClientTimeout time.Duration `jsc:"-"` // Allows you to embed the canvas written to inside of another canvas. // // Useful for navbars, footers etc. // // The page should be embedded into the element returned by this function. EmbedFunc func(ctx context.Context, page *jse.Element) *jse.Element `jsc:"-"` // Templates which can be set, these can be used globally in the application. // // The arguments passed to the function are the arguments passed to the template. Templates map[string]func(args ...interface{}) Marshaller `jsc:"-"` }
type CraterFlags ¶
type CraterFlags uint32
const ( // Change the page on each click of a link // // If not set, the page will only change when the URL changes. F_CHANGE_PAGE_EACH_CLICK CraterFlags = 1 << iota // Log each message sent with crater.InfoMessage(), crater.ErrorMessage() etc. F_LOG_EACH_MESSAGE // Close all websocket connections after switching pages // // If not set, websocket connections will be kept open // and must be closed manually, or they will be reused for the handler it was set on. F_CLOSE_SOCKS_EACH_PAGE // Append the canvas to the application's element instead of replacing it F_APPEND_CANVAS )
type FullPage ¶ added in v1.4.7
type FullPage interface { PageFunc Preloader Initter Templater SockConfigurator }
type Loader ¶
type Loader interface { Show() Hide() }
A loader which will display when a page is loading
type Marshaller ¶ added in v1.4.1
func ExecGlobalFunc ¶ added in v1.7.7
func ExecGlobalFunc(name string, args ...interface{}) Marshaller
Call a global function.
This function will panic if the function does not exist.
Arguments (if any) are limited to the types supported by jsext.ValueOf() if it is bound to js.Global alone, otherwise it is limited to the types supported by jsext.ToGo()
func WithTemplate ¶
func WithTemplate(name string, args ...interface{}) Marshaller
WithTemplate adds a template to the application.
This function will panic if the template does not exist.
The arguments passed to this function will be passed to the template function.
type NullMarshaller ¶ added in v1.7.3
type NullMarshaller struct{}
func (NullMarshaller) MarshalJS ¶ added in v1.7.3
func (NullMarshaller) MarshalJS() js.Value
type OnClickFunc ¶
type OnClickFunc messenger.OnClickFunc
func (OnClickFunc) OnClick ¶
func (f OnClickFunc) OnClick()
type Page ¶
type Page struct { // The root element of the page // // This is the element which will act as a canvas for the page Canvas *jse.Element `jsc:"root"` // The response received from the server *craterhttp.Response `jsc:"-"` // The variables received from the server Variables mux.Variables `jsc:"variables"` // The context of the page // // This will be reset for each page render. Context context.Context `jsc:"-"` // A function which can be arbitrarily set, and will be called after the page is rendered. AfterRender func(p *Page) `jsc:"-"` // State is an object where we can more easily keep track of and store state. // // This is useful for keeping track of things like whether or not a page is loading. State *state.State `jsc:"-"` // Sock is a websocket connection to the server for the current page. Sock *websocket.WebSocket `jsc:"-"` }
Page represents a page in the application.
func (*Page) AppendChild ¶ added in v1.4.3
type PageFunc ¶
type PageFunc interface {
Serve(p *Page)
}
func ToPageFunc ¶ added in v1.4.1
Create a new page function from a which will be called when the page is rendered.
type SockConfigurator ¶ added in v1.4.3
type SockOpts ¶
type Templater ¶ added in v1.4.3
type Templater interface {
Templates() map[string]func(args ...interface{}) Marshaller
}