Skip to content

Commit f97b0e7

Browse files
Merge pull request #156 from mainmatter/reference-docs
docs: start building the reference docs
2 parents 51c09ec + 714e775 commit f97b0e7

File tree

7 files changed

+1361
-23
lines changed

7 files changed

+1361
-23
lines changed

apps/docs/astro.config.mjs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,28 @@ export default defineConfig({
9090
},
9191
{
9292
label: 'Reference',
93-
autogenerate: {
94-
directory: 'reference',
95-
},
93+
items: [
94+
{
95+
label: 'Default task',
96+
link: '/reference/default',
97+
},
98+
{
99+
label: 'Drop task',
100+
link: '/reference/drop',
101+
},
102+
{
103+
label: 'Enqueue task',
104+
link: '/reference/enqueue',
105+
},
106+
{
107+
label: 'KeepLatest task',
108+
link: '/reference/keep-latest',
109+
},
110+
{
111+
label: 'Restart task',
112+
link: '/reference/restart',
113+
},
114+
],
96115
},
97116
],
98117
}),
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
---
2+
title: Default task
3+
description: the task function
4+
---
5+
6+
import { LinkCard, Tabs, TabItem, Aside } from '@astrojs/starlight/components';
7+
8+
This is the way to create a default task.
9+
10+
There's no concurrency management in this task and every `perform` will be executed immediately.
11+
12+
## Usage
13+
14+
To specify a default task, you can either use the dot notation or the options notation.
15+
16+
<Aside type="note">
17+
We call this tab "Dot notation" because you can define a default task with `task.default`. But it's
18+
much easier and recommended to just use `task`.
19+
</Aside>
20+
21+
<Tabs syncKey="notation">
22+
23+
<TabItem label="Dot notation">
24+
25+
```svelte
26+
<script lang="ts">
27+
import { task } from '@sheepdog/svelte';
28+
29+
const myTask = task(async () => {
30+
// your code
31+
});
32+
</script>
33+
```
34+
35+
</TabItem>
36+
37+
<TabItem label="Options notation">
38+
39+
```svelte
40+
<script lang="ts">
41+
import { task } from '@sheepdog/svelte';
42+
43+
const myTask = task(
44+
async () => {
45+
// your code
46+
},
47+
{ kind: 'default' },
48+
);
49+
</script>
50+
```
51+
52+
</TabItem>
53+
54+
</Tabs>
55+
56+
### The task store
57+
58+
The return value of the task function will be a svelte store where you can access state from all the
59+
instances running and eventually cancel them with `cancelAll`.
60+
61+
<LinkCard
62+
href="/getting-started/usage/#task-structure"
63+
title="Task structure"
64+
description="Take a look at the Task structure documentation in our Getting started"
65+
/>
66+
67+
### Passing props
68+
69+
While defining a task, if the function that you pass in has some arguments, those will be required
70+
by the `perform` function (and it will be strongly typed too).
71+
72+
<Aside type="caution">
73+
If you need to pass multiple props you should use an object because the second parameter will
74+
always be the [SheepdogOptions](#).
75+
</Aside>
76+
77+
<Tabs syncKey="notation">
78+
79+
<TabItem label="Dot notation">
80+
81+
```svelte
82+
<script lang="ts">
83+
import { task } from '@sheepdog/svelte';
84+
85+
const myTask = task(async (id: string) => {
86+
// your code
87+
});
88+
</script>
89+
90+
<button
91+
on:click={() => {
92+
myTask.perform('42');
93+
}}>perform</button
94+
>
95+
```
96+
97+
</TabItem>
98+
99+
<TabItem label="Options notation">
100+
101+
```svelte
102+
<script lang="ts">
103+
import { task } from '@sheepdog/svelte';
104+
105+
const myTask = task(
106+
async (id: string) => {
107+
// your code
108+
},
109+
{ kind: 'default' },
110+
);
111+
</script>
112+
113+
<button
114+
on:click={() => {
115+
myTask.perform('42');
116+
}}>perform</button
117+
>
118+
```
119+
120+
</TabItem>
121+
122+
</Tabs>
123+
124+
### Getting the return value
125+
126+
If you return something from your task you can access the return value by awaiting the `perform`
127+
function.
128+
129+
<Tabs syncKey="notation">
130+
131+
<TabItem label="Dot notation">
132+
133+
```svelte
134+
<script lang="ts">
135+
import { task } from '@sheepdog/svelte';
136+
137+
const myTask = task(async () => {
138+
return 42;
139+
});
140+
</script>
141+
142+
<button
143+
on:click={() => {
144+
const number = await myTask.perform();
145+
console.log(number); // 42
146+
}}>perform</button
147+
>
148+
```
149+
150+
</TabItem>
151+
152+
<TabItem label="Options notation">
153+
154+
```svelte
155+
<script lang="ts">
156+
import { task } from '@sheepdog/svelte';
157+
158+
const myTask = task(
159+
async () => {
160+
return 42;
161+
},
162+
{ kind: 'default' },
163+
);
164+
</script>
165+
166+
<button
167+
on:click={() => {
168+
const number = await myTask.perform();
169+
console.log(number); // 42
170+
}}>perform</button
171+
>
172+
```
173+
174+
</TabItem>
175+
176+
</Tabs>
177+
178+
### Getting the `TaskInstance`
179+
180+
If you don't await the `perform` function, then you'll get back the
181+
[task instance](/reference/task-instance) that you can use either to cancel it or to get its current
182+
state. The `TaskInstance` is also a svelte store and you can access the current value with
183+
`instance.get()`.
184+
185+
<Tabs syncKey="notation">
186+
187+
<TabItem label="Dot notation">
188+
189+
```svelte
190+
<script lang="ts">
191+
import { task } from '@sheepdog/svelte';
192+
193+
const myTask = task(async () => {
194+
// your code
195+
});
196+
</script>
197+
198+
<button
199+
on:click={() => {
200+
const lastRun = myTask.perform();
201+
console.log(lastRun.get()); // { isRunning: true, hasStarted: true, ... }
202+
lastRun.cancel();
203+
}}>perform</button
204+
>
205+
```
206+
207+
</TabItem>
208+
209+
<TabItem label="Options notation">
210+
211+
```svelte
212+
<script lang="ts">
213+
import { task } from '@sheepdog/svelte';
214+
215+
const myTask = task(
216+
async () => {
217+
// your code
218+
},
219+
{ kind: 'default' },
220+
);
221+
</script>
222+
223+
<button
224+
on:click={() => {
225+
lastRun = myTask.perform();
226+
console.log(lastRun.get()); // { isRunning: true, hasStarted: true, ... }
227+
lastRun.cancel();
228+
}}>perform</button
229+
>
230+
```
231+
232+
</TabItem>
233+
234+
</Tabs>
235+
236+
<Aside type="tip">
237+
Since this kind of task does not do any concurrency management there's no way to specify a `max`
238+
concurrency value.
239+
</Aside>

0 commit comments

Comments
 (0)