htmx-router 1.0.14 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/event-source.js +13 -8
- package/package.json +1 -1
package/event-source.js
CHANGED
|
@@ -17,6 +17,7 @@ const headers = {
|
|
|
17
17
|
*/
|
|
18
18
|
export class EventSource {
|
|
19
19
|
#controller;
|
|
20
|
+
#signal;
|
|
20
21
|
#timer;
|
|
21
22
|
#state;
|
|
22
23
|
response;
|
|
@@ -43,6 +44,7 @@ export class EventSource {
|
|
|
43
44
|
// immediate prepare for abortion
|
|
44
45
|
const cancel = () => { this.close(); };
|
|
45
46
|
request.signal.addEventListener('abort', cancel);
|
|
47
|
+
this.#signal = request.signal;
|
|
46
48
|
const start = (c) => { this.#controller = c; this.#state = EventSource.OPEN; };
|
|
47
49
|
const stream = new ReadableStream({ start, cancel }, { highWaterMark: 0 });
|
|
48
50
|
this.response = new Response(stream, { headers });
|
|
@@ -50,6 +52,14 @@ export class EventSource {
|
|
|
50
52
|
register.add(this);
|
|
51
53
|
}
|
|
52
54
|
sendBytes(chunk, active) {
|
|
55
|
+
if (this.#state === EventSource.CLOSED) {
|
|
56
|
+
const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
|
|
57
|
+
console.warn(err);
|
|
58
|
+
}
|
|
59
|
+
if (this.#signal.aborted) {
|
|
60
|
+
this.close();
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
53
63
|
if (!this.#controller)
|
|
54
64
|
return false;
|
|
55
65
|
try {
|
|
@@ -71,17 +81,9 @@ export class EventSource {
|
|
|
71
81
|
return this.sendText("\n\n", false);
|
|
72
82
|
}
|
|
73
83
|
dispatch(type, data) {
|
|
74
|
-
if (this.#state === EventSource.CLOSED) {
|
|
75
|
-
const err = new Error(`Warn: Attempted to dispatch event "${type}" to a closed connection for: ${this.url}`, {});
|
|
76
|
-
console.warn(err);
|
|
77
|
-
}
|
|
78
84
|
return this.sendText(`event: ${type}\ndata: ${data}\n\n`, true);
|
|
79
85
|
}
|
|
80
86
|
close(unlink = true) {
|
|
81
|
-
if (this.#state === EventSource.CLOSED) {
|
|
82
|
-
this.#controller = null;
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
87
|
if (this.#controller) {
|
|
86
88
|
try {
|
|
87
89
|
this.#controller.close();
|
|
@@ -96,6 +98,9 @@ export class EventSource {
|
|
|
96
98
|
clearInterval(this.#timer);
|
|
97
99
|
if (unlink)
|
|
98
100
|
register.delete(this);
|
|
101
|
+
// was already closed
|
|
102
|
+
if (this.#state === EventSource.CLOSED)
|
|
103
|
+
return false;
|
|
99
104
|
// Mark closed
|
|
100
105
|
this.#state = EventSource.CLOSED;
|
|
101
106
|
return true;
|