Categories
Tags
Ai 生成 API学习 API简化 api请求 API调用 best-practices Blogging Caching catchTag catchTags class CLI Config context Context Context.Tag CSS Customization Demo development DocC Docker dual API Effect effect Effect.Service Effect.succeed Example extension ffmpeg filterOrFail flatMap Fuwari gen generator grep hooks HTML HTTP响应 IDE自动补全 iOS javascript JavaScript Javascript Layer.effect Layer.provide Layers Linux Markdown Mock n8n Next.js ParseError pipe pokemon PostCSS process.env progress Promise promise provideService PWA react React React Hook Form React Query React Router react-native Scheduler Schema Schema.Class security Service Worker Services SSR state-management suspense Tagged Errors TaggedError TanStack Query TanStack Start tips tryPromise tsconfig TypeScript typescript Video VS Code vscode Web API Web Development yield yt-dlp Zod 不透明类型 二叉树 代码组织 任务调度 优先级 使用服务 依赖注入 依赖管理 值语义 入门教程 最佳实践 最小堆 函数式编程 函数组合 前端 前端开发 副作用 副作用控制 可视化 可组合性 可维护性 可访问性 命令行 响应过滤 多个错误 实现 实践指南 层 层依赖 层组合 工具链 并发控制 应用架构 延迟执行 开发技巧 开发教程 开源 异步处理 异步操作 异步编程 性能优化 手写系列 排序 接口设计 插件开发 数据结构 数据获取 数据解码 数据验证 无限滚动 日历 日志分析 服务 服务依赖 服务定义 服务实现 服务提供 测试 源码分析 状态管理 环境变量 生成器 离线支持 程序分离 算法 类型安全 类型定义 类型推断 类型系统 类定义 线性代码 组合 翻译 自动化 自定义错误 表单验证 记忆化 设计模式 语义化 运维 运行时验证 部分应用 配置 配置变量 配置服务 配置管理 重构 错误处理 错误定义 错误恢复 项目设置
380 words
2 minutes
[Effect Layers] 05. 层间依赖
层间依赖
https://github.com/typeonce-dev/effect-getting-started-course
你可能在运行 main 时注意到了一个类型错误:

我们仍然无法运行main,因为似乎缺少一个依赖关系!
Type 'PokeApiUrl' is not assignable to type 'never'.这是一个缺失的依赖关系!为什么 PokeApiUrl 缺失?我们不是已经向 Layer.mergeAll 提供了 PokeApiUrl 吗?
const MainLayer = Layer.mergeAll(
PokeApi.Live,
PokemonCollection.Live,
BuildPokeApiUrl.Live,
PokeApiUrl.Live
);是的,但我们有一个没有解决的层之间的依赖关系:BuildPokeApiUrl 层依赖于 PokeApiUrl。
BuildPokeApiUrl.ts
export class BuildPokeApiUrl extends Context.Tag("BuildPokeApiUrl")<
BuildPokeApiUrl,
({ name }: { name: string }) => string
>() {
static readonly Live = Layer.effect(
this,
Effect.gen(function* () {
const pokeApiUrl = yield* PokeApiUrl; // 👈 创建依赖
return ({ name }) => `${pokeApiUrl}/${name}`;
})
);
}如果我们检查 BuildPokeApiUrl.Live 的类型,我们会看到以下内容:Layer.Layer<BuildPokeApiUrl, never, PokeApiUrl>。
由于完整的服务需要 PokeApiUrl,我们需要直接向 BuildPokeApiUrl 提供它。
我们通过使用 Layer.provide 来做到这一点:
index.ts
const MainLayer = Layer.mergeAll(
PokeApi.Live,
PokemonCollection.Live,
BuildPokeApiUrl.Live.pipe(Layer.provide(PokeApiUrl.Live)),
PokeApiUrl.Live
);IMPORTANTAPI 区别
我们在组合层时使用
Layer.provide,在提供最终层来运行Effect时使用Effect.provide。
Layer.provide和Effect.provide是两个不同的API!
现在我们的程序可以编译并工作了:
index.ts
import { Effect, Layer } from "effect";
import { BuildPokeApiUrl } from "./BuildPokeApiUrl";
import { PokeApi } from "./PokeApi";
import { PokeApiUrl } from "./PokeApiUrl";
import { PokemonCollection } from "./PokemonCollection";
const MainLayer = Layer.mergeAll(
PokeApi.Live,
PokemonCollection.Live,
BuildPokeApiUrl.Live.pipe(Layer.provide(PokeApiUrl.Live)),
PokeApiUrl.Live
);
export const program = Effect.gen(function* () {
const pokeApi = yield* PokeApi;
return yield* pokeApi.getPokemon;
});
const runnable = program.pipe(Effect.provide(MainLayer));
const main = runnable.pipe(
Effect.catchTags({
FetchError: () => Effect.succeed("Fetch error"),
JsonError: () => Effect.succeed("Json error"),
ParseError: () => Effect.succeed("Parse error"),
})
);
Effect.runPromise(main).then(console.log);> effect-getting-started-course@1.0.0 dev
> BASE_URL=https://pokeapi.co tsx src/index.ts
{ id: 120, order: 191, name: 'staryu', height: 8, weight: 345 }