-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// package sse connects to an EventSource directly from HTML. It manages the connections to your web server, listens for server events, and then swaps their contents into your htmx webpage in real-time. | ||
// | ||
// [EventSource]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events | ||
package sse | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/will-wow/typed-htmx-go/htmx" | ||
) | ||
|
||
// Extension connects to an EventSource directly from HTML. It manages the connections to your web server, listens for server events, and then swaps their contents into your htmx webpage in real-time. | ||
// | ||
// # Install | ||
// | ||
// <script src="https://unpkg.com/[email protected]/dist/ext/sse.js"></script> | ||
// | ||
// Extension: [server-sent-events] | ||
// | ||
// [server-sent-events]: https://htmx.org/extensions/server-sent-events/ | ||
// | ||
// [EventSource]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events | ||
const Extension htmx.Extension = "sse" | ||
|
||
// Message is the the default name of an empty SSE event. | ||
const Message = "message" | ||
|
||
// Connect | ||
func Connect[T any](hx htmx.HX[T], url string) T { | ||
return hx.Attr("sse-connect", url) | ||
} | ||
|
||
// Swap | ||
func Swap[T any](hx htmx.HX[T], messageName string) T { | ||
return hx.Attr("sse-swap", messageName) | ||
} | ||
|
||
// Trigger allows SSE messages to trigger HTTP callbacks using the [htmx.HX.Trigger()] attribute. | ||
func Trigger[T any](hx htmx.HX[T], event string) T { | ||
prefixedEvent := fmt.Sprintf("sse:%s", event) | ||
return hx.Attr(htmx.Trigger, prefixedEvent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package sse_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/will-wow/typed-htmx-go/htmx" | ||
"github.com/will-wow/typed-htmx-go/htmx/ext/sse" | ||
) | ||
|
||
var hx = htmx.NewStringAttrs() | ||
|
||
func ExampleExtension() { | ||
attr := hx.Ext(sse.Extension) | ||
fmt.Println(attr) | ||
// Output: hx-ext='sse' | ||
} | ||
|
||
func ExampleMessage() { | ||
attr := sse.Swap(hx, sse.Message) | ||
fmt.Println(attr) | ||
// Output: sse-swap='message' | ||
} | ||
|
||
func ExampleConnect() { | ||
attr := sse.Connect(hx, "/chatroom") | ||
fmt.Println(attr) | ||
// Output: sse-connect='/chatroom' | ||
} | ||
|
||
func ExampleSwap() { | ||
attr := sse.Swap(hx, "event") | ||
fmt.Println(attr) | ||
// Output: sse-swap='event' | ||
} | ||
|
||
func ExampleTrigger() { | ||
attr := sse.Trigger(hx, "event") | ||
fmt.Println(attr) | ||
// Output: hx-trigger='sse:event' | ||
} |