wipwipwip
This commit is contained in:
@ -142,17 +142,14 @@ func CreateEventsHandler(notifier *Notifier[Notification]) http.HandlerFunc {
|
|||||||
w.Header().Set("Content-Type", "text/event-stream")
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
w.Header().Set("Connection", "keep-alive")
|
w.Header().Set("Connection", "keep-alive")
|
||||||
w.(http.Flusher).Flush()
|
// w.(http.Flusher).Flush()
|
||||||
|
|
||||||
notifier.AddKey(userId.String())
|
|
||||||
defer notifier.RemoveKey(userId.String())
|
|
||||||
|
|
||||||
if err := notifier.Create(userId.String()); err != nil {
|
if err := notifier.Create(userId.String()); err != nil {
|
||||||
// TODO: this could be better.
|
// TODO: this could be better.
|
||||||
// EG: The user could attempt to create many connections
|
// EG: The user could attempt to create many connections
|
||||||
// and they just get a 500, with no explanation.
|
// and they just get a 500, with no explanation.
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.(http.Flusher).Flush()
|
// w.(http.Flusher).Flush()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +171,7 @@ func CreateEventsHandler(notifier *Notifier[Notification]) http.HandlerFunc {
|
|||||||
|
|
||||||
fmt.Printf("Sending msg %s\n", msg)
|
fmt.Printf("Sending msg %s\n", msg)
|
||||||
fmt.Fprintf(w, "event: data\ndata: %s\n\n", string(msgString))
|
fmt.Fprintf(w, "event: data\ndata: %s\n\n", string(msgString))
|
||||||
w.(http.Flusher).Flush()
|
// w.(http.Flusher).Flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,6 @@ type Notifier[TNotification any] struct {
|
|||||||
bufferSize int
|
bufferSize int
|
||||||
|
|
||||||
Listeners map[string]chan TNotification
|
Listeners map[string]chan TNotification
|
||||||
|
|
||||||
AllowedKeys map[string]bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notifier[TNotification]) Create(id string) error {
|
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")
|
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)
|
n.Listeners[id] = make(chan TNotification, n.bufferSize)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -56,18 +50,9 @@ func (n *Notifier[TNotification]) Delete(id string) error {
|
|||||||
return nil
|
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] {
|
func NewNotifier[TNotification any](bufferSize int) Notifier[TNotification] {
|
||||||
return Notifier[TNotification]{
|
return Notifier[TNotification]{
|
||||||
bufferSize: bufferSize,
|
bufferSize: bufferSize,
|
||||||
Listeners: make(map[string]chan TNotification),
|
Listeners: make(map[string]chan TNotification),
|
||||||
AllowedKeys: make(map[string]bool),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,6 @@ func TestSendingNotifications(t *testing.T) {
|
|||||||
|
|
||||||
notifier := NewNotifier[string](3)
|
notifier := NewNotifier[string](3)
|
||||||
|
|
||||||
notifier.AddKey("1")
|
|
||||||
|
|
||||||
err := notifier.SendAndCreate("1", "a")
|
err := notifier.SendAndCreate("1", "a")
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
|
|
||||||
@ -41,8 +39,6 @@ func TestFullBuffer(t *testing.T) {
|
|||||||
|
|
||||||
notifier := NewNotifier[string](1)
|
notifier := NewNotifier[string](1)
|
||||||
|
|
||||||
notifier.AddKey("1")
|
|
||||||
|
|
||||||
err := notifier.SendAndCreate("1", "a")
|
err := notifier.SendAndCreate("1", "a")
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
|
|
||||||
@ -50,11 +46,3 @@ func TestFullBuffer(t *testing.T) {
|
|||||||
|
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoAllowedKey(t *testing.T) {
|
|
||||||
require := require.New(t)
|
|
||||||
notifier := NewNotifier[string](1)
|
|
||||||
|
|
||||||
err := notifier.SendAndCreate("1", "a")
|
|
||||||
require.Error(err)
|
|
||||||
}
|
|
||||||
|
@ -26,8 +26,8 @@
|
|||||||
"icons/icon.icns",
|
"icons/icon.icns",
|
||||||
"icons/icon.ico"
|
"icons/icon.ico"
|
||||||
],
|
],
|
||||||
"macOS": {
|
"macOS": {
|
||||||
"signingIdentity": "6F3F957C06DE870B9A9F2CA8C2E762C6752AB2CB"
|
"signingIdentity": "6F3F957C06DE870B9A9F2CA8C2E762C6752AB2CB"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,8 @@ type BaseRequestParams = Partial<{
|
|||||||
method: "GET" | "POST";
|
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 => {
|
const getBaseRequest = ({ path, body, method }: BaseRequestParams): Request => {
|
||||||
return new Request(`${base}/${path}`, {
|
return new Request(`${base}/${path}`, {
|
||||||
|
@ -83,6 +83,10 @@ export const Notifications = (onCompleteImage: () => void) => {
|
|||||||
|
|
||||||
events.addEventListener("data", dataEventListener);
|
events.addEventListener("data", dataEventListener);
|
||||||
|
|
||||||
|
events.onerror = (e) => {
|
||||||
|
console.error(e);
|
||||||
|
};
|
||||||
|
|
||||||
onCleanup(() => {
|
onCleanup(() => {
|
||||||
events.removeEventListener("data", dataEventListener);
|
events.removeEventListener("data", dataEventListener);
|
||||||
events.close();
|
events.close();
|
||||||
|
Reference in New Issue
Block a user