# Dreamcomposer PRE

A library to control a [Dreamcomposer] iFrame.

## Getting Started

### Initialize Dreamcomposer

Use the `init` function to obtain a `DcController` instance.

```html
<div id="my-dc" />

<script>
  import * as dc from "@dreamcomposer/pre";

  const dcController = dc.init("my-dc", { accessToken: "<yourToken>" });
  dc.setGridVisibility(dcController, true);
</script>
```

### Handle engine failures

The engine runs in an iframe, so its failures are not visible to the embedding
page on their own. Register an `error` listener to replace your loading
indicator with an error state:

```js
dc.on(dcController, "error", (event) => {
  // event.reason: "unreachable" | "not-ready" | "engine-error"
  // event.message is technical and meant for logs, not for users
  showEngineError();
});
```

The event is dispatched once per load, in these cases:

- `unreachable`: the engine page did not start within `engineAliveTimeout`
  (default 10 seconds) after `load`, e.g. because the engine server is down.
- `not-ready`: the page started, but the engine did not become ready within
  `engineReadyTimeout` (default 2 minutes, includes downloading assets).
- `engine-error`: the engine reported a fatal error, e.g. missing WebGL support.

After a failure, messages that wait for an acknowledgement from the engine are
rejected with the error event, and so is every later one. The failure sticks
until `unload`, even if a slow engine becomes ready after the ready timeout;
`unload` followed by `load` starts a new attempt. A loading screen set with
`setLoadingScreen` is hidden when the error event is dispatched.
Both timeouts can be set in the options of `init`.

The engine announces its start with an `engine-alive` message. Engines that
predate it only send `engine-ready`, which also counts; with such an engine the
alive timeout fires if becoming ready takes longer than that timeout.

## License

This work is under exclusive copyright. You are not allowed to copy, distribute,
or modify this work.

[dreamcomposer]: https://dreamcomposer.de/
