# @absinthe/socket 中文文档

规范地址：https://npmcn.com/package/%40absinthe/socket/

版本：0\.2\.1
npm 元数据更新时间：2022-06-12T14:15:17.595Z
中文译文更新时间：2026-11-05T17:40:06.009Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40absinthe/socket
原文对照：https://npmcn.com/package/%40absinthe/socket/?lang=original
项目主页：https://github.com/absinthe-graphql/absinthe-socket#readme
源码仓库：https://github.com/absinthe-graphql/absinthe-socket

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：1.8 MB，215 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：6 个
- peer 依赖：phoenix ^1\.4\.0
- 首次发布：2017-11-07T21:12:48.084Z
- 最新版本发布：2019-02-20T01:45:23.502Z
- 近 30 天下载量：174492（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：13（近 12 个月 0 个）

## 中文简介

饮用套接字

## 安装

```sh
npm install @absinthe/socket
```

也可以使用：`pnpm add @absinthe/socket`、`yarn add @absinthe/socket`、`bun add @absinthe/socket`

## README

# @ absinthe/ socket (韩语)

> 饮用套接字

-   [特征](https://npmcn.com/package/%40absinthe/socket/#readme-features)
-   [安装](https://npmcn.com/package/%40absinthe/socket/#readme-installation)
    -   [使用 npm](https://npmcn.com/package/%40absinthe/socket/#readme-using-npm)
    -   [使用线条](https://npmcn.com/package/%40absinthe/socket/#readme-using-yarn)
-   [类型](https://npmcn.com/package/%40absinthe/socket/#readme-types)
-   [API 密码](https://npmcn.com/package/%40absinthe/socket/#readme-api)
    -   [取消( e)](https://npmcn.com/package/%40absinthe/socket/#readme-cancel)
        -   [参数](https://npmcn.com/package/%40absinthe/socket/#readme-parameters)
        -   [实例](https://npmcn.com/package/%40absinthe/socket/#readme-examples)
    -   [创建](https://npmcn.com/package/%40absinthe/socket/#readme-create)
        -   [参数](https://npmcn.com/package/%40absinthe/socket/#readme-parameters-1)
        -   [实例](https://npmcn.com/package/%40absinthe/socket/#readme-examples-1)
    -   [观察](https://npmcn.com/package/%40absinthe/socket/#readme-observe)
        -   [参数](https://npmcn.com/package/%40absinthe/socket/#readme-parameters-2)
        -   [实例](https://npmcn.com/package/%40absinthe/socket/#readme-examples-2)
    -   [发送](https://npmcn.com/package/%40absinthe/socket/#readme-send)
        -   [参数](https://npmcn.com/package/%40absinthe/socket/#readme-parameters-3)
        -   [实例](https://npmcn.com/package/%40absinthe/socket/#readme-examples-3)
    -   [可读](https://npmcn.com/package/%40absinthe/socket/#readme-toobservable)
        -   [参数](https://npmcn.com/package/%40absinthe/socket/#readme-parameters-4)
        -   [实例](https://npmcn.com/package/%40absinthe/socket/#readme-examples-4)
    -   [取消观察](https://npmcn.com/package/%40absinthe/socket/#readme-unobserve)
        -   [参数](https://npmcn.com/package/%40absinthe/socket/#readme-parameters-5)
        -   [实例](https://npmcn.com/package/%40absinthe/socket/#readme-examples-5)
    -   [取消观察](https://npmcn.com/package/%40absinthe/socket/#readme-unobserveorcancel)
        -   [参数](https://npmcn.com/package/%40absinthe/socket/#readme-parameters-6)
        -   [实例](https://npmcn.com/package/%40absinthe/socket/#readme-examples-6)
-   [许可证](https://npmcn.com/package/%40absinthe/socket/#readme-license)

## 特征

-   可移动功能 API
    
    > 除Absinthe Socket外,所有收到并返回的物体 情况(有计划使这种不可改变)在以下情况下得到处理: 无法改变的方式。 物体没有方法,相反,我们提供依赖性 无国籍的功能与他们互动.
    
-   懒惰的连接/ 加入
    
    > 如果提供凤凰套接字实例时未连接,则不用 在创建时连接, 连接将在下一个时建立 援引 [发送](https://npmcn.com/package/%40absinthe/socket/#readme-send)。 。 。 .
    
-   处理连接丢失的待处理操作
    
    > 等待突变中止, 询问会很不满, 订阅 复设所.
    
-   可撤销的请求
    
    > 通话 [取消( e)](https://npmcn.com/package/%40absinthe/socket/#readme-cancel) 从 assinthe Socket 实例中删除给定的通知符 并发送一个取消事件给其所有观察者和未订阅者,以防它发生 持有订阅申请.
    
-   业务重复
    
    > 如果已经向 [发送](https://npmcn.com/package/%40absinthe/socket/#readme-send),而不是发送 它将再次返回与之相关的通知者.
    
-   观察员对可收回错误的支持
    
    > 由于失去的连接被处理,那么需要存在两个事件来代表 3⁄4 ̄ ̧漯B.
    
-   每个请求的多名观察员
    
    > 通话 [发送](https://npmcn.com/package/%40absinthe/socket/#readme-send) 返回允许附加任何编号的通知符 观察员,在结果到来时将予以通知.
    
-   根据操作类型进行观察互动
    
    > 就订阅而言, _开始_ 当 订阅成立,其他类型则订阅 (口水和变异),当请求发出.
    

## 安装

### 使用 [无](https://docs.npmjs.com/cli/npm)

```
$ npm install --save phoenix @absinthe/socket
```

### 使用 [线条](https://yarnpkg.com/)

```
$ yarn add phoenix @absinthe/socket
```

## 类型

```flowtype
type RequestStatus = "canceled" | "canceling" | "pending" | "sent" | "sending";

// from @jumpn/utils-graphql
type GqlRequest<Variables: void | Object = void> = {
  operation: string,
  variables?: Variables
};

// from @jumpn/utils-graphql
type GqlResponse<Data> = {
  data?: Data,
  errors?: Array<GqlError>
};

// from @jumpn/utils-graphql
type GqlOperationType = "mutation" | "query" | "subscription";

type Observer<Result, Variables: void | Object = void> = {|
  onAbort?: (error: Error) => any,
  onCancel?: () => any,
  onError?: (error: Error) => any,
  onStart?: (notifier: Notifier<Result, Variables>) => any,
  onResult?: (result: Result) => any
|};

type Notifier<Result, Variables: void | Object = void> = {|
  activeObservers: $ReadOnlyArray<Observer<Result, Variables>>,
  canceledObservers: $ReadOnlyArray<Observer<Result, Variables>>,
  isActive: boolean,
  operationType: GqlOperationType,
  request: GqlRequest<Variables>,
  requestStatus: RequestStatus,
  subscriptionId?: string
|};

type AbsintheSocket = {|
  channel: Channel,
  channelJoinCreated: boolean,
  notifiers: Array<Notifier<any>>,
  phoenixSocket: PhoenixSocket
|};
```

## API 密码

### 取消( e)

取消向所有观察员发送取消事件的通知人,并 如果持有订阅请求, 则不订阅

#### 参数

-   `absintheSocket` **饮用沙袋**
-   `notifier` **通知“ 任何,任何”**

#### 实例

```javascript
import * as withAbsintheSocket from "@absinthe/socket";

withAbsintheSocket.cancel(absintheSocket, notifier);
```

回返 **饮用沙袋**

### 创建

使用给定的 Phoenix Socket 实例创建 Absinthe Socket

#### 参数

-   `phoenixSocket` **凤凰套**

#### 实例

```javascript
import * as withAbsintheSocket from "@absinthe/socket";
import {Socket as PhoenixSocket} from "phoenix";

const absintheSocket = withAbsintheSocket.create(
  new PhoenixSocket("ws://localhost:4000/socket")
);
```

回返 **饮用沙袋**

### 观察

使用所提供的观察员给予的观察

#### 参数

-   `absintheSocket` **饮用沙袋**
-   `notifier` **通知器<结果,变量>**
-   `observer` **观察器 < 结果, 变量 >**

#### 实例

```javascript
import * as withAbsintheSocket from "@absinthe/socket"

const logEvent = eventName => (...args) => console.log(eventName, ...args);

const updatedNotifier = withAbsintheSocket.observe(absintheSocket, notifier, {
  onAbort: logEvent("abort"),
  onError: logEvent("error"),
  onStart: logEvent("open"),
  onResult: logEvent("result")
});
```

### 发送

发送给定的请求并返回一个对象( 通知符) 以跟踪其进度 (见观察功能)

#### 参数

-   `absintheSocket` **饮用沙袋**
-   `request` **Gql 请求 < 变量 >**

#### 实例

```javascript
import * as withAbsintheSocket from "@absinthe/socket";

const operation = `
  subscription userSubscription($userId: ID!) {
    user(userId: $userId) {
      id
      name
    }
  }
`;

// This example uses a subscription, but the functionallity is the same for
// all operation types (queries, mutations and subscriptions)

const notifier = withAbsintheSocket.send(absintheSocket, {
  operation,
  variables: {userId: 10}
});
```

回返 **通知器<结果,变量>**

### 可读

创建一个可观察的,将遵循给定的通知符

#### 参数

-   `absintheSocket` **饮用沙袋**
-   `notifier` **通知器<结果,变量>**
-   `options` **[对象](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)? 吗?** (可选,默认) `{}`(中文(简体) )
    -   `options.unsubscribe` **函数 (): [未定义](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)? 吗?**
    -   `options.onError` **函数( 错误 : [错误](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error): [未定义](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)? 吗?**
    -   `options.onStart` **函数( 通知符: 通知符 < Result, 变量 > ): [未定义](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined)? 吗?**
    -   `options.handlers` **...任何东西**

#### 实例

```javascript
import * as withAbsintheSocket from "@absinthe/socket";

const unobserveOrCancelIfNeeded = (absintheSocket, notifier, observer) => {
  if (notifier && observer) {
    withAbsintheSocket.unobserveOrCancel(absintheSocket, notifier, observer);
  }
};

const logEvent = eventName => (...args) => console.log(eventName, ...args);

const observable = withAbsintheSocket.toObservable(absintheSocket, notifier, {
  onError: logEvent("error"),
  onStart: logEvent("open"),
  unsubscribe: unobserveOrCancelIfNeeded
});
```

回返 **可观察**

### 取消观察

从通知中除去观察员

#### 参数

-   `absintheSocket` **饮用沙袋**
-   `notifier` **通知“ 任何,任何”**
-   `observer` **观察器“ 任何,任何”**

#### 实例

```javascript
import * as withAbsintheSocket from "@absinthe/socket";

withAbsintheSocket.unobserve(absintheSocket, notifier, observer);
```

回返 **饮用沙袋**

### 取消观察

如果除给定的观察者之外没有其他观察者,则取消通知者,或 将观察员与通知人分开,否则

#### 参数

-   `absintheSocket` **饮用沙袋**
-   `notifier` **通知器<结果,变量>**
-   `observer` **观察器 < 结果, 变量 >**

#### 实例

```javascript
import * as withAbsintheSocket from "@absinthe/socket";

withAbsintheSocket.unobserve(absintheSocket, notifier, observer);
```

## 许可证

[麻省理工学院](https://npmcn.com/package/%40absinthe/socket/LICENSE.txt) :版权:Jumpn Limited.

## 常见问题

### @absinthe/socket 是什么？

饮用套接字

### 如何安装 @absinthe/socket？

在项目目录运行 npm install @absinthe/socket，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：phoenix（^1\.4\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @absinthe/socket 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/absinthe\_\_socket 是否存在。

### @absinthe/socket 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @absinthe/socket 可以商用吗？

@absinthe/socket 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @absinthe/socket 的下载量有多少？

根据 npm 官方下载统计，@absinthe/socket 在 2026/10/05 – 2026/11/03 这 30 天内被下载 174,492 次，日均约 5,816 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @absinthe/socket 还在维护吗？

最新版本 0\.2\.1 发布于 2019/02/20（7 年前）；至今共发布 13 个版本，其中过去 12 个月发布了 0 个；首次发布于 2017/11/08。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

