|
| 1 | +<script context="module"> |
| 2 | + export { default as layout } from '../Components/Layout.svelte' |
| 3 | +</script> |
| 4 | + |
| 5 | +<script lang="ts"> |
| 6 | + import { router, useForm } from '@inertiajs/svelte' |
| 7 | + import TestGrid from '../Components/TestGrid.svelte' |
| 8 | + import TestGridItem from '../Components/TestGridItem.svelte' |
| 9 | +
|
| 10 | + export let appName |
| 11 | + export let jonathan: boolean |
| 12 | + export let taylor: boolean |
| 13 | + export let joe: boolean |
| 14 | +
|
| 15 | + let reloadCount = 0 |
| 16 | + const form = useForm({ jonathan, taylor, joe }) |
| 17 | +
|
| 18 | + $: console.log('watched reload count value', reloadCount) |
| 19 | +
|
| 20 | + function submit() { |
| 21 | + router.post( |
| 22 | + '/async/checkbox', |
| 23 | + { |
| 24 | + jonathan: $form.jonathan, |
| 25 | + taylor: $form.taylor, |
| 26 | + joe: $form.joe, |
| 27 | + }, |
| 28 | + { |
| 29 | + async: true, |
| 30 | + }, |
| 31 | + ) |
| 32 | + } |
| 33 | +
|
| 34 | + function simulateConflict() { |
| 35 | + router.reload({ |
| 36 | + only: ['sleep'], |
| 37 | + }) |
| 38 | + router.visit('/sleepy/2') |
| 39 | + } |
| 40 | +
|
| 41 | + function triggerVisitThenReload() { |
| 42 | + router.visit('/sleepy/1') |
| 43 | + router.reload({ |
| 44 | + only: ['sleep'], |
| 45 | + }) |
| 46 | + } |
| 47 | +
|
| 48 | + function triggerLongReload() { |
| 49 | + router.reload({ |
| 50 | + only: ['sleep'], |
| 51 | + onFinish() { |
| 52 | + console.log('finished reload') |
| 53 | + reloadCount++ |
| 54 | + console.log('incremented reload count') |
| 55 | + }, |
| 56 | + }) |
| 57 | + } |
| 58 | +
|
| 59 | + function triggerCancel() { |
| 60 | + router.post( |
| 61 | + '/sleepy/3', |
| 62 | + {}, |
| 63 | + { |
| 64 | + onCancelToken: (token) => { |
| 65 | + console.log('onCancelToken') |
| 66 | +
|
| 67 | + setTimeout(() => { |
| 68 | + console.log('CANCELLING!') |
| 69 | + token.cancel() |
| 70 | + }, 1000) |
| 71 | + }, |
| 72 | + }, |
| 73 | + ) |
| 74 | + } |
| 75 | +
|
| 76 | + function triggerCancelAfterFinish() { |
| 77 | + let cancelToken |
| 78 | +
|
| 79 | + router.post( |
| 80 | + '/sleepy/1', |
| 81 | + {}, |
| 82 | + { |
| 83 | + onCancelToken: (token) => { |
| 84 | + console.log('onCancelToken') |
| 85 | +
|
| 86 | + cancelToken = token |
| 87 | + }, |
| 88 | + onFinish: () => { |
| 89 | + console.log('onFinish') |
| 90 | + console.log('CANCELLING!') |
| 91 | + cancelToken.cancel() |
| 92 | + }, |
| 93 | + }, |
| 94 | + ) |
| 95 | + } |
| 96 | +</script> |
| 97 | + |
| 98 | +<svelte:head> |
| 99 | + <title>Async Request - {appName}</title> |
| 100 | +</svelte:head> |
| 101 | + |
| 102 | +<h1 class="text-3xl">Async Request</h1> |
| 103 | +<p class="mt-6">Reload Count: {reloadCount}</p> |
| 104 | + |
| 105 | +<TestGrid> |
| 106 | + <TestGridItem class="space-y-4"> |
| 107 | + <p>Trigger an async reload that takes a moment and immediately programmatically visit another page</p> |
| 108 | + <button class="rounded bg-green-600 px-4 py-2 text-white" on:click={simulateConflict}>Reload → Visit</button> |
| 109 | + </TestGridItem> |
| 110 | + |
| 111 | + <TestGridItem class="space-y-4"> |
| 112 | + <form on:change={submit}> |
| 113 | + <label class="block"> |
| 114 | + <input bind:checked={$form.jonathan} type="checkbox" class="mr-2" /> |
| 115 | + Jonathan |
| 116 | + </label> |
| 117 | + <label class="block"> |
| 118 | + <input bind:checked={$form.taylor} type="checkbox" class="mr-2" /> |
| 119 | + Taylor |
| 120 | + </label> |
| 121 | + <label class="block"> |
| 122 | + <input bind:checked={$form.joe} type="checkbox" class="mr-2" /> |
| 123 | + Joe |
| 124 | + </label> |
| 125 | + </form> |
| 126 | + <p>You can check these on and off and then navigate to another page and the requests should still complete.</p> |
| 127 | + <p>Toggling "Joe" on will cause a redirect to "Article", simulating an authorized action e.g.</p> |
| 128 | + </TestGridItem> |
| 129 | + |
| 130 | + <TestGridItem class="space-y-4"> |
| 131 | + <p>Trigger programmatic visit and an async reload one after another</p> |
| 132 | + |
| 133 | + <p>Reload should still happen but won't re-direct back to the reloaded component, we should respect the visit</p> |
| 134 | + |
| 135 | + <button on:click={triggerVisitThenReload} class="rounded bg-green-600 px-4 py-2 text-white">Visit → Reload</button> |
| 136 | + </TestGridItem> |
| 137 | + |
| 138 | + <TestGridItem class="space-y-4"> |
| 139 | + <p>Simply trigger a 4 second reload so you can navigate or do whatever you'd like during it.</p> |
| 140 | + <button on:click={triggerLongReload} class="rounded bg-green-600 px-4 py-2 text-white">Trigger Long Reload</button> |
| 141 | + </TestGridItem> |
| 142 | + |
| 143 | + <TestGridItem class="space-y-4"> |
| 144 | + <p>Trigger an automatic cancellation from the token.</p> |
| 145 | + <button on:click={triggerCancel} class="rounded bg-green-600 px-4 py-2 text-white">Trigger Cancel</button> |
| 146 | + </TestGridItem> |
| 147 | + |
| 148 | + <TestGridItem class="space-y-4"> |
| 149 | + <p>Trigger an automatic cancellation from the token after finishing request.</p> |
| 150 | + <button on:click={triggerCancelAfterFinish} class="rounded bg-green-600 px-4 py-2 text-white"> |
| 151 | + Trigger Cancel After Finish |
| 152 | + </button> |
| 153 | + </TestGridItem> |
| 154 | +</TestGrid> |
0 commit comments