# Recurring events

> Repeat events with RFC 5545 rules, skip dates with exdates, and decide whether a drag, resize or delete changes one occurrence or the whole series.

Give an event an RFC 5545 rule in `rrule` and it repeats on the timeline: a daily vehicle check, weekly planned maintenance, a monthly inspection. The rules are read by the same engine as the [Event Calendar](https://docs.birchui.com/calendar/recurrence.md), so a series means the same thing in both products.

## A recurring event

```ts title="maintenance.ts"
scheduler.addEvent({
  id: 'ppm-hillside',
  resourceId: 'maya',
  title: 'Planned maintenance: Hillside Care Home boilers',
  start: '2026-11-02T08:00',
  end: '2026-11-02T10:00',
  rrule: 'FREQ=WEEKLY;BYDAY=MO',
  exdates: ['2026-12-28T08:00'],
});
```

- `start` and `end` are the first occurrence; every occurrence has the same duration.
- `rrule` is written with or without the `RRULE:` prefix. `FREQ=DAILY|WEEKLY|MONTHLY|YEARLY`, `INTERVAL`, `COUNT`, `UNTIL`, `BYDAY` (including `1MO` and `-1FR`), `BYMONTHDAY`, `BYMONTH`, `BYSETPOS` and `WKST` are supported. Unsupported parts are ignored with one console warning; an invalid rule shows the event once, also with a warning.
- `exdates` skips occurrences. A date-time skips the occurrence that starts then; a plain date (`'2026-12-28'`) skips every occurrence on that day.
- Occurrences keep their wall-clock time in the scheduler's [`timeZone`](https://docs.birchui.com/scheduler/time-zones.md) across daylight-saving changes.
- Occurrences are generated only for the visible range, up to 5,000 per series, so a rule without an end costs nothing outside the view.

**`SchedulerEvent`** (interface from `@birchui/scheduler`, full reference: https://docs.birchui.com/api/scheduler.md)

```ts
interface SchedulerEvent
[key: string]: unknown
```

| Property | Type | Description |
| --- | --- | --- |
| `rrule?` | `string` |  |
| `exdates?` | `DateInput[]` |  |
| `recurringEventId?` | `string` |  |
| `recurrenceId?` | `string` |  |

## Occurrence ids

Each occurrence is rendered as an event whose id is the series id, `@`, and the occurrence's local start: `'ppm-hillside@2026-11-23T08:00:00'`. It carries `recurringEventId` (the series id) and `recurrenceId` (`'2026-11-23T08:00:00'`).

- `getEvents()` returns the series once, as you stored it.
- `getEventById(occurrenceId)` and `getEventsInRange()` also return occurrences.
- `updateEvent(occurrenceId, patch)` and `removeEvent(occurrenceId)` change a single occurrence, the same way a drag or delete does.

## This occurrence or the whole series

When a user drags, resizes or deletes an occurrence, `eventDrop`, `eventResize` and `eventRemove` fire **before** the change is applied, with `occurrence` and a mutable `scope` that starts as `'this'`:

**`SchedulerOccurrence`** (interface from `@birchui/scheduler`, full reference: https://docs.birchui.com/api/scheduler.md)

```ts
interface SchedulerOccurrence<TEvent extends SchedulerEvent = SchedulerEvent>
```

| Property | Type | Description |
| --- | --- | --- |
| `id` | `string` |  |
| `occurrenceId` | `string` |  |
| `recurrenceId` | `string` |  |
| `start` | `Date` |  |
| `end` | `Date` |  |
| `event` | `TEvent` |  |

| `scope` | Drag or resize | Delete |
| --- | --- | --- |
| `'this'` | Adds the occurrence to the series' `exdates` and adds a single event with the occurrence id, `recurringEventId` and `recurrenceId` at the new time | Adds the occurrence to `exdates` |
| `'all'` | Moves or resizes the whole series by the same amount; weekday rules (`BYDAY`) shift with it | Removes the series |

Set `scope` synchronously inside the listener:

```ts
const wholeSeries = document.querySelector<HTMLInputElement>('#whole-series')!;

scheduler.on('eventDrop', (change) => {
  if (change.occurrence) change.scope = wholeSeries.checked ? 'all' : 'this';
});

scheduler.on('eventRemove', (change) => {
  if (change.occurrence) change.scope = confirm('Delete every visit in this series?') ? 'all' : 'this';
});
```

> **Warning**
>
> The listener must decide synchronously. An `await` before setting `scope` is too late; the change is applied as soon as the listener returns. To ask with your own dialog, keep the default `'this'` and offer an "Apply to the whole series" action afterwards that updates the series with `updateEvent(seriesId, patch)`.

## Saving recurring changes

A change to one occurrence touches two records: the series (a new `exdates` entry) and the detached single event. Persist both. The `historyChange` event lists every record a user action changed, with `before` and `after`, which makes it the simplest place to save:

```ts
scheduler.on('historyChange', ({ action, changes }) => {
  if (action === 'clear') return;
  for (const { id, before, after } of changes) {
    if (after === null) void jobs.remove(id);
    else if (before === null) void jobs.create(after);
    else void jobs.update(id, after);
  }
});
```

`historyChange` fires unless history is turned off with `undoRedo: false`; see [Undo and redo](https://docs.birchui.com/scheduler/undo-redo.md).

## Related

- [Recurring jobs recipe](https://docs.birchui.com/recipes/scheduler-recurring-jobs.md)
- [Calendar recurrence](https://docs.birchui.com/calendar/recurrence.md) for the rule syntax in detail
- [Drag, drop, resize and create](https://docs.birchui.com/scheduler/interactions.md)
