forked from hotwired/turbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
269fa24
commit a7d02f0
Showing
10 changed files
with
256 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { nextMicrotask } from "../../util" | ||
import { View, ViewDelegate } 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> { | ||
viewWillCacheSnapshot(): void | ||
} | ||
|
||
type PageViewRenderer = PageRenderer | ErrorRenderer | ||
|
||
export class PageView extends View<PageSnapshot, PageViewDelegate, PageViewRenderer> { | ||
readonly snapshotCache = new SnapshotCache(10) | ||
lastRenderedLocation = new URL(location.href) | ||
|
||
renderPage(snapshot: PageSnapshot, isPreview = false) { | ||
const renderer = new PageRenderer(this.snapshot, snapshot, isPreview) | ||
return this.render(renderer) | ||
} | ||
|
||
renderError(snapshot: PageSnapshot) { | ||
const renderer = new ErrorRenderer(this.snapshot, snapshot, false) | ||
this.render(renderer) | ||
} | ||
|
||
clearSnapshotCache() { | ||
this.snapshotCache.clear() | ||
} | ||
|
||
async cacheSnapshot() { | ||
if (this.shouldCacheSnapshot) { | ||
this.delegate.viewWillCacheSnapshot() | ||
const { snapshot, lastRenderedLocation: location } = this | ||
await nextMicrotask() | ||
this.snapshotCache.put(location, snapshot.clone()) | ||
} | ||
} | ||
|
||
getCachedSnapshotForLocation(location: URL) { | ||
return this.snapshotCache.get(location) | ||
} | ||
|
||
get snapshot() { | ||
return PageSnapshot.fromHTMLElement(this.element) | ||
} | ||
|
||
get shouldCacheSnapshot() { | ||
return this.snapshot.isCacheable | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.