Skip to content

Commit

Permalink
Implicitlt complete interceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
domchristie committed Jul 8, 2021
1 parent caf9f2b commit 505c807
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/core/interception.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Intercept = (callback: (value: any) => void) => void
export type Intercept = (callback: () => Promise<any>) => void

export class Interception {
started = false
Expand All @@ -8,9 +8,10 @@ export class Interception {

constructor () {
this.completed = new Promise((resolve) => this.resolve = resolve)
this.intercept = (callback) => {
this.intercept = async (callback) => {
this.start()
callback(this.complete.bind(this))
const value = await callback()
this.complete(value)
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/tests/fixtures/pausable_rendering.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
<script src="/src/tests/fixtures/test.js"></script>
<script type="module">
addEventListener('turbo:before-render', function(event) {
event.detail.intercept(function (complete) {
if (confirm('Continue rendering?')) {
complete()
} else {
alert('Rendering aborted')
}
event.detail.intercept(function () {
return new Promise(function (resolve) {
if (confirm('Continue rendering?')) {
resolve()
} else {
alert('Rendering aborted')
}
})
})
})
</script>
Expand Down

0 comments on commit 505c807

Please sign in to comment.