@blockprotocol/service

中文文档
0.1.5发布于 2024/03/19MITTypeScriptES Module近 30 天下载 2.3万

Implementation of the Block Protocol service module specification for blocks and embedding applications

原始使用文档
文档目录6 节
  1. Block Protocol – Service Module
  2. Blocks
  3. React example
  4. Custom elements
  5. Embedding applications
  6. React

Block Protocol – Service Module

This package implements the Block Protocol Service module for blocks and embedding applications.

To get started:

  1. yarn add @blockprotocol/service or npm install @blockprotocol/service
  2. Follow the instructions to use the service module as a block or an embedding application

Blocks

To create a ServiceBlockHandler, pass the constructor an element in your block, along with any callbacks you wish to register to handle incoming messages.

React example

For React, we provide a useServiceBlockModule hook, which accepts a ref to an element, and optionally any callbacks you wish to provide on initialization.

Custom elements

Create a ServiceBlockHandler in your element. If you are using our template, you can add this in the constructedCallback instance method (Lit docs) and make it available for use in your element.

  ...
  connectedCallback() {
    super.connectedCallback();
    if (!this.serviceModule || this.serviceModule.destroyed) {
      this.serviceModule = new ServiceBlockHandler({
        element: this,
      });
    }
  }

  retrieveDirections(data) {
    if (!this.serviceModule) {
      throw new Error("Service module not available");
    }
    this.serviceModule.mapboxRetrieveDirections(args).then(() => { ... });
  }

  ...

Embedding applications

You should construct one ServiceEmbedderHandler per block.

It is not currently possible to wrap multiple blocks with a single handler.

To create a ServiceEmbedderHandler, pass the constructor:

  1. An element wrapping your block
  2. callbacks to respond to messages from the block
import { ServiceEmbedderHandler } from "@blockprotocol/service";

const serviceModule = new ServiceEmbedderHandler({
  callbacks: { ... },
  element: elementWrappingTheBlock,
});

React

For React embedding applications, we provide a useServiceEmbedderModule hook, which accepts a ref to an element, and optionally any additional constructor arguments you wish to pass.

import { useServiceEmbedderModule } from "@blockprotocol/service";
import { useRef } from "react";

export const App = () => {
  const wrappingRef = useRef<HTMLDivElement>(null);

  const { serviceModule } = useServiceEmbedderModule(blockRef);

  return (
    <div ref={wrappingRef}>
      <Block />
    </div>
  );
};

版本与发布记录

@blockprotocol/service 共发布 34 个版本,首次发布于 2023/02/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.5latest2024/03/192 年前
0.1.5-canary-20240314113521canary2024/03/142 年前
0.1.5-canary-202403131225332024/03/132 年前
0.1.5-canary-202403121912272024/03/132 年前
0.1.5-canary-202403041547262024/03/042 年前
0.1.5-canary-202403041425212024/03/042 年前

关于 @blockprotocol/service 的常见问题

@blockprotocol/service 是什么?

Implementation of the Block Protocol service module specification for blocks and embedding applications

如何安装 @blockprotocol/service?

在项目目录运行 npm install @blockprotocol/service,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@blockprotocol/service 支持 TypeScript 吗?

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

@blockprotocol/service 支持 ESM 还是 CommonJS?

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

@blockprotocol/service 可以商用吗?

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

@blockprotocol/service 的下载量有多少?

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

@blockprotocol/service 还在维护吗?

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