# FeatureSelection

The set of features a person has picked, kept wherever the host likes: in
memory, on a server or in its own state. Optional on `Services`: a host that
supplies none offers no selection, and the ui must not require it.

This is an interface exported by `@mapcairn/core`, checked against version 0.2.0.

## Signature

```ts
import type { FeatureSelection } from "@mapcairn/core";
 
interface FeatureSelection
```

## Description

The port is synchronous so that `useSyncExternalStore` can read it, which
needs a snapshot of stable identity, so it has no loading or failed state. A
store that saves somewhere slow keeps a local copy, writes behind it and
calls its subscribers when the copy changes, when it hydrates, or when a
save fails and it restores the copy.

Rules for every implementation:
- `getState()` returns the same array until a change, in selection order.
 `getState` and `subscribe` work when taken off the object, as React reads
 them.
- A duplicate is ignored, within a call and against what is already held.
- A call that changes the set tells every subscriber once. A call that
 changes nothing tells none.
- A call that would take the total past `limit` changes nothing, so a batch
 is refused whole. A refusal is silent and the methods return nothing, so a
 caller who needs to tell a refused batch from one of duplicates compares
 `getState()` before and after.
- A store may hold more than `limit` references, for example when it
 hydrates a longer list. It shows them all and refuses every addition, and
 removing is always allowed.
- No method throws, provided its input is well typed (every reference an
 object with both ids) and every listener returns without throwing.

## Properties

### limit

```ts
readonly limit: number;
```

The most references the set holds.

## Methods

### add

```ts
add(refs: readonly FeatureRef[]): void;
```

Appends the references that are not yet selected, in order.

- `refs` (`readonly FeatureRef[]`)

### clear

```ts
clear(): void;
```

Empties the set.

### getState

```ts
getState(): readonly FeatureRef[];
```

The selected references in selection order. It is also the server snapshot.

### remove

```ts
remove(refs: readonly FeatureRef[]): void;
```

Removes the references that are selected, keeping the order of the rest.

- `refs` (`readonly FeatureRef[]`)

### replace

```ts
replace(refs: readonly FeatureRef[]): void;
```

Makes the set exactly `refs`, in that order, without their duplicates.
Refused whole when the result would pass `limit`. The same members in
another order are a change.

- `refs` (`readonly FeatureRef[]`)

### subscribe

```ts
subscribe(listener: () => void): () => void;
```

Calls `listener` after each change and returns how to stop.

- `listener` (`() => void`)

## See also

- [`FeatureRef`](https://docs.mapcairn.com/api/core/FeatureRef.md)

Source: [packages/core/src/ports/selection.ts:55](https://github.com/MapCairn/mapcairn/blob/main/packages/core/src/ports/selection.ts#L55)
