-
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 #56 from mainmatter/dbeer/keep-latest
Add keep_latest
- Loading branch information
Showing
5 changed files
with
223 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Handler } from './types'; | ||
|
||
type Handle = ReturnType<Handler>; | ||
type Fn = Parameters<Handle>[0]; | ||
type Utils = Parameters<Handle>[1]; | ||
|
||
const handler = (({ max = 1 }: { max?: number } = { max: 1 }) => { | ||
let running = 0; | ||
let latest: | ||
| { | ||
fn: Fn; | ||
utils: Utils; | ||
} | ||
| undefined = undefined; | ||
|
||
const handle: Handle = async (fn: () => void, utils) => { | ||
if (running >= max) { | ||
latest?.utils.abort_controller.abort(); | ||
latest = { fn, utils }; | ||
return; | ||
} | ||
running++; | ||
try { | ||
fn(); | ||
await utils.promise; | ||
} catch { | ||
/** empty */ | ||
} | ||
running--; | ||
|
||
if (latest) { | ||
handle(latest.fn, latest.utils); | ||
latest = undefined; | ||
} | ||
}; | ||
return handle; | ||
}) satisfies Handler; | ||
|
||
export default handler; |
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,80 @@ | ||
<script lang="ts"> | ||
import { task, type SvelteConcurrencyUtils } from '../../task'; | ||
export let fn: ( | ||
args: number, | ||
utils: SvelteConcurrencyUtils, | ||
) => Promise<unknown> | AsyncGenerator<unknown, unknown, unknown>; | ||
export let return_value: (value: unknown) => void = () => {}; | ||
export let argument = 0; | ||
export let max = 1; | ||
const default_task = task.keepLatest(fn, { max }); | ||
const options_task = task(fn, { kind: 'keepLatest', max }); | ||
let latest_task_instance: ReturnType<typeof default_task.perform>; | ||
let latest_options_task_instance: ReturnType<typeof options_task.perform>; | ||
</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="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-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="perform-error" | ||
on:click={async () => { | ||
try { | ||
await default_task.perform(argument); | ||
} catch (e) { | ||
return_value({ | ||
error: e, | ||
store: default_task, | ||
}); | ||
} | ||
}}>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
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