# createMessageStore

Builds the message store for `services`, so a language loads once
however many parts of a page read it.

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

## Signature

```ts
import { createMessageStore } from "@mapcairn/core";
 
function createMessageStore(services: MessageStoreServices): ConfigurableMessageStore;
```

## Parameters

- `services` (`MessageStoreServices`)

## Returns

`ConfigurableMessageStore`

## Remarks

Without `Services.languages` the catalogue is `Services.messages`,
 always. With it, the language is resolved from the person's setting and
 the browser's languages (see [`resolveLanguage`](https://docs.mapcairn.com/api/core/resolveLanguage.md)) and then loaded.

Building the store does nothing: until its first subscription the state is
 `Services.messages` and nothing is read, loaded or subscribed to, so it is
 safe on a server and when a framework builds it twice. While anything
 listens, it follows the settings and preferences stores, and it releases
 both with the last listener. A later subscription picks up what changed
 meanwhile, and does not repeat a load that is in flight, has succeeded, or
 has failed with the setting and the browser's languages as they were.

Every render until a load finishes uses `Services.messages` or the last
 catalogue that loaded, so a slow load never blanks the text. A load that
 finishes after another language was asked for is dropped. One that fails
 keeps the previous catalogue and warns, names the language in
 [`MessageStoreState`](https://docs.mapcairn.com/api/core/MessageStoreState.md), and the language is requested again the
 next time the setting or the browser's languages change, even to the same
 tag, or when [`MessageStore`](https://docs.mapcairn.com/api/core/MessageStore.md) is called.

A listener that throws stops the listeners after it, and its error leaves
 the call that changed the state. A load it was being told about still runs.
 When the error leaves the first `subscribe`, that listener is removed and
 the services are released again, as if it had never subscribed.

## See also

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

Source: [packages/core/src/messages/messageStore.ts:198](https://github.com/MapCairn/mapcairn/blob/main/packages/core/src/messages/messageStore.ts#L198)
