Skip to content

Commit

Permalink
Re-use getVisitAction utility function (#799)
Browse files Browse the repository at this point in the history
The logic for reading an `Action` from a list of `Element` instances'
`[data-turbo-action]` attribute is still scattered across several
modules, in spite of the `getVisitAction` declared in the `util` module.

This commit replaces duplicate implementations of the `getVisitAction`
method with the utility function.

With those calls added, the `isAction` type checking utility is only
ever invoked by `getVisitAction`, so this commit moves its
implementation to the `util` module, and removes all other invocations.
  • Loading branch information
seanpdoyle authored Nov 27, 2022
1 parent 055c3a7 commit 515ae56
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
10 changes: 4 additions & 6 deletions src/core/drive/navigator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Action, isAction } from "../types"
import { Action } from "../types"
import { getVisitAction } from "../../util"
import { FetchMethod } from "../../http/fetch_request"
import { FetchResponse } from "../../http/fetch_response"
import { FormSubmission } from "./form_submission"
import { expandURL, getAnchor, getRequestURL, Locatable, locationIsVisitable } from "../url"
import { getAttribute } from "../../util"
import { Visit, VisitDelegate, VisitOptions } from "./visit"
import { PageSnapshot } from "./page_snapshot"

Expand Down Expand Up @@ -164,9 +164,7 @@ export class Navigator {
return this.history.restorationIdentifier
}

getActionForFormSubmission(formSubmission: FormSubmission): Action {
const { formElement, submitter } = formSubmission
const action = getAttribute("data-turbo-action", submitter, formElement)
return isAction(action) ? action : "advance"
getActionForFormSubmission({ submitter, formElement }: FormSubmission): Action {
return getVisitAction(submitter, formElement) || "advance"
}
}
4 changes: 2 additions & 2 deletions src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { LinkInterceptor, LinkInterceptorDelegate } from "./link_interceptor"
import { FormLinkClickObserver, FormLinkClickObserverDelegate } from "../../observers/form_link_click_observer"
import { FrameRenderer } from "./frame_renderer"
import { session } from "../index"
import { isAction, Action } from "../types"
import { Action } from "../types"
import { VisitOptions } from "../drive/visit"
import { TurboBeforeFrameRenderEvent } from "../session"
import { StreamMessage } from "../streams/stream_message"
Expand Down Expand Up @@ -381,7 +381,7 @@ export class FrameController
proposeVisitIfNavigatedWithAction(frame: FrameElement, element: Element, submitter?: HTMLElement) {
this.action = getVisitAction(submitter, element, frame)

if (isAction(this.action)) {
if (this.action) {
const { visitCachedSnapshot } = frame.delegate

frame.delegate.fetchResponseLoaded = (fetchResponse: FetchResponse) => {
Expand Down
7 changes: 3 additions & 4 deletions src/core/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { ScrollObserver } from "../observers/scroll_observer"
import { StreamMessage } from "./streams/stream_message"
import { StreamMessageRenderer } from "./streams/stream_message_renderer"
import { StreamObserver } from "../observers/stream_observer"
import { Action, Position, StreamSource, isAction } from "./types"
import { clearBusyState, dispatch, markAsBusy } from "../util"
import { Action, Position, StreamSource } from "./types"
import { clearBusyState, dispatch, getVisitAction, markAsBusy } from "../util"
import { PageView, PageViewDelegate, PageViewRenderOptions } from "./drive/page_view"
import { Visit, VisitOptions } from "./drive/visit"
import { PageSnapshot } from "./drive/page_snapshot"
Expand Down Expand Up @@ -427,8 +427,7 @@ export class Session
// Private

getActionForLink(link: Element): Action {
const action = link.getAttribute("data-turbo-action")
return isAction(action) ? action : "advance"
return getVisitAction(link) || "advance"
}

get snapshot() {
Expand Down
4 changes: 0 additions & 4 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
export type Action = "advance" | "replace" | "restore"

export function isAction(action: any): action is Action {
return action == "advance" || action == "replace" || action == "restore"
}

export type Position = { x: number; y: number }

export type StreamSource = {
Expand Down
3 changes: 2 additions & 1 deletion src/observers/form_link_click_observer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LinkClickObserver, LinkClickObserverDelegate } from "./link_click_observer"
import { getVisitAction } from "../util"

export type FormLinkClickObserverDelegate = {
willSubmitFormLinkToLocation(link: Element, location: URL, event: MouseEvent): boolean
Expand Down Expand Up @@ -42,7 +43,7 @@ export class FormLinkClickObserver implements LinkClickObserverDelegate {
const turboFrame = link.getAttribute("data-turbo-frame")
if (turboFrame) form.setAttribute("data-turbo-frame", turboFrame)

const turboAction = link.getAttribute("data-turbo-action")
const turboAction = getVisitAction(link)
if (turboAction) form.setAttribute("data-turbo-action", turboAction)

const turboConfirm = link.getAttribute("data-turbo-confirm")
Expand Down
6 changes: 5 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action, isAction } from "./core/types"
import { Action } from "./core/types"

export type DispatchOptions<T extends CustomEvent> = {
target: EventTarget
Expand Down Expand Up @@ -154,6 +154,10 @@ export function getHistoryMethodForAction(action: Action) {
}
}

export function isAction(action: any): action is Action {
return action == "advance" || action == "replace" || action == "restore"
}

export function getVisitAction(...elements: (Element | undefined)[]): Action | null {
const action = getAttribute("data-turbo-action", ...elements)

Expand Down

0 comments on commit 515ae56

Please sign in to comment.