Skip to content

request ​

request() 向另一个 NApp 发起 Event 或 Ability 请求,并返回请求句柄。

js
const call = app.request("world", {
  kind: "ability",
  target: "appendWorld",
  payload: { text: "Hello, " },
})

const response = await call.response
console.log(response.payload)

参数 ​

ts
app.request(to, {
  kind,
  target?,
  payload?,
  onProcess?,
})
参数说明
to目标 NApp 的 id
kind请求类型:"ability" 或 "event"
target要执行的 Ability 或 Event 名称
payload发送给目标的业务载荷
onProcessEvent 的过程结果回调,Ability 不使用

Ability 请求 ​

Ability 只有一个最终响应:

js
const { reqId, response } = app.request("math", {
  kind: "ability",
  target: "add",
  payload: { a: 114514, b: 1805296 },
})

console.log(reqId)
console.log((await response).payload) // 1919810

Event 请求 ​

Event 除最终响应外,还可以返回过程消息流。

你可以使用 onProcess 回调或 AsyncIterator 读取过程消息。

两种方式都返回完整的 NotifyMessage,和Subscribe基本一致。

INFO

request会自动订阅相关事件的Process流,并通过这两种方式允许用户读取。

详情参阅 AutoSubscribe 机制。

监听中途流 ​

js
const call = app.request("worker", {
  kind: "event",
  target: "countdown",
  payload: { from: 3 },
  onProcess: (message) => console.log(message.payload),
})

const result = await call.response
js
const call = app.request("worker", {
  kind: "event",
  target: "countdown",
  payload: { from: 3 },
})

for await (const message of call.stream) {
  console.log(message.payload)
}

const result = await call.response

TIP

迭代器和回调的差异和细节内容基本与Subscribe用法一致。

不过request的迭代器中提前break不会中断原始流的进行。

WARNING

需要注意,只有迭代器有队列,回调默认没有队列!

这意味着用回调的方式很有可能会漏掉最开始的几条消息。

Subscribe章节有对此完整解释。

返回值与失败 ​

所有请求句柄都具有以下参数:

字段说明
reqId本次请求的唯一 ID
response最终 ResponseMessage 的 Promise
stream仅 Event 请求提供的 AsyncIterable<NotifyMessage>

目标拒绝请求、处理失败或消息无法发出时,response 会 reject:

js
const call = app.request("math", {
  kind: "ability",
  target: "unknown",
})
call.response.then(res => console.log).catch((e) => throw)

Released under the MIT License.