startTask function

Automating Task Execution with startTask in doable.sh

The startTask function from the Doable AI SDK allows you to programmatically trigger operator tasks. This function is particularly useful when you want to automatically execute a task based on specific conditions or user interactions. Imagine a banner or button that says "Get Started" and you want to automatically start a task when the user clicks it.

Why Use startTask?

By leveraging startTask, you can ensure that tasks are executed without manual input. This is especially helpful in scenarios where you want the operator to start a task right after a page loads or when a particular event occurs.

Key Benefits:

  • Automates repetitive tasks without user intervention.
  • Ensures consistent execution of predefined workflows.
  • Easily integrates with application state and lifecycle methods.

Installation and Setup

Make sure you have the Doable AI SDK installed:

npm install @doable.sh/sdk  
pnpm i @doable.sh/sdk  
yarn add @doable.sh/sdk  

Next, import the function in your React component:

import { startTask } from '@doable.sh/sdk'  

Basic Usage of startTask

To trigger a task manually, use the function as follows:

startTask('create-operator-task')  

This line initiates the task identified by the given task ID.


Using startTask with useEffect

Often, you may want a task to execute automatically when a page loads or when a specific state changes. To achieve this, use startTask inside a useEffect hook.

Example:

useEffect(() => {  
    console.log("autoExecute", autoExecute);  
    if (autoExecute) {  
        startTask("create-operator-task");  
    }  
}, [autoExecute]);  

Explanation:

  • The useEffect hook ensures that the task starts automatically when the component loads or when the autoExecute state changes.
  • The console.log statement helps you debug whether the task is being triggered.
  • Replace create-operator-task with the ID of the task you want to execute.

Use Case: Auto-Execute on Page Load

Imagine a scenario where you want to automatically start a setup task when the dashboard page loads:

// src/app/(app)/(user)/dashboard/page.tsx
useEffect(() => {  
    const autoExecute = true;  
    if (autoExecute) {  
        startTask("create-operator-task");  
    }  
}, []);  

Advanced Usage: Conditional Task Execution

You can also trigger a task based on user preferences stored in state:

const [autoExecute, setAutoExecute] = useState(false);  
 
useEffect(() => {  
    if (autoExecute) {  
        startTask("user-onboarding-task");  
    }  
}, [autoExecute]);  

When the autoExecute state is set to true, the onboarding task will start automatically.


Best Practices

  1. Control Execution: Always check conditions before starting a task to avoid unintended behavior.
  2. Handle State Changes: Use useEffect to watch relevant state variables and trigger tasks accordingly.
  3. Provide Feedback: Log task initiation to track when and why a task is triggered.

Summary

The startTask function empowers you to trigger tasks programmatically, providing greater control and automation within your application. By combining it with useEffect, you can seamlessly integrate auto-execution features into your dashboard or any other page. Automate tasks efficiently and ensure a smoother user experience with startTask in your doable.sh-powered applications.