Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTransform = fmt.Errorf("cannot transform signals")
ErrTransform is error in case we coudn't transform signals into a nested object. Basically indicates if two fields are conflicting.
Example: signal {"user.fullname.name": "user name"} is used along with signal {"user.fullname" = "users full name"}
Functions ¶
This section is empty.
Types ¶
type CtxFragment ¶
CtxFragment is a ctx aware fragment renderer. Examples are: templ.
type Datastar ¶
type Datastar struct {
// Sets sse retry field
// See https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#retry
SSERetry time.Duration
// contains filtered or unexported fields
}
Datastar is the main engine to handle datastart requests. It allows you to parse incoming signals or send events to client.
func New ¶
func New(w http.ResponseWriter, r *http.Request) (ds *Datastar, release func())
New creates a new Datastar instance. It uses fastjson.Parser to parse incoming signals.
You should `defer release()` to reuse these parsers. If you don't - nothing will leak, but parsing signals will be less optimised.
func (*Datastar) Send ¶
Send sends a datastar event to client. Events are created individually with respective functions or structs. Events are buffered, with reusable buffer pool.
func (*Datastar) UnmarshalSignals ¶
UnmarshalSignals unmarshals a signal (or multiple) into a provided value. It uses json.Unmarshal to do it, so regular Unmarshal rules apply. If path is provided it will find signal value at that path.
Path can be separated by "." or be made of individual components, like: "my.data.value" or ("my", "data", "value").
type Event ¶
type Event interface {
Name() string
WriteEvent(writer *sseserver.EventWriter, req *http.Request) error
}
Event is a datastar event that will be sent to the client. Current implementations: EventMergeFragments, EventMergeSignals, EventRemoveFragments, EventRemoveSignals, EventExecuteScript. Refer to individual events for details.
type EventExecuteScript ¶
type EventExecuteScript struct {
// Script to send to the client.
// It can have multiple lines that will be split by newline at event creation.
Script []string
// AutoRemove determines whether to remove the script after execution.
AutoRemove bool
// Attributes to add to the script element.
Attributes map[string]string
}
EventExecuteScript is the implementation for `datastar-execute-script` event. Event options can be set with respective fields.
func ExecuteScript ¶
func ExecuteScript(scrpit ...string) EventExecuteScript
ExecuteScript is a shortcut to create a `datastar-execute-script` event.
func (EventExecuteScript) Name ¶
func (event EventExecuteScript) Name() string
func (EventExecuteScript) WriteEvent ¶
func (event EventExecuteScript) WriteEvent(writer *sseserver.EventWriter, req *http.Request) error
type EventMergeFragments ¶
type EventMergeFragments struct {
Fragments []Fragment
CtxFragments []CtxFragment
// Selects the target element of the merge process using a CSS selector.
Selector string
// Sets the mode to merge fragments with.
// Refer to individual MergeMode constants for details.
MergeMode MergeMode
// Determines whether to use view transitions when merging into the DOM.
UseViewTransition bool
}
EventMergeFragments is the implementation for `datastar-merge-fragments` event. You can set both Fragment and CtxFragments, as many as you need in a single request. Event options can be set with respective fields.
func MergeCtxFragments ¶
func MergeCtxFragments(fragments ...CtxFragment) EventMergeFragments
MergeCtxFragments is a shortcut to create a `datastar-merge-fragments` event with a set of ctx aware fragments.
func MergeFragments ¶
func MergeFragments(fragments ...Fragment) EventMergeFragments
MergeFragments is a shortcut to create a `datastar-merge-fragments` event with a set of fragments.
func (EventMergeFragments) Name ¶
func (event EventMergeFragments) Name() string
func (EventMergeFragments) WriteEvent ¶
func (event EventMergeFragments) WriteEvent(writer *sseserver.EventWriter, req *http.Request) error
type EventMergeSignals ¶
type EventMergeSignals struct {
// Signals values, refer to MergeSignals for docs.
Signals Signals
// Value is marshalled to a json object and sent to frontend.
Value any
// OnlyIfMissing determines whether to update the signals with new values only if the key does not exist.
OnlyIfMissing bool
}
EventMergeSignals is the implementation for `datastar-merge-signals` event.
Only one of Signals or Value may be provided. If both are set, only Value will be used.
Use respective MergeSignals and MergeSignalsObj functions to simplify creation of the event.
func MergeSignals ¶
func MergeSignals(signals Signals) EventMergeSignals
MergeSignals is a shortcut to create a `datastar-merge-signals` event with a Signals value. Signals is a map[string]any that is transformed (unflattened) and set as Value field.
Top level signal names are split by the "." separator. If you send a signal {"user.fullname.name": "john"} it will be turned into {"user": {"fullname": {"name": "john"}}}. You can use nested signals if you wish.
func MergeSignalsObj ¶
func MergeSignalsObj(signals any) EventMergeSignals
MergeSignalsObj is a shortcut to create a `datastar-merge-signals` event with a struct value.
func (EventMergeSignals) Name ¶
func (event EventMergeSignals) Name() string
func (EventMergeSignals) WriteEvent ¶
func (event EventMergeSignals) WriteEvent(writer *sseserver.EventWriter, req *http.Request) error
type EventRemoveFragments ¶
type EventRemoveFragments struct {
// Selector is used to match the elements to remove from the DOM.
// Only one selector can be provided in the event.
Selector string
}
EventRemoveFragments is the implementation for `datastar-remove-fragments` event.
func RemoveFragments ¶
func RemoveFragments(selector string) EventRemoveFragments
RemoveFragments is a shortcut to create a `datastar-remove-fragments` event.
func (EventRemoveFragments) Name ¶
func (event EventRemoveFragments) Name() string
func (EventRemoveFragments) WriteEvent ¶
func (event EventRemoveFragments) WriteEvent(writer *sseserver.EventWriter, req *http.Request) error
type EventRemoveSignals ¶
type EventRemoveSignals struct {
// Full paths to match the signals to remove.
Paths []string
}
EventRemoveSignals is the implementation for `datastar-remove-signals` event.
func RemoveSignals ¶
func RemoveSignals(paths ...string) EventRemoveSignals
RemoveSignals is a shortcut to create a `datastar-remove-signals` event.
func (EventRemoveSignals) Name ¶
func (event EventRemoveSignals) Name() string
func (EventRemoveSignals) WriteEvent ¶
func (event EventRemoveSignals) WriteEvent(writer *sseserver.EventWriter, req *http.Request) error
type MergeMode ¶
type MergeMode string
MergeMode sets the mode to merge fragments with. Refer to individual MergeMode constants for details.
const ( ModeMorph MergeMode = "morph" //Merges the fragment using Idiomorph. This is the default merge strategy. ModeInner MergeMode = "inner" //Replaces the target’s innerHTML with the fragment. ModeOuter MergeMode = "outer" //Replaces the target’s outerHTML with the fragment. ModePrepend MergeMode = "prepend" //Prepends the fragment to the target’s children. ModeAppend MergeMode = "append" //Appends the fragment to the target’s children. ModeBefore MergeMode = "before" //Inserts the fragment before the target as a sibling. ModeAfter MergeMode = "after" //Inserts the fragment after the target as a sibling. ModeUpsertAttributes MergeMode = "upsertAttributes" //Merges attributes from the fragment into the target – useful for updating a signals. )
type Signals ¶
Signals is a map[string]any that is transformed (unflattened) and set as Value field.
Top level signal names are split by the "." separator. If you send a signal {"user.fullname.name": "john"} it will be turned into {"user": {"fullname": {"name": "john"}}}. You can use nested signals if you wish.