Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout utility function #114

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ This simply gives you a task wrapper around your function. It will not handle an

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task(async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
});
</script>
Expand All @@ -59,11 +59,11 @@ OR

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task(
async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
},
{ kind: 'default' },
Expand All @@ -77,11 +77,11 @@ This will cancel the oldest instance of the task and start a new instance of it.

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task.restart(
async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
},
{ max: 3 },
Expand All @@ -93,11 +93,11 @@ OR

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task(
async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
},
{ kind: 'restart', max: 3 },
Expand All @@ -113,11 +113,11 @@ This will cancel any new instances of the task. You can also provide a `max` tha

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task.drop(
async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
},
{ max: 3 },
Expand All @@ -129,11 +129,11 @@ OR

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task(
async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
},
{ kind: 'drop', max: 3 },
Expand All @@ -149,11 +149,11 @@ This will add all task instances to a list and each task will be run in order. Y

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task.enqueue(
async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
},
{ max: 3 },
Expand All @@ -165,11 +165,11 @@ OR

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task(
async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
},
{ kind: 'enqueue', max: 3 },
Expand All @@ -185,11 +185,11 @@ This will run the initial tasks and then ensure that the very last task instance

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task.keepLatest(
async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
},
{ max: 3 },
Expand All @@ -201,11 +201,11 @@ OR

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task(
async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
},
{ kind: 'keepLatest', max: 3 },
Expand All @@ -221,10 +221,10 @@ As the return value from the task wrapper is a store, you can access it just lik

```svelte
<script>
import { task } from '@sheepdog/svelte';
import { task, timeout } from '@sheepdog/svelte';

const myTask = task(async (param: number) => {
await new Promise((r) => setTimeout(r, 2000));
await timeout(2000);
return param * 2;
});
</script>
Expand Down Expand Up @@ -340,6 +340,20 @@ parent_task.perform().catch((e) => {

In this example, our `fetch` call might throw an error or our task might be canceled. With `didCancel` we can check the error and ignore any cancelation errors, while doing something meaningful with any real errors that come from our task.

### timeout

The `timeout` function provides a convenient way to wait for a certain amount of milliseconds. Its implementation comes down to a promisified `setTimeout` call.

```ts
import { task, timeout } from '@sheepdog/svelte';

const myTask = task(async () => {
await timeout(500);
const res = await fetch('...');
return res;
});
```

## Contributing

### How to write async transform tests?
Expand Down
4 changes: 2 additions & 2 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Reexport your entry components here
import { didCancel } from './utils';
import { didCancel, timeout } from './utils';
import { task, CancelationError } from './task.js';
export type { Task, SheepdogUtils, TaskDerivedState } from './task.js';

export { task, CancelationError, didCancel };
export { task, CancelationError, didCancel, timeout };
Loading