Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpdoyle committed Sep 14, 2022
1 parent 98cde44 commit b3f08e1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
27 changes: 12 additions & 15 deletions src/core/drive/page_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ import { Renderer } from "../renderer"
import { PageSnapshot } from "./page_snapshot"
import { ReloadReason } from "../native/browser_adapter"
import { activateScriptElement, waitForLoad, getBodyElementId } from "../../util"
import { nextEventLoopTick } from "../../util"

export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
static renderElement(currentElement: HTMLBodyElement, newElement: HTMLBodyElement) {
if (document.body && newElement instanceof HTMLBodyElement) {
const currentBody = PageRenderer.getBodyElement(currentElement) || currentElement
const newBody = PageRenderer.getBodyElement(newElement) || newElement
static renderElement(currentBody: HTMLBodyElement, newBody: HTMLBodyElement) {
if (document.body && newBody instanceof HTMLBodyElement) {
const currentElement = PageRenderer.getRenderedElement(currentBody) || currentBody
const newElement = PageRenderer.getRenderedElement(newBody) || newBody

currentBody.replaceWith(newBody)
currentElement.replaceWith(newElement)
} else {
document.documentElement.appendChild(newElement)
document.documentElement.appendChild(newBody)
}
}

get shouldRender() {
return this.newSnapshot.isVisitable && this.trackedElementsAreIdentical && this.bodyElementMatches
return this.newSnapshot.isVisitable && this.trackedElementsAreIdentical && this.renderedElementMatches
}

get reloadReason(): ReloadReason {
Expand All @@ -33,9 +32,9 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
}
}

if (!this.bodyElementMatches) {
if (!this.renderedElementMatches) {
return {
reason: "body_element_mismatch",
reason: "rendered_element_mismatch",
}
}
}
Expand All @@ -45,8 +44,6 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
}

async render() {
await nextEventLoopTick()

if (this.willRender) {
this.replaceBody()
}
Expand Down Expand Up @@ -90,8 +87,8 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
return this.currentHeadSnapshot.trackedElementSignature == this.newHeadSnapshot.trackedElementSignature
}

get bodyElementMatches() {
return PageRenderer.getBodyElement(this.newElement) !== null
get renderedElementMatches() {
return PageRenderer.getRenderedElement(this.newElement) !== null
}

static get bodySelector() {
Expand All @@ -100,7 +97,7 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
return bodyId ? `#${bodyId}` : "body"
}

static getBodyElement(element: HTMLElement): HTMLElement | null {
static getRenderedElement(element: HTMLElement): HTMLElement | null {
if (element.matches(this.bodySelector)) {
return element
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/core/drive/page_view.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { nextEventLoopTick } from "../../util"
import { View, ViewDelegate, ViewRenderOptions } from "../view"
import { ErrorRenderer } from "./error_renderer"
import { PageRenderer } from "./page_renderer"
Expand Down Expand Up @@ -39,11 +38,10 @@ export class PageView extends View<HTMLBodyElement, PageSnapshot, PageViewRender
this.snapshotCache.clear()
}

async cacheSnapshot() {
cacheSnapshot() {
if (this.shouldCacheSnapshot) {
this.delegate.viewWillCacheSnapshot()
const { snapshot, lastRenderedLocation: location } = this
await nextEventLoopTick()
const cachedSnapshot = snapshot.clone()
this.snapshotCache.put(location, cachedSnapshot)
return cachedSnapshot
Expand Down
3 changes: 2 additions & 1 deletion src/core/drive/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ export class Visit implements FetchRequestDelegate {

cacheSnapshot() {
if (!this.snapshotCached) {
this.view.cacheSnapshot().then((snapshot) => snapshot && this.visitCachedSnapshot(snapshot))
const snapshot = this.view.cacheSnapshot()
if (snapshot) this.visitCachedSnapshot(snapshot)
this.snapshotCached = true
}
}
Expand Down

0 comments on commit b3f08e1

Please sign in to comment.