Skip to content

Commit

Permalink
retyped to guard, fixes #56, fixes #57, fixes #58
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftyp committed Sep 14, 2023
1 parent cc583f7 commit 8991680
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/http/fetch_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export class FetchRequest {
url: URL
target?: FrameElement | HTMLFormElement | null
abortController = new AbortController()
private resolveRequestPromise = (_value: any) => {}
fetchOptions: FetchRequestOptions
enctype: string
#resolveRequestPromise = (_value: unknown) => {}
Expand Down Expand Up @@ -173,7 +172,7 @@ export class FetchRequest {
async perform(): Promise<FetchResponse | void> {
const { fetchOptions } = this
this.delegate.prepareRequest(this)
await this.allowRequestToBeIntercepted(fetchOptions)
await this.#allowRequestToBeIntercepted(fetchOptions)
try {
this.delegate.requestStarted(this)
const response = await fetch(this.url.href, fetchOptions)
Expand Down Expand Up @@ -225,14 +224,14 @@ export class FetchRequest {
this.headers["Accept"] = [mimeType, this.headers["Accept"]].join(", ")
}

private async allowRequestToBeIntercepted(fetchOptions: RequestInit) {
const requestInterception = new Promise((resolve) => (this.resolveRequestPromise = resolve))
async #allowRequestToBeIntercepted(fetchOptions: RequestInit) {
const requestInterception = new Promise((resolve) => (this.#resolveRequestPromise = resolve))
const event = dispatch<TurboBeforeFetchRequestEvent>("turbo:before-fetch-request", {
cancelable: true,
detail: {
fetchOptions,
url: this.url,
resume: this.resolveRequestPromise,
resume: this.#resolveRequestPromise,
},
target: this.target as EventTarget,
})
Expand Down
8 changes: 4 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ export function findClosestRecursively<E extends Element>(element: Element | nul
}
}

export function elementIsFocusable(element: HTMLElement | null) {
export function elementIsFocusable(element: Element | null): element is HTMLElement {
const inertDisabledOrHidden = "[inert], :disabled, [hidden], details:not([open]), dialog:not([open])"

return !!element && element.closest(inertDisabledOrHidden) == null && typeof element.focus == "function"
return !!element && element.closest(inertDisabledOrHidden) == null && typeof (element as HTMLElement).focus == "function"
}

export function queryAutofocusableElement(elementOrDocumentFragment: HTMLElement | DocumentFragment) {
export function queryAutofocusableElement(elementOrDocumentFragment: Element | DocumentFragment) {
for (const element of elementOrDocumentFragment.querySelectorAll("[autofocus]")) {
if (elementIsFocusable(element as HTMLElement)) return element
if (elementIsFocusable(element)) return element
else continue
}

Expand Down

0 comments on commit 8991680

Please sign in to comment.