> For AI agents: the complete documentation index is available at /apps-plugin/docs/llms.txt, the full documentation bundle is available at /apps-plugin/docs/llms-full.txt.

# createCustomEvent

创建一组用于发送和监听自定义事件的函数。

## 支持版本

前端库版本不低于 `0.0.1`。

## 支持平台

<table>
<thead>
<tr><th>平台</th><th>支持情况</th></tr>
</thead>
<tbody>
<tr><td>Android</td><td>支持</td></tr>
<tr><td>iOS</td><td>支持</td></tr>
<tr><td>PC</td><td>支持</td></tr>
<tr><td>HarmonyOS</td><td>不支持</td></tr>
</tbody>
</table>

## 支持场景

<table>
<thead>
<tr><th>场景</th><th>支持情况</th></tr>
</thead>
<tbody>
<tr><td>页面</td><td>支持</td></tr>
<tr><td>卡片</td><td>支持</td></tr>
</tbody>
</table>

## 接入准备

### 前置条件

- 发送端与接收端需使用相同的事件名称和参数结构

## 使用说明

- 为事件名称添加业务命名空间，并在不再监听时调用注册函数返回的注销函数。

## 调用方式

### 同步 API

```typescript
createCustomEvent<Params extends object>(name: string, options?: CustomEventOptions<Params>): [CustomEventCaller<Params>, CustomEventHandlerRegistry<Params>]
```

## 入参

<table>
<thead>
<tr><th>名称</th><th>类型</th><th>必填</th><th>默认值</th><th>约束</th><th>说明</th></tr>
</thead>
<tbody>
<tr><td><code>name</code></td><td><code>string</code></td><td>是</td><td>-</td><td>-</td><td>事件名称。建议添加业务命名空间，避免与其他事件重名。</td></tr>
<tr><td><code>options.paramsTypeGuard</code></td><td><code>TypeGuard&lt;object, Params&gt;</code></td><td>否</td><td><code>-</code></td><td>-</td><td>接收端的参数类型守卫。返回 `false` 时会忽略当前事件，不调用已注册的处理函数。</td></tr>
</tbody>
</table>

## 调用示例

```typescript
import { createCustomEvent } from '@doubao-dev/framework/api';

interface TestEventParams {
  input: string;
}

const [emitEvent, onReceiveEvent] =
  createCustomEvent<TestEventParams>('example.testEvent');

const unregister = onReceiveEvent((params) => {
  console.log(params.input);
});

emitEvent({ input: 'test' });
unregister();
```

## 返回值

返回一个元组，第一项用于发送事件，第二项用于注册事件处理函数。
