# Queue

> The `Queue` class is a generic FIFO (First-In, First-Out) linked list used internally by the `Events` class to process tracking events in order.

## Usage

```typescript
import { Queue } from '@segmentify/integration';

const queue = new Queue<string>();

queue.enqueue('first', 'second', 'third');
queue.getHead(); // 'first'
queue.getTail(); // 'third'
queue.length; // 3

queue.dequeue(); // removes 'first'
queue.dequeueBy(2); // removes next 2 items
```

## How Events Uses the Queue

When you call any event method (`PageView`, `Purchase`, etc.), the enriched payload is enqueued and a drain loop sends them sequentially:

- **Order preserved** — events sent in the order triggered
- **No duplication** — only one drain loop runs at a time
- **Resilient** — a failed event is skipped, the next one processes

## Custom Implementation

The `Events` constructor accepts any queue implementing `IQueue<T>`:

```typescript
import type { IQueue } from '@segmentify/integration';

class MyQueue<T> implements IQueue<T> {
  length = 0;
  enqueue(...args: T[]) {
    /* ... */
  }
  dequeue() {
    /* ... */
  }
  dequeueBy(count: number) {
    /* ... */
  }
  getHead(): T | undefined {
    /* ... */
  }
  getTail(): T | undefined {
    /* ... */
  }
}

const events = new Events(new MyQueue());
```

## See Also

- [Events](/05-events)
