Skip to content

Commit

Permalink
Extract ViewRenderOptions
Browse files Browse the repository at this point in the history
Introduce the `ViewRenderOptions<E extends Element>` to store the
`resume()` function dispatched with the `turbo:before-render` event.

Future commits will add additional properties to this option interface.
  • Loading branch information
seanpdoyle committed Nov 11, 2021
1 parent 8e5699a commit aad4011
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/core/drive/page_view.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { nextEventLoopTick } from "../../util"
import { View, ViewDelegate } from "../view"
import { View, ViewDelegate, ViewRenderOptions } from "../view"
import { ErrorRenderer } from "./error_renderer"
import { PageRenderer } from "./page_renderer"
import { PageSnapshot } from "./page_snapshot"
import { SnapshotCache } from "./snapshot_cache"

export interface PageViewDelegate extends ViewDelegate<PageSnapshot> {
export interface PageViewRenderOptions extends ViewRenderOptions<HTMLBodyElement> {
}

export interface PageViewDelegate extends ViewDelegate<HTMLBodyElement, PageSnapshot> {
viewWillCacheSnapshot(): void
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AppearanceObserver, AppearanceObserverDelegate } from "../../observers/
import { clearBusyState, getAttribute, parseHTMLDocument, markAsBusy } from "../../util"
import { FormSubmission, FormSubmissionDelegate } from "../drive/form_submission"
import { Snapshot } from "../snapshot"
import { ViewDelegate } from "../view"
import { ViewDelegate, ViewRenderOptions } from "../view"
import { getAction, expandURL, urlsAreEqual, locationIsVisitable, Locatable } from "../url"
import { FormInterceptor, FormInterceptorDelegate } from "./form_interceptor"
import { FrameView } from "./frame_view"
Expand All @@ -14,7 +14,7 @@ import { FrameRenderer } from "./frame_renderer"
import { session } from "../index"
import { isAction } from "../types"

export class FrameController implements AppearanceObserverDelegate, FetchRequestDelegate, FormInterceptorDelegate, FormSubmissionDelegate, FrameElementDelegate, LinkInterceptorDelegate, ViewDelegate<Snapshot<FrameElement>> {
export class FrameController implements AppearanceObserverDelegate, FetchRequestDelegate, FormInterceptorDelegate, FormSubmissionDelegate, FrameElementDelegate, LinkInterceptorDelegate, ViewDelegate<FrameElement, Snapshot<FrameElement>> {
readonly element: FrameElement
readonly view: FrameView
readonly appearanceObserver: AppearanceObserver
Expand Down Expand Up @@ -219,7 +219,7 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest

// View delegate

allowsImmediateRender(snapshot: Snapshot, resume: (value: any) => void) {
allowsImmediateRender(snapshot: Snapshot, options: ViewRenderOptions<FrameElement>) {
return true
}

Expand Down
10 changes: 5 additions & 5 deletions src/core/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { StreamMessage } from "./streams/stream_message"
import { StreamObserver } from "../observers/stream_observer"
import { Action, Position, StreamSource, isAction } from "./types"
import { clearBusyState, dispatch, markAsBusy } from "../util"
import { PageView, PageViewDelegate } from "./drive/page_view"
import { PageView, PageViewDelegate, PageViewRenderOptions } from "./drive/page_view"
import { Visit, VisitOptions } from "./drive/visit"
import { PageSnapshot } from "./drive/page_snapshot"
import { FrameElement } from "../elements/frame_element"
Expand Down Expand Up @@ -240,8 +240,8 @@ export class Session implements FormSubmitObserverDelegate, HistoryDelegate, Lin
}
}

allowsImmediateRender({ element }: PageSnapshot, resume: (value: any) => void) {
const event = this.notifyApplicationBeforeRender(element, resume)
allowsImmediateRender({ element }: PageSnapshot, options: PageViewRenderOptions) {
const event = this.notifyApplicationBeforeRender(element, options)
return !event.defaultPrevented
}

Expand Down Expand Up @@ -293,8 +293,8 @@ export class Session implements FormSubmitObserverDelegate, HistoryDelegate, Lin
return dispatch("turbo:before-cache")
}

notifyApplicationBeforeRender(newBody: HTMLBodyElement, resume: (value: any) => void) {
return dispatch("turbo:before-render", { detail: { newBody, resume }, cancelable: true })
notifyApplicationBeforeRender(newBody: HTMLBodyElement, options: PageViewRenderOptions) {
return dispatch("turbo:before-render", { detail: { newBody, ...options }, cancelable: true })
}

notifyApplicationAfterRender() {
Expand Down
9 changes: 7 additions & 2 deletions src/core/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { Snapshot } from "./snapshot"
import { Position } from "./types"
import { getAnchor } from "./url"

export interface ViewRenderOptions<E> {
resume: (value: any) => void
}

export interface ViewDelegate<E extends Element, S extends Snapshot<E>> {
allowsImmediateRender(snapshot: S, resume: (value: any) => void): boolean
allowsImmediateRender(snapshot: S, options: ViewRenderOptions<E>): boolean
viewRenderedSnapshot(snapshot: S, isPreview: boolean): void
viewInvalidated(): void
}
Expand Down Expand Up @@ -78,7 +82,8 @@ export abstract class View<E extends Element, S extends Snapshot<E> = Snapshot<E
this.prepareToRenderSnapshot(renderer)

const renderInterception = new Promise(resolve => this.resolveInterceptionPromise = resolve)
const immediateRender = this.delegate.allowsImmediateRender(snapshot, this.resolveInterceptionPromise)
const options = { resume: this.resolveInterceptionPromise }
const immediateRender = this.delegate.allowsImmediateRender(snapshot, options)
if (!immediateRender) await renderInterception

await this.renderSnapshot(renderer)
Expand Down

0 comments on commit aad4011

Please sign in to comment.