hx-stream 0.0.9 → 0.0.10

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.
Files changed (2) hide show
  1. package/dist/server.js +13 -8
  2. package/package.json +1 -1
package/dist/server.js CHANGED
@@ -14,6 +14,7 @@ function MakeBoundary() {
14
14
  }
15
15
  export class StreamResponse {
16
16
  #controller;
17
+ #signal;
17
18
  #timer;
18
19
  #state;
19
20
  #boundary;
@@ -36,6 +37,7 @@ export class StreamResponse {
36
37
  // immediate prepare for abortion
37
38
  const cancel = () => { this.close(); };
38
39
  request.signal.addEventListener('abort', cancel);
40
+ this.#signal = request.signal;
39
41
  const start = (c) => { this.#controller = c; this.#state = StreamResponse.OPEN; };
40
42
  const stream = new ReadableStream({ start, cancel }, { highWaterMark: options.highWaterMark || 0 });
41
43
  this.response = new Response(stream, { headers });
@@ -46,6 +48,14 @@ export class StreamResponse {
46
48
  this.#controller = controller;
47
49
  }
48
50
  sendBytes(chunk) {
51
+ if (this.#state === StreamResponse.CLOSED) {
52
+ const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
53
+ console.warn(err);
54
+ }
55
+ if (this.#signal.aborted) {
56
+ this.close();
57
+ return false;
58
+ }
49
59
  if (!this.#controller)
50
60
  return false;
51
61
  try {
@@ -63,10 +73,6 @@ export class StreamResponse {
63
73
  }
64
74
  keepAlive() { return this.sendText(" "); }
65
75
  send(target, swap, html) {
66
- if (this.#state === StreamResponse.CLOSED) {
67
- const err = new Error(`Warn: Attempted to send data on closed stream for: ${this.url}`);
68
- console.warn(err);
69
- }
70
76
  if (typeof html !== "string") {
71
77
  if (!this.#render)
72
78
  throw new Error(`Cannot render to JSX when no renderer provided during class initialization`);
@@ -75,10 +81,6 @@ export class StreamResponse {
75
81
  return this.sendText(`<${this.#boundary}>${target}|${swap}|${html}</${this.#boundary}>\n`);
76
82
  }
77
83
  close() {
78
- if (this.#state === StreamResponse.CLOSED) {
79
- this.#controller = null;
80
- return false;
81
- }
82
84
  if (this.#controller) {
83
85
  try {
84
86
  this.#controller.close();
@@ -91,6 +93,9 @@ export class StreamResponse {
91
93
  // Cleanup
92
94
  if (this.#timer)
93
95
  clearInterval(this.#timer);
96
+ // was already closed
97
+ if (this.#state === EventSource.CLOSED)
98
+ return false;
94
99
  // Mark closed
95
100
  this.#state = StreamResponse.CLOSED;
96
101
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hx-stream",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "keep htmx client state across page refreshes",
5
5
  "keywords": [ "htmx", "streaming" ],
6
6
  "homepage": "https://hx-stream.ajanibilby.com",