wipwipwip

This commit is contained in:
2025-05-12 19:54:09 +01:00
parent 3eab20049e
commit 636bd9df0e
6 changed files with 14 additions and 39 deletions

View File

@ -142,17 +142,14 @@ func CreateEventsHandler(notifier *Notifier[Notification]) http.HandlerFunc {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.(http.Flusher).Flush()
notifier.AddKey(userId.String())
defer notifier.RemoveKey(userId.String())
// w.(http.Flusher).Flush()
if err := notifier.Create(userId.String()); err != nil {
// TODO: this could be better.
// EG: The user could attempt to create many connections
// and they just get a 500, with no explanation.
w.WriteHeader(http.StatusInternalServerError)
w.(http.Flusher).Flush()
// w.(http.Flusher).Flush()
return
}
@ -174,7 +171,7 @@ func CreateEventsHandler(notifier *Notifier[Notification]) http.HandlerFunc {
fmt.Printf("Sending msg %s\n", msg)
fmt.Fprintf(w, "event: data\ndata: %s\n\n", string(msgString))
w.(http.Flusher).Flush()
// w.(http.Flusher).Flush()
}
}
}

View File

@ -8,8 +8,6 @@ type Notifier[TNotification any] struct {
bufferSize int
Listeners map[string]chan TNotification
AllowedKeys map[string]bool
}
func (n *Notifier[TNotification]) Create(id string) error {
@ -17,10 +15,6 @@ func (n *Notifier[TNotification]) Create(id string) error {
return errors.New("This listener already exists")
}
if _, exists := n.AllowedKeys[id]; !exists {
return errors.New("This key cannot be used to create a listener")
}
n.Listeners[id] = make(chan TNotification, n.bufferSize)
return nil
@ -56,18 +50,9 @@ func (n *Notifier[TNotification]) Delete(id string) error {
return nil
}
func (n *Notifier[TNotification]) AddKey(id string) {
n.AllowedKeys[id] = true
}
func (n *Notifier[TNotification]) RemoveKey(id string) {
delete(n.AllowedKeys, id)
}
func NewNotifier[TNotification any](bufferSize int) Notifier[TNotification] {
return Notifier[TNotification]{
bufferSize: bufferSize,
Listeners: make(map[string]chan TNotification),
AllowedKeys: make(map[string]bool),
bufferSize: bufferSize,
Listeners: make(map[string]chan TNotification),
}
}

View File

@ -13,8 +13,6 @@ func TestSendingNotifications(t *testing.T) {
notifier := NewNotifier[string](3)
notifier.AddKey("1")
err := notifier.SendAndCreate("1", "a")
require.NoError(err)
@ -41,8 +39,6 @@ func TestFullBuffer(t *testing.T) {
notifier := NewNotifier[string](1)
notifier.AddKey("1")
err := notifier.SendAndCreate("1", "a")
require.NoError(err)
@ -50,11 +46,3 @@ func TestFullBuffer(t *testing.T) {
assert.Error(err)
}
func TestNoAllowedKey(t *testing.T) {
require := require.New(t)
notifier := NewNotifier[string](1)
err := notifier.SendAndCreate("1", "a")
require.Error(err)
}

View File

@ -26,8 +26,8 @@
"icons/icon.icns",
"icons/icon.ico"
],
"macOS": {
"signingIdentity": "6F3F957C06DE870B9A9F2CA8C2E762C6752AB2CB"
}
"macOS": {
"signingIdentity": "6F3F957C06DE870B9A9F2CA8C2E762C6752AB2CB"
}
}
}

View File

@ -20,7 +20,8 @@ type BaseRequestParams = Partial<{
method: "GET" | "POST";
}>;
export const base = "https://haystack.johncosta.tech";
// export const base = "https://haystack.johncosta.tech";
export const base = "http://192.168.1.199:3040";
const getBaseRequest = ({ path, body, method }: BaseRequestParams): Request => {
return new Request(`${base}/${path}`, {

View File

@ -83,6 +83,10 @@ export const Notifications = (onCompleteImage: () => void) => {
events.addEventListener("data", dataEventListener);
events.onerror = (e) => {
console.error(e);
};
onCleanup(() => {
events.removeEventListener("data", dataEventListener);
events.close();