v0.1首个版本化设计基线查看版本范围
Developer

组织本地数据与同步

v0.1 鼓励 local-first:store 是应用权威本地状态;identity 只决定平台同步是否可用;net 只在应用确实有自有服务时引入。

1. 先读本地状态

const entry = await host.store.get('documents/current');
const document = entry ? decodeDocument(entry.value) : createDocument();
render(document);

应用不应等网络或登录才显示本地内容。

2. 使用版本避免覆盖

async function save(document, expectedVersion) {
  try {
    return await host.store.put('documents/current', encodeDocument(document), expectedVersion);
  } catch (error) {
    if (error.code !== 'conflict') throw error;
    return resolveConflict(document, await host.store.get('documents/current'));
  }
}

3. 可选登录

let account = await host.identity.status();
if (!account.authenticated && userWantsSync) {
  await host.identity.login();
  account = await host.identity.status();
}

应用只知道布尔状态,不得用全局用户 ID 做数据库主键。

4. 自有服务的明确边界

只有应用确实需要访问 manifest 声明的第三方 API 时才使用 net.fetch()

const response = await host.net.fetch({
  url: 'https://sync.example.com/v1/changes',
  method: 'POST',
  body: encodeEncryptedChanges(document.pendingChanges),
});

平台持有的身份不会自动成为任意第三方 API 的身份。凭据作用域必须由独立契约定义。

5. 哪些东西留在 guest

数据库页、索引、查询、CRDT、同步日志、冲突解决与端到端加密协议都是 L2/L3。宿主 API 只提供持久化资源、受控身份流程和经代理网络。

相关参考

storeidentitynet.fetch()

Host API 版本与分层概览Host API v0.1版本非正式提议提议canvas API呈现canvas.getInfo()方法canvas.draw()方法canvas.present()方法canvas.measureText()方法canvas.frame event事件canvas.resize event事件asset API呈现asset.read()方法asset.loadFont()方法playback API呈现playback.open()方法playback.write()方法playback.stop()方法playback.ready event事件playback.ended event事件input API输入input.focus()方法input.pointer event事件input.key event事件input.wheel event事件ime API输入ime.openSession()方法ime.updateState()方法ime.updateGeometry()方法ime.closeSession()方法ime.textUpdate event事件ime.composition event事件ime.formatUpdate event事件ime.characterBoundsRequest event事件capture API输入capture.requestCamera()方法capture.requestMic()方法capture.frame()方法capture.readAudio()方法capture.stop()方法capture.ended event事件store API数据store.get()方法store.put()方法store.delete()方法store.list()方法store.deletePrefix()方法clipboard API数据clipboard.readText()方法clipboard.writeText()方法clipboard.readImage()方法clipboard.writeImage()方法net API网络net.fetch()方法identity API身份identity.status()方法identity.login()方法identity.logout()方法env API平台集成env.snapshot()方法env.change event事件print API平台集成print.open()方法开始调用 Host API指南构建交互式绘图应用指南构建自绘文本编辑器指南管理媒体采集与播放指南组织本地数据与同步指南