Skip to content

Placeholder 与调度工具

这组能力本身在客户端执行,但服务端接入者经常要为它们设计节奏和回写策略。

Placeholder

javascript
Placeholder.parse("Welcome %player_name%", (text) => {
  document.getElementById("welcome")?.setValueString(text)
})

适合轻量展示,不适合承担核心业务真值。

Scheduler

javascript
const task = Scheduler.sync(20, 20, true, () => {
  // every second
})

onUnmounted(() => task.cancel())
  • sync:主线程
  • async:异步
  • render:渲染线程

Debounce

javascript
import { query } from "bui:action"

const d = Debounce.create(120, false, false, () => {
  query("shop.search", { keyword: store.get("keyword") })
})

function onInput() {
  d.trigger()
}

Throttle

javascript
import { command } from "bui:action"

const t = Throttle.create(200, true, () => {
  command("skill.cast", { id: "fireball" })
})

function onCast() {
  t.trigger()
}

使用组合

  1. 高频读请求:Debounce + query()
  2. 高频写请求:Throttle + command()
  3. 长生命周期任务在卸载时统一 cancel()