-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
158 lines (132 loc) · 4.35 KB
/
index.ts
File metadata and controls
158 lines (132 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
import type { HtmxExtension } from "htmx.org";
import type htmxType from "htmx.org";
//
declare const htmx: typeof htmxType;
//
(function () {
let api: HtmxApi;
// Helper function to detect chunked transfer (HTTP/1.1) or streaming (HTTP/2)
function isChunkedTransfer(xhr: XMLHttpRequest): boolean {
const te = xhr.getResponseHeader("Transfer-Encoding");
const cl = xhr.getResponseHeader("Content-Length");
const isHttp1Chunked = te === "chunked";
const isStreamingWithoutLength = !te && !cl; // typical HTTP/2 streaming
return isHttp1Chunked || isStreamingWithoutLength;
}
htmx.defineExtension("chunked-transfer", {
init: function (apiRef: HtmxApi) {
api = apiRef;
},
onEvent: function (name, evt) {
const elt = evt.target as Element;
const target = api.getTarget(elt);
if (name === "htmx:beforeRequest") {
const xhr = evt.detail.xhr as XMLHttpRequest;
(xhr as any)._chunkedMode =
elt.getAttribute("hx-chunked-mode") || "append";
(xhr as any)._chunkedLastLen = 0;
xhr.onprogress = function () {
if (!isChunkedTransfer(xhr)) return;
const swapSpec = api.getSwapSpecification(elt);
if (swapSpec.swapStyle !== "innerHTML") return;
const mode = (xhr as any)._chunkedMode || "append";
const full = (xhr.response as string) ?? "";
let response: string;
if (mode === "swap") {
const lastLen = (xhr as any)._chunkedLastLen || 0;
if (full.length <= lastLen) return;
response = full.slice(lastLen);
(xhr as any)._chunkedLastLen = full.length;
} else {
response = full;
}
// Skip comment-only chunks (heartbeats), but allow empty chunks through
const hasComment = /<!--[\s\S]*?-->/.test(response);
const stripped = response.replace(/<!--[\s\S]*?-->/g, "").trim();
if (hasComment && stripped.length === 0) return;
api.withExtensions(elt, function (extension) {
if (!extension.transformResponse) return;
response = extension.transformResponse(response, xhr, elt);
});
const settleInfo = api.makeSettleInfo(elt);
if (api.swap) {
// Pass contextElement to enable OOB swap processing (Issue #117)
api.swap(target, response, swapSpec, {
contextElement: elt,
} as any);
} else {
api.selectAndSwap(
swapSpec.swapStyle,
target,
elt,
response,
settleInfo,
);
}
api.settleImmediately(settleInfo.tasks);
};
}
// Keep: cancel final full swap in swap mode
if (name === "htmx:beforeSwap") {
const detail = evt.detail as any;
const xhr = detail && (detail.xhr as XMLHttpRequest | undefined);
if (!xhr) return;
const mode = (xhr as any)._chunkedMode;
if (mode !== "swap") return;
if (!isChunkedTransfer(xhr)) return;
detail.shouldSwap = false;
}
},
} as HtmxExtension & { init: (apiRef: any) => void });
})();
//
// Inspired by https://github.com/delaneyj/nothtmx2
interface HtmxApi {
defineExtension(name: string, extension: HtmxExtension): void;
getSwapSpecification(elt: Element): SwapSpec;
getTarget(elt: Element): Element;
makeSettleInfo(elt: Element): SettleInfo;
selectAndSwap( // HTMX 1.0
swapStyle: string,
target: Element,
elt: Element,
responseText: string,
settleInfo: SettleInfo,
): void;
swap( // HTMX 2.0
target: Element,
content: string,
swapSpec: SwapSpec,
swapOptions?: SwapOptions,
): void;
settleImmediately(tasks: Task[]): void;
withExtensions(
elt: Element,
callback: (extension: HtmxExtension) => void,
): void;
}
interface SwapSpec {
swapStyle: string;
swapDelay: Number;
settleDelay: Number;
transition: Boolean;
ignoreTitle: Boolean;
head: string;
// scroll, scrollTarget, show, showTarget, focusScroll
}
interface SwapOptions {
select?: string;
selectOOB?: string;
eventInfo?: Object;
anchor?: Element;
contextElement?: Element;
afterSwapCallback?: () => void;
afterSettleCallback?: () => void;
}
interface SettleInfo {
title?: string;
elts: Element[];
tasks: Task[];
}
export type Task = () => void;