隔离代理

描述

Isolate Agents 添加了一个新的类,Agent,它是 Dart 中 Actor 模型 的一个正确实现。 与 Isolates 没有通信机制不同,Agents 具有。 它受 Clojure 的 agents 的启发。

用法示例

import 'package:isolate_agents/isolate_agents.dart';

void main() async {
  Agent<int> agent = await Agent.create(1);
  // The following add operation is executed in the Agent's isolate.
  agent.send((int? x) => x! + 1);
  assert(2 == await agent.exit());
}

为什么?

在编写 Dart 代码几年后,我意识到为了能够使用 Isolates,我一直在重复编写相同的代码。 Isolates 没有完全实现 Actor 模型,因此需要为 Isolates 的每个非平凡用法设计 SendPort 的握手和协议。 Agents 将所有这些逻辑分解为一个可重用的包,消除了 SendPort 握手并标准化了协议。

GitHub

查看 Github