# call-service

> Call-Service 是一个基于 xstate 的通话状态机.

Latest version **1.0.7** (published 2022-04-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install call-service
pnpm add call-service
yarn add call-service
bun add call-service
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2022-04-24 |
| First published | 2022-01-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 20.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | jshawn |

## Links

- npm: https://www.npmjs.com/package/call-service
- Repository: https://git.code.tencent.com/ethanczhang/call-service
- npm.io page: https://npm.io/package/call-service

## Dependencies (1)

- [xstate](https://npm.io/package/xstate.md) ^4.27.0

## Recent versions

- 1.0.7 (latest) — 2022-04-24
- 1.0.6 — 2022-04-24
- 1.0.5 — 2022-02-11
- 1.0.4 — 2022-02-11
- 1.0.3 — 2022-01-14
- 1.0.2 — 2022-01-13
- 1.0.1 — 2022-01-13
- 1.0.0 — 2022-01-11

## README

# call-service

Call-Service 是一个基于 xstate 的通话状态机.

## 安装

```bash
yarn add call-service
```

## 状态机

![call state flow:https://stately.ai/viz/ef5ef1d2-f8ab-4e94-883f-e1348edf246c](./assets/call-state-machine.png)

## 用法

### Context

Context 在 xState 中的定位为 extended states, 即和状态相关的拓展字段。在这个状态机中，存在这样几个字段，用于记录通话的信息

```ts
interface CallContext {
  chatId: string;
  customerInfo: string;
}
```

1. chatId: 当前通话的 id,触发 onCall 必须传入，创建通话 id 建议使用 nanoid
2. customerInfo： 当前通话的客户信息，可以在通话 onCall 来传入，也可以使用 onDataUpdate 来更新，但是 onDataUpdate 只会在状态为 ringing 和 calling 的时候才会生效，在 idle 状态时传入的 onDataUpdate 会被忽略。在 onCall 触发的时候，也会先清空 customerInfo

```ts
const first = callService.send({
  type: 'onCall',
  chatId: nanoid(8),
  customerInfo: customerInfo1,
});
const state = callService.send({
  type: 'onDataUpdate',
  customerInfo: customerInfo2,
});
```

### 如何获取当前的 state 或 context

```ts
import { CallState } from 'call-service/lib/callStateMachine';
// 获取当前的state值
callService.state.value;
// 获取当前context的值
callService.state.context;
// 判断当前的状态是否匹配某个状态
callService.state.matches(CallState.Calling);
```

### 发送 call event

```ts
import { nanoid } from 'nanoid';
import { CallState, CallTalkBeginMode, defaultCallContext } from 'call-service/lib/callStateMachine';
import createCallService from 'call-service';

const callService = createCallService().onTransition((state, event) => {
  if (state.changed) {
  }
});
callService.start();

const first = callService.send({
  type: 'onCall',
  chatId: nanoid(8),
  customerInfo: customerInfo1,
});

const second = callService.send({
  type: 'onTalkBegin',
});

const third = callService.send({
  type: 'onClosed',
});
```

### callMonitor

callMonitor 用于生成通话记录，每一通电话都会通过 onCallFinished 返回，用法如下

```ts
const callService = createCallService();
const handleCallFinished = (callInstance: CallInstance) => {
  console.log({
    startTime: formatTime(callInstance.startTime),
    // 可以通过判断talkBeginTime是否为0，确定通话是否被接通
    talkBeginTime: formatTime(callInstance.talkBeginTime),
    endTime: formatTime(callInstance.endTime),
    chatId: callInstance.chatId,
    customerInfo: callInstance.customerInfo,
  });
  mark();
};
// 注册回调
const callMonitor = createCallMonitor(callService).onCallFinished(handleCallFinished);
callService.start();
const first = callService.send({
  type: 'onCall',
  chatId: nanoid(8),
  customerInfo: customerInfo1,
});

const second = callService.send({
  type: 'onTalkBegin',
});

const third = callService.send({
  type: 'onClosed',
});
// 移除回调
callMonitor.off(handleCallFinished);
```

### 回调

1. onTransition
   当事件被 service 接收时触发，同时在 service 初始化的时候，会触发一个 xstate.init 事件，一样会触发 onTransition 回调。即使事件被接收后，状态并未发生改变，也同样会被触发。但可以通过 state.changed 来判断本次 transition，状态有没有被改变，如下例：

```ts
const callService = createCallService().onTransition((state, event) => {
  if (state.changed) {
    if (event.type === 'onCall') {
      handleTransition(event.type, event.customerInfo);
    } else {
      handleTransition(event.type);
    }
  }
});
```

2. onChange
   当 context 发生变化时触发

3. onEvent
   当给 service 发送事件时触发，不管该事件有没有被 service 接收，onEvent 都会被触发。

---
_Source: https://npm.io/package/call-service · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
