# Frontend Command Invocation Examples

## Basic Command Call
```typescript
import { invoke } from '@tauri-apps/api/core';

// Simple command
const greeting = await invoke('greet', { name: 'World' });
console.log(greeting); // "Hello, World! From Rust backend!"
```

## Command with Structured Payload
```typescript
interface ExamplePayload {
  data: string;
  count: number;
}

const result = await invoke<ExamplePayload>('process_data', {
  payload: {
    data: 'test',
    count: 42
  }
});
```

## Async Command
```typescript
const asyncResult = await invoke('async_operation', {
  input: 'test input'
});
console.log(asyncResult); // Returns after 1 second
```

## Error Handling
```typescript
try {
  await invoke('command_with_error', {
    should_fail: false
  });
} catch (error) {
  console.error('Command failed:', error);
}
```

## With Progress Tracking
```typescript
import { listen } from '@tauri-apps/api/event';

// Listen for progress events
const unlisten = await listen('progress', (event) => {
  const { current, total, message } = event.payload as {
    current: number;
    total: number;
    message: string;
  };
  console.log(`Progress: ${current}/${total} - ${message}`);
});

// Then call a long-running command
await invoke('long_running_task');

// Cleanup when done
unlisten();
```

## Type-Safe Commands
```typescript
// Define command types
interface GreetArgs {
  name: string;
}

// Use with generics
const result = await invoke<string>('greet', { name: 'Claude' } as GreetArgs);
```

## Batch Commands
```typescript
const results = await Promise.all([
  invoke('greet', { name: 'Alice' }),
  invoke('greet', { name: 'Bob' }),
  invoke('greet', { name: 'Charlie' })
]);
```

## With Window Reference
```typescript
import { getCurrentWindow } from '@tauri-apps/api/window';

const window = getCurrentWindow();
await invoke('window_command', {
  windowLabel: window.label
});
```
