- Add debounced state updates for title and content (500ms delay) - Immediate UI updates with delayed history saving - Prevent one-letter-per-undo issue - Add cleanup for debounce timers on unmount
73 lines
2.1 KiB
JavaScript
73 lines
2.1 KiB
JavaScript
// src/response.ts
|
|
var responseCache = Symbol("responseCache");
|
|
var getResponseCache = Symbol("getResponseCache");
|
|
var cacheKey = Symbol("cache");
|
|
var GlobalResponse = global.Response;
|
|
var Response = class _Response {
|
|
#body;
|
|
#init;
|
|
[getResponseCache]() {
|
|
delete this[cacheKey];
|
|
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
}
|
|
constructor(body, init) {
|
|
let headers;
|
|
this.#body = body;
|
|
if (init instanceof _Response) {
|
|
const cachedGlobalResponse = init[responseCache];
|
|
if (cachedGlobalResponse) {
|
|
this.#init = cachedGlobalResponse;
|
|
this[getResponseCache]();
|
|
return;
|
|
} else {
|
|
this.#init = init.#init;
|
|
headers = new Headers(init.#init.headers);
|
|
}
|
|
} else {
|
|
this.#init = init;
|
|
}
|
|
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
this[cacheKey] = [init?.status || 200, body, headers];
|
|
}
|
|
}
|
|
get headers() {
|
|
const cache = this[cacheKey];
|
|
if (cache) {
|
|
if (!(cache[2] instanceof Headers)) {
|
|
cache[2] = new Headers(cache[2]);
|
|
}
|
|
return cache[2];
|
|
}
|
|
return this[getResponseCache]().headers;
|
|
}
|
|
get status() {
|
|
return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
|
|
}
|
|
get ok() {
|
|
const status = this.status;
|
|
return status >= 200 && status < 300;
|
|
}
|
|
};
|
|
["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
|
|
Object.defineProperty(Response.prototype, k, {
|
|
get() {
|
|
return this[getResponseCache]()[k];
|
|
}
|
|
});
|
|
});
|
|
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
Object.defineProperty(Response.prototype, k, {
|
|
value: function() {
|
|
return this[getResponseCache]()[k]();
|
|
}
|
|
});
|
|
});
|
|
Object.setPrototypeOf(Response, GlobalResponse);
|
|
Object.setPrototypeOf(Response.prototype, GlobalResponse.prototype);
|
|
export {
|
|
GlobalResponse,
|
|
Response,
|
|
cacheKey
|
|
};
|