@atomico/channel

中文文档
0.0.1发布于 2022/05/09MITTypeScriptES Module零依赖近 30 天下载 1850

在WebComputers或DOM之间轻松共享状态.

中文使用文档
文档目录7 节
  1. 什么是频道?
  2. 示例
  3. API 密码
  4. 频道( )
  5. 频道. connect ()
  6. 频道. disconect ()
  7. 频道.cast ()

@ 原子/ 频道

@atomico/channel 允许WebComputers或DOM之间轻松共享状态.

Example

这个API的灵感来自React Context的目标,但消除了复杂性,是一种不可知的执行.

什么是频道?

一个频道是发送者和通过 DOM 的本地事件系统获得状态的接收者,一个频道有以下逻辑:

  1. 读取父的上下文
  2. 修改儿童的上下文

示例

这是网络组件订阅父节点的例子 :

import { Channel } from "@atomico/channel";

const CHANNEL = "MyChannel";
// Parent channel
const parentChannel = new Channel(document.body, CHANNEL);

class MyComponent extends HTMLElement {
    constructor() {
        super();
        // Child channel
        this.channel = new Channel(this, CHANNEL);
    }
    connectedcallback() {
        this.channel.connected(
            (data) => (this.textContent = JSON.stringify(data))
        );
    }
    disconnectedCallback() {
        this.channel.disconnect();
    }
}

// Connect the channel to the native DOM event system
parentChannel.connect();

parentChannel.cast("I'm your father");

API 密码

频道( )

const channel = new Channel(
    // Element
    host,
    // string
    "idString",
    // associates the composed option to the event
    // this allows bypassing the shadowDOM when connecting the channels
    true
);

频道. connect ()

channel.connect(optionalCallback);

地点:

  1. optionalCallback:可选择的回调,允许读取父的传输.

频道. disconect ()

删除订阅

频道.cast ()

向儿童频道播放新节目

channel.cast(10);
channel.cast({ data });

执行这种方法可以防止从父方传出的任何传输向该通道的孩子传播.

版本与发布记录

@atomico/channel 共发布 2 个版本,首次发布于 2021/06/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.1latest2022/05/094 年前
0.0.02021/06/145 年前

关于 @atomico/channel 的常见问题

@atomico/channel 是什么?

在WebComputers或DOM之间轻松共享状态.

如何安装 @atomico/channel?

在项目目录运行 npm install @atomico/channel,或使用 pnpm add / yarn add / bun add。

@atomico/channel 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@atomico/channel 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@atomico/channel 可以商用吗?

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

@atomico/channel 的下载量有多少?

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

@atomico/channel 还在维护吗?

最新版本 0.0.1 发布于 2022/05/09(4 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/06/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。