# Controller (/reference/controller)

```ts
import { createUpwell, UpwellController, type UpwellOptions, type UpwellState } from 'upwell';
```

The controller runs Upwell end to end. It reads the device, fetches the policy, asks the store and the
OTA tool, runs `decide()`, and performs the resulting update. It doesn't depend on React;
[`UpwellProvider`](/docs/reference/react#upwellprovider) wraps it.

```ts
const upwell = createUpwell({ policy: 'https://cdn.example.com/upwell/policy.json' });
```

## UpwellOptions [#upwelloptions]

<TypeTable
  type={{
    policy: {
      type: 'PolicySource | string',
      description:
        'Policy URL, or a source. Optional: without one, Upwell still applies OTA updates and recommends store updates that Google Play or the App Store report, but can\'t force updates or detect stranded installs.',
      typeDescriptionLink: '/docs/guides/custom-sources-and-adapters#policy-sources',
    },
    releases: {
      type: 'ReleasesSource | string',
      description: 'releases.json URL or source, for "What\'s new" and the "Updated" sheet.',
    },
    ota: {
      type: 'OtaAdapter',
      description: 'OTA tool adapter, e.g. expoUpdatesAdapter(). Without one, only store updates are handled.',
    },
    platform: {
      type: 'UpwellPlatform | null',
      default: 'the native module',
      description: 'Device access. Pass a SimulatedPlatform in tests. null uses a platform that always fails.',
    },
    engine: {
      type: 'Partial<EngineConfig>',
      default: '{ snoozeHours: 24, maxPrompts: 3, promptPeriodHours: 168 }',
      description: 'Snooze and prompt-cap settings.',
      typeDescriptionLink: '/docs/concepts/decision-engine#engine-configuration',
    },
    minCheckIntervalMs: {
      type: 'number',
      default: '60000',
      description: 'Minimum time between non-forced checks (foreground checks).',
    },
    timeoutMs: {
      type: 'number',
      default: '10000',
      description: 'Network timeout for the policy and releases.',
    },
    autoDownloadOta: {
      type: 'boolean',
      default: 'true',
      description: 'Download OTA updates as soon as they are found.',
    },
    debug: {
      type: 'boolean',
      default: 'false',
      description: 'Log every decision\'s trace with console.log, and errors with console.warn.',
    },
    onDecision: {
      type: '(explanation: { decision, trace }) => void',
      description: 'Called every time the engine runs.',
    },
    onError: {
      type: '(error: unknown, context: string) => void',
      description: 'Called on errors other than policy fetches.',
      typeDescriptionLink: '/docs/guides/debugging-and-analytics#error-contexts',
    },
    onEvent: {
      type: '(event: UpwellEvent) => void',
      description:
        'Called with every update event: checks, store prompts and what the user did with them, store flows, OTA downloads and restarts, errors. For your analytics; Upwell sends nothing itself. If it throws or rejects, the error goes to onError with context "onEvent" and updating carries on.',
      typeDescriptionLink: '/docs/guides/analytics-events',
    },
    now: {
      type: '() => number',
      default: 'Date.now',
      description: 'Clock, for tests.',
    },
  }}
/>

### Events [#events]

```ts
const upwell = createUpwell({
  policy: url,
  onEvent: (event) => posthog.capture(`upwell_${event.type}`, event),
});
```

The controller emits the events, so they're the same with the UI kit, your own UI, or no UI.
`event.type` is one of `check`, `prompt_shown`, `prompt_action`, `store_flow`, `ota_downloaded`,
`ota_applied`, `updated_seen` and `error`; every event also carries the app version, build,
runtime, running update, install source, SDK version, timestamp and the random per-install
`installationId`. See [Send update events to your analytics](/docs/guides/analytics-events) for
when each one fires, and the [`UpwellEvent`](/docs/reference/types#upwellevent) type.

## Methods [#methods]

| Method | |
| --- | --- |
| `check(opts?)` | Re-evaluates everything. Concurrent calls share one run. Without `{ force: true }`, a check within `minCheckIntervalMs` of the last one returns the current decision |
| `startUpdate()` | Does what the current decision calls for: the store flow, installing a downloaded update, or restarting into an OTA update |
| `startStoreUpdate(flow?)` | Starts a store flow (default: the decision's flow). Falls back to the store link if a Play flow fails |
| `completeStoreUpdate()` | Installs a downloaded flexible update. The app restarts |
| `applyOta()` | Restarts into the downloaded OTA update |
| `dismiss()` | "Later" for the current decision. Reported as a `prompt_action` event for store prompts |
| `markUpdateSeen()` | Clears `justUpdated`. Reported as an `updated_seen` event |
| `explain()` | `{ decision, trace }` for the current decision |
| `handleAppStateChange(next)` | Call with `AppState` changes when you don't use the provider. `'active'` checks; `'background'` installs a postponed flexible update |
| `simulate(scenario, options?)` | Jumps to a [simulated state](/docs/guides/testing-and-simulation). `null` returns to the real device |
| `getState()` | The current `UpwellState` |
| `subscribe(listener)` | Calls `listener(state)` on every change. Returns an unsubscribe function |
| `dispose()` | Stops listening to the store and drops all listeners |

## UpwellState [#upwellstate]

<TypeTable
  type={{
    status: { type: '"idle" | "checking" | "ready"', description: 'Whether a check has run, or is running.' },
    decision: { type: 'Decision', description: 'The current decision.', typeDescriptionLink: '/docs/concepts/decisions' },
    trace: { type: 'string[]', description: 'Why the engine decided what it did.' },
    dismissed: { type: 'boolean', description: 'The user dismissed the current decision this session.' },
    busy: { type: 'boolean', description: 'A store flow or an OTA restart is in progress.' },
    installed: { type: 'InstalledInfo | null', description: 'What the device reported.' },
    policy: { type: 'Policy | null', description: 'The policy in use (fresh, or last known good). `null` when none is configured.' },
    policyError: { type: 'string | null', description: 'Why the latest fetch failed or was rejected.' },
    policyWarnings: { type: 'string[]', description: 'Validator warnings, e.g. unknown keys.' },
    store: { type: 'StoreInfo | null', description: 'Play or App Store info.' },
    ota: { type: '{ status, error? }', description: 'The OTA status: idle, checking, available, downloading, ready, upToDate, disabled or error.' },
    releases: { type: 'ReleasesDocument | null', description: 'The releases.json in use.' },
    currentRelease: { type: 'Release | null', description: 'The entry for the running OTA update.' },
    history: { type: 'Release[]', description: 'Entries for the running runtime, newest first.' },
    justUpdated: { type: 'Release | null', description: 'Set after an OTA update was applied, until markUpdateSeen().' },
    lastCheckedAt: { type: 'number | null', description: 'Epoch ms of the last completed check.' },
    simulation: { type: 'SimulationScenario | null', description: 'The active simulation scenario.' },
  }}
/>
