-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from mainmatter/link-cancel-only-instance
feat: link only cancel the instance of the task that was linked
- Loading branch information
Showing
5 changed files
with
350 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<script lang="ts"> | ||
import { task, type Task } from '../../../task'; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export let parent: Task<number, any>; | ||
export let kind: string; | ||
const default_task = task.default(async (_, { link }) => { | ||
await link(parent).perform(0); | ||
}); | ||
const options_task = task( | ||
async (_, { link }) => { | ||
await link(parent).perform(0); | ||
}, | ||
{ kind: 'default' }, | ||
); | ||
</script> | ||
|
||
<button | ||
data-testid="child-component-perform-{kind}" | ||
on:click={async () => { | ||
if (kind === 'default') { | ||
default_task.perform(); | ||
} else { | ||
options_task.perform(); | ||
} | ||
}}>perform</button | ||
> |
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,135 @@ | ||
<script lang="ts"> | ||
import { task, type SvelteConcurrencyUtils } from '../../../task'; | ||
import Child from './child.svelte'; | ||
export let fn: ( | ||
args: number, | ||
utils: SvelteConcurrencyUtils, | ||
) => Promise<unknown> | AsyncGenerator<unknown, unknown, unknown>; | ||
export let return_value: (value: unknown) => void = () => {}; | ||
export let argument = 0; | ||
const default_task = task.default(fn); | ||
const options_task = task(fn, { kind: 'default' }); | ||
const default_task_child = task.default(async (_, { link }) => { | ||
await link(default_task).perform(argument); | ||
}); | ||
const default_options_task_child = task.default(async (_, { link }) => { | ||
await link(options_task).perform(argument); | ||
}); | ||
let latest_task_instance: ReturnType<typeof default_task.perform>; | ||
let latest_task_child_instance: ReturnType<typeof default_task_child.perform>; | ||
let latest_options_task_child_instance: ReturnType<typeof default_options_task_child.perform>; | ||
let latest_options_task_instance: ReturnType<typeof options_task.perform>; | ||
let mounted = true; | ||
</script> | ||
|
||
<button | ||
data-testid="perform-default" | ||
on:click={async () => { | ||
latest_task_instance = default_task.perform(argument); | ||
return_value(await latest_task_instance); | ||
}}>perform</button | ||
> | ||
|
||
<button | ||
data-testid="perform-options" | ||
on:click={async () => { | ||
latest_options_task_instance = options_task.perform(argument); | ||
return_value(await latest_options_task_instance); | ||
}}>perform options</button | ||
> | ||
|
||
<button | ||
data-testid="perform-child-default" | ||
on:click={async () => { | ||
latest_task_child_instance = default_task_child.perform(); | ||
}}>perform child</button | ||
> | ||
|
||
<button | ||
data-testid="perform-child-options" | ||
on:click={async () => { | ||
latest_options_task_child_instance = default_options_task_child.perform(); | ||
}}>perform child options</button | ||
> | ||
|
||
<button | ||
data-testid="cancel-default" | ||
on:click={() => { | ||
default_task.cancelAll(); | ||
}}>cancel</button | ||
> | ||
|
||
<button | ||
data-testid="cancel-options" | ||
on:click={() => { | ||
options_task.cancelAll(); | ||
}}>cancel options</button | ||
> | ||
|
||
<button | ||
data-testid="cancel-child-default" | ||
on:click={() => { | ||
default_task_child.cancelAll(); | ||
}}>cancel child</button | ||
> | ||
|
||
<button | ||
data-testid="cancel-child-options" | ||
on:click={() => { | ||
default_options_task_child.cancelAll(); | ||
}}>cancel child options</button | ||
> | ||
|
||
<button | ||
data-testid="cancel-default-last" | ||
on:click={() => { | ||
if (latest_task_instance) { | ||
latest_task_instance.cancel(); | ||
} | ||
}}>cancel last instance</button | ||
> | ||
|
||
<button | ||
data-testid="cancel-options-last" | ||
on:click={() => { | ||
if (latest_options_task_instance) { | ||
latest_options_task_instance.cancel(); | ||
} | ||
}}>cancel last options instance</button | ||
> | ||
|
||
<button | ||
data-testid="cancel-child-default-last" | ||
on:click={() => { | ||
if (latest_task_child_instance) { | ||
latest_task_child_instance.cancel(); | ||
} | ||
}}>cancel last child instance</button | ||
> | ||
|
||
<button | ||
data-testid="cancel-child-options-last" | ||
on:click={() => { | ||
if (latest_options_task_child_instance) { | ||
latest_options_task_child_instance.cancel(); | ||
} | ||
}}>cancel last child options instance</button | ||
> | ||
|
||
<button | ||
data-testid="unmount-child-component" | ||
on:click={() => { | ||
mounted = !mounted; | ||
}}>unmount child component</button | ||
> | ||
{#if mounted} | ||
<Child parent={default_task} kind="default" /> | ||
<Child parent={options_task} kind="options" /> | ||
{/if} |
Oops, something went wrong.