---
title: "createRegistry"
description: "Create an id-keyed instance registry with optional reactive storage and identity-checked disposal handles."
canonical_url: "https://core.mhaibaraai.cn/en/docs/helpers/create-registry"
---
# createRegistry

> Create an id-keyed instance registry with optional reactive storage and identity-checked disposal handles.

## Usage

`createRegistry` creates a registry that indexes instances by id, suited to accessing instances across component trees or routes.

```ts
import { createRegistry } from '@movk/core'

const registry = createRegistry<MapInstance>()

registry.register('main', map)
registry.get('main') // MapInstance
registry.has('main') // true
registry.unregister('main') // true
```

### Disposal Handles

The handle returned by `register` is identity-checked: it only deletes the entry while the stored value is still the one it registered. When instances hand over (the new one registers before the old one unmounts), the stale handle cannot remove the new registration.

```ts
const disposeFirst = registry.register('main', first)
registry.register('main', second)

disposeFirst()
registry.get('main') // second, untouched by the stale handle
```

Inside a component, hand the handle to the effect scope:

```ts
import { onScopeDispose } from 'vue'

const dispose = registry.register(id, instance)
onScopeDispose(dispose)
```

### Reactive Mode

With `reactive` enabled the registry stores entries in a `shallowReactive` map, so `computed` and `watch` observe registrations and removals.

```ts
const registry = createRegistry<DrawContext>({
  reactive: true,
  onDuplicate: id => console.warn(`"${id}" is already registered; the latest one takes over`),
})

const draw = computed(() => registry.get(mapId))
const count = computed(() => registry.size)
```

`onDuplicate` is a warning hook only; it does not change the "latest one wins" behavior.

## API

### `createRegistry<T>(options?)`

Create a registry that indexes instances by id.

### Parameters

**options** (`RegistryOptions`): Registry behavior options.

### Returns

**returns** (`Registry<T>`): The registry instance.

### RegistryOptions

**reactive** (`boolean`): Whether to store entries in a shallowReactive map so computed/watch observe changes. Defaults to false.

**onDuplicate** (`(id: string) => void`): Called when the same id is registered again, for warning purposes.

### Registry

**register** (`(id: string, value: T) => () => void`): Register an instance and return an identity-checked disposal handle.

**unregister** (`(id: string) => boolean`): Remove an entry by id; returns whether an entry was actually deleted.

**get** (`(id: string) => T | undefined`): Read the instance registered under an id.

**has** (`(id: string) => boolean`): Whether an id is registered.

**keys** (`() => string[]`): Registered ids, in registration order.

**clear** (`() => void`): Remove every entry.

**size** (`number`): Number of registered entries.

## Changelog

See commit history for [src/helpers/createRegistry.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/helpers/createRegistry.ts).

---

- [GitHub](https://github.com/mhaibaraai/movk-core/blob/main/src/helpers/createRegistry.ts)


## Sitemap

See the full [sitemap](https://core.mhaibaraai.cn/sitemap.md) for all pages.
