Skip to content
Open
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
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,30 @@ async function getDownload() {

#### Start, pause, resume or cancel a download

The API uses discriminated unions for compile-time safety.
Only valid methods are available based on the download's state.

```ts
import { get } from 'tauri-plugin-download';
import { create, get, DownloadState } from 'tauri-plugin-download';

async function createAndStartDownload() {
const download = await create('file.zip', 'https://example.com/file.zip', '/path/to/file.zip');

const active = await download.start(); // Returns ActiveDownload
const paused = await active.pause(); // Returns PausedDownload
const resumed = await paused.resume(); // Returns ActiveDownload
}

async function getDownloadAndUpdate() {
const download = await get('file.zip');

download.start();
download.pause();
download.resume();
download.cancel();
if (download.state === DownloadState.CREATED) {
await download.start(); // TypeScript knows start() is available
} else if (download.state === DownloadState.IN_PROGRESS) {
await download.pause(); // TypeScript knows pause() is available
} else if (download.state === DownloadState.PAUSED) {
await download.resume(); // TypeScript knows resume() is available
}
}
```

Expand Down
31 changes: 20 additions & 11 deletions examples/tauri-app/src/DownloadView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="download-item">
<div class="item-header">
<h3 class="item-name">{{ download?.key }}</h3>
<div class="item-actions" v-if="!(isCancelled || isCompleted)">
<div class="item-actions" v-if="!isTerminal">
<button class="btn start-btn" type="button" @click="startDownload" v-if="canStart">Start</button>
<button class="btn cancel-btn" type="button" @click="cancelDownload" v-if="canCancel">Cancel</button>
<button class="btn pause-btn" type="button" @click="pauseDownload" v-if="canPause">Pause</button>
Expand Down Expand Up @@ -31,13 +31,13 @@ const props = defineProps({
let unlisten: UnlistenFn;

const download = ref<Download>(props.model),
isCancelled = computed(() => { return download.value.state === DownloadState.CANCELLED; }),
isCompleted = computed(() => { return download.value.state === DownloadState.COMPLETED; }),
canStart = computed(() => { return download.value?.state === DownloadState.CREATED; }),
// eslint-disable-next-line max-len
canCancel = computed(() => { return [ DownloadState.CREATED, DownloadState.IN_PROGRESS, DownloadState.PAUSED ].includes(download.value.state); }),
canPause = computed(() => { return download.value?.state === DownloadState.IN_PROGRESS; }),
canResume = computed(() => { return download.value?.state === DownloadState.PAUSED; });
isTerminal = computed(() => { return download.value.state === DownloadState.CANCELLED || download.value.state === DownloadState.COMPLETED; }),
canStart = computed(() => { return download.value.state === DownloadState.CREATED; }),
// eslint-disable-next-line max-len
canCancel = computed(() => { return download.value.state === DownloadState.CREATED || download.value.state === DownloadState.IN_PROGRESS || download.value.state === DownloadState.PAUSED; }),
canPause = computed(() => { return download.value.state === DownloadState.IN_PROGRESS; }),
canResume = computed(() => { return download.value.state === DownloadState.PAUSED; });

onMounted(async () => {
unlisten = await props.model.listen((updated: Download) => {
Expand All @@ -48,19 +48,28 @@ onMounted(async () => {
onUnmounted(() => { return unlisten(); });

async function startDownload() {
await props.model.start();
if (download.value.state === DownloadState.CREATED) {
await download.value.start();
}
}

async function cancelDownload() {
await props.model.cancel();
// eslint-disable-next-line max-len
if (download.value.state === DownloadState.CREATED || download.value.state === DownloadState.IN_PROGRESS || download.value.state === DownloadState.PAUSED) {
await download.value.cancel();
}
}

async function pauseDownload() {
await props.model.pause();
if (download.value.state === DownloadState.IN_PROGRESS) {
await download.value.pause();
}
}

async function resumeDownload() {
await props.model.resume();
if (download.value.state === DownloadState.PAUSED) {
await download.value.resume();
}
}
</script>

Expand Down
Loading