npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dolphin-call-service

v1.0.15

Published

dolphin-call-service 是一个基于 xstate 的通话状态机.

Readme

dolphin-call-service

dolphin-call-service 是一个基于 xstate 的通话状态机.

安装

yarn add dolphin-call-service

状态机

call state flow:https://stately.ai/viz/ef5ef1d2-f8ab-4e94-883f-e1348edf246c

用法

Context

Context 在 xState 中的定位为 extended states, 即和状态相关的拓展字段。在这个状态机中,存在这样几个字段,用于记录通话的信息

interface CallContext {
  chatId: string;
  customerInfo: string;
}
  1. chatId: 当前通话的 id,触发 onCall 必须传入,创建通话 id 建议使用 nanoid
  2. customerInfo: 当前通话的客户信息,可以在通话 onCall 来传入,也可以使用 onDataUpdate 来更新,但是 onDataUpdate 只会在状态为 ringing 和 calling 的时候才会生效,在 idle 状态时传入的 onDataUpdate 会被忽略。在 onCall 触发的时候,也会先清空 customerInfo
const first = callService.send({
  type: "onCall",
  chatId: nanoid(8),
  customerInfo: customerInfo1,
});
const state = callService.send({
  type: "onDataUpdate",
  customerInfo: customerInfo2,
});

如何获取当前的 state 或 context

import { CallState } from "call-service/lib/callStateMachine";
// 获取当前的state值
callService.state.value;
// 获取当前context的值
callService.state.context;
// 判断当前的状态是否匹配某个状态
callService.state.matches(CallState.Calling);

发送 call event

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 返回,用法如下

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,状态有没有被改变,如下例:
const callService = createCallService().onTransition((state, event) => {
  if (state.changed) {
    if (event.type === "onCall") {
      handleTransition(event.type, event.customerInfo);
    } else {
      handleTransition(event.type);
    }
  }
});
  1. onChange 当 context 发生变化时触发

  2. onEvent 当给 service 发送事件时触发,不管该事件有没有被 service 接收,onEvent 都会被触发。