# Event Listening Examples

## Basic Event Listener
```typescript
import { listen } from '@tauri-apps/api/event';

const unlisten = await listen('my-event', (event) => {
  console.log('Received event:', event.payload);
});
```

## Typed Event Payload
```typescript
interface ProgressPayload {
  current: number;
  total: number;
  message: string;
}

const unlisten = await listen<ProgressPayload>('progress', (event) => {
  const { current, total, message } = event.payload;
  console.log(`Progress: ${current}/${total} - ${message}`);
});
```

## Multiple Event Listeners
```typescript
const listeners = [
  await listen('progress', (e) => console.log('Progress:', e.payload)),
  await listen('error', (e) => console.error('Error:', e.payload)),
  await listen('complete', (e) => console.log('Complete:', e.payload))
];

// Cleanup all
listeners.forEach(unlisten => unlisten());
```

## Event with Window Filter
```typescript
import { listen } from '@tauri-apps/api/event';
import { getCurrentWindow } from '@tauri-apps/api/window';

const window = getCurrentWindow();

await listen('window-event', (event) => {
  console.log('Window event:', event.payload);
}, {
  window: window.label
});
```

## One-Time Event
```typescript
import { once } from '@tauri-apps/api/event';

once('initial-data', (event) => {
  console.log('Initial data:', event.payload);
});
```

## Emitting from Frontend
```typescript
import { emit } from '@tauri-apps/api/event';

emit('frontend-event', { data: 'from frontend' });
```

## Real-Time Updates Pattern
```typescript
class TauriEventHandler {
  private listeners: Array<() => void> = [];

  async setup() {
    this.listeners.push(
      await listen('data-update', this.handleDataUpdate.bind(this)),
      await listen('progress', this.handleProgress.bind(this)),
      await listen('error', this.handleError.bind(this))
    );
  }

  private handleDataUpdate(event: Event) {
    // Handle data updates
  }

  private handleProgress(event: Event) {
    // Update progress bar
  }

  private handleError(event: Event) {
    // Show error notification
  }

  cleanup() {
    this.listeners.forEach(unlisten => unlisten());
  }
}

// Usage
const handler = new TauriEventHandler();
await handler.setup();
// ... later
handler.cleanup();
```
