# @xmcl/task

> A task interface using tree structure.

Latest version **4.1.1** (published 2025-01-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @xmcl/task
pnpm add @xmcl/task
yarn add @xmcl/task
bun add @xmcl/task
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 4.1.1 |
| Published | 2025-01-10 |
| First published | 2019-07-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 69 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 220 |
| Author | cijhn@hotmail.com |
| Maintainers | ci010 |

## Links

- npm: https://www.npmjs.com/package/@xmcl/task
- Repository: https://github.com/Voxelum/minecraft-launcher-core-node
- Homepage: https://github.com/Voxelum/minecraft-launcher-core-node#readme
- Issues: https://github.com/Voxelum/minecraft-launcher-core-node/issues
- npm.io page: https://npm.io/package/@xmcl/task

## Recent versions

- 4.1.1 (latest) — 2025-01-10
- 4.1.0 — 2023-10-22
- 4.0.6 — 2023-06-11
- 4.0.5 — 2023-04-24
- 4.0.4 — 2023-04-13
- 4.0.3 — 2022-07-06
- 4.0.2 — 2021-12-04
- 4.0.1 — 2021-10-21
- 4.0.0 — 2021-10-20
- 3.0.0 — 2020-12-20
- 2.2.0 — 2020-05-30
- 2.1.8 — 2020-05-14
- 2.1.7 — 2020-04-25
- 2.1.6 — 2020-04-14
- 2.1.5 — 2020-03-18
- … 21 more at https://npm.io/package/@xmcl/task/versions

## README

# Task Module

[![npm version](https://img.shields.io/npm/v/@xmcl/task.svg)](https://www.npmjs.com/package/@xmcl/task)
[![Downloads](https://img.shields.io/npm/dm/@xmcl/task.svg)](https://npmjs.com/@xmcl/task)
[![Install size](https://packagephobia.now.sh/badge?p=@xmcl/task)](https://packagephobia.now.sh/result?p=@xmcl/task)
[![npm](https://img.shields.io/npm/l/@xmcl/minecraft-launcher-core.svg)](https://github.com/voxelum/minecraft-launcher-core-node/blob/master/LICENSE)
[![Build Status](https://github.com/voxelum/minecraft-launcher-core-node/workflows/Build/badge.svg)](https://github.com/Voxelum/minecraft-launcher-core-node/actions?query=workflow%3ABuild)

This is a helper module for Minecraft Launcher. See the github home page for more information.

## Usage

### Progress Moniting

You can use `@xmcl/task` model to track the progress of a task. *In the launcher, they are majorly download task.*

This module implements a basic object model for task with progress. The task can be paused or cancelled.

```ts
    import { Task, TaskBase, task } from "@xmcl/task";

    class ATask extends TaskBase {
        // implement a task
    }

    class BTask extends TaskBase {
        // implement a task
    }

    // suppose you have such task
    const myTask = task("hello", function() {
        await this.yield(new ATask().setName("world"));
        await this.yield(new BTask().setName("xmcl"));
    });

    // start a task
    const result = await task.startAndWait({
        onStart(task: Task<any>) {
            // the task path is the task name joined by dot (.)
            const path = task.path;
            console.log(`${path} started!`);
        },
        onUpdate(task: Task<any>, chunkSize: number) {
            // a task update
        },
        onFailed(task: Task<any>, error: any) {
            // on a task fail
        },
        onSucceed(task: Task<any>, result: any) {
            // on task success
            const path = task.path;
            console.log(`${path} ended!`);
        },
        // on task is paused/resumed/cancelled
        onPaused(task: Task<any>) { },
        onResumed(task: Task<any>) { },
        onCancelled(task: Task<any>) { },
    });
    // the result will print like
    // hello started!
    // hello.world started!
    // hello.world ended!
    // hello.xmcl started!
    // hello.xmcl ended!
    // hello ended!
```

---
_Source: https://npm.io/package/@xmcl/task · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
