# MemoryFeatureSelection

An in-memory [`FeatureSelection`](https://docs.mapcairn.com/api/core/FeatureSelection.md): the SDK's store and its fake in one
class, as [`MemorySettingsStore`](https://docs.mapcairn.com/api/core/MemorySettingsStore.md) is. A host that wants the set kept
elsewhere supplies its own store instead.

This is a class exported by `@mapcairn/core`, checked against version 0.2.0.

## Signature

```ts
import { MemoryFeatureSelection } from "@mapcairn/core";
 
class MemoryFeatureSelection implements FeatureSelection
```

## Description

How to script it: pass `limit` to change how many references it holds, and
call `replaceExternally` to change the set from outside, as a host does when
it hydrates a saved set or restores one after a failed save. Its
`getState` and `subscribe` are arrow properties, so they work unbound.

A change tells the listeners that were subscribed when it began, so a
listener that an earlier one unsubscribes during the same change is still
told once. That is deliberate, as in the layer catalogue's store, and
`useSyncExternalStore` tolerates it. A listener that throws stops the
listeners after it, and its error leaves the call that changed the set.

## Constructors

### (constructor)

```ts
constructor(options?: MemoryFeatureSelectionOptions);
```

Constructs a new instance of the `MemoryFeatureSelection` class

- `options?` (`MemoryFeatureSelectionOptions`)

## Properties

### getState

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

The references held, in order. The same array is returned until the set changes.

### limit

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

The most references it holds. An `add` or `replace` that would pass it changes nothing.

### subscribe

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

Calls the listener after the set changes. Returns the function that unsubscribes.

## Methods

### add

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

Appends the references it does not already hold. Changes nothing if that would pass the limit.

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

### clear

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

Empties the set.

### remove

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

Drops the references that are held. Others are ignored.

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

### replace

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

Makes the set exactly these references, without duplicates. Changes nothing if there are more than the limit.

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

### replaceExternally

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

Makes the set exactly `refs`, without their duplicates and whatever the
limit, and tells the subscribers once if that changed it. For a test to
script a change from outside the ui.

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

## See also

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

Source: [packages/core/src/fakes/MemoryFeatureSelection.ts:63](https://github.com/MapCairn/mapcairn/blob/main/packages/core/src/fakes/MemoryFeatureSelection.ts#L63)
