@hocuspocus/server 后端失败通知用户

huangapple go评论51阅读模式
英文:

@hocuspocus/server Notify user on backend fail

问题

我正在服务器配置中使用 onStoreDocument 钩子。从这个钩子中,我正在进行数据库查询以存储文档。

如果数据库保存更新失败,我想通知客户端 UI。

所以我有两个问题:

  1. 有没有一种方法(即方法)可以标记文档在客户端未同步?

  2. 我能否向客户端发送带有错误消息的消息?我看到有人引用了 connection.sendStateless,但我如何从 onStoreDocument 中访问它?

英文:

I am using onStoreDocument hook in server configuration. From this hook I am making a database query to store the document.

I would like to notify the client UI in case database fails to save the update.

So I have two questions:

  1. Is there any way (i.e. method) to somehow mark that the document is not synced on client side?

  2. Can i send a message with error to the client? I saw people reference connection.sendStateless but how can access it from onStoreDocument?

答案1

得分: 1

我找到了我的问题的答案:

> 是否有办法(即方法)来标记文档在客户端端未同步的方式?

您可以从 provider.hasUnsyncedChanges 获取布尔值。为了实现响应性,您可以使用提供程序上发出的 unsyncedChanges 事件,例如:

provider.on('unsyncedChanges', (n: number) => {
  console.log('Number of changes to send to server:', n);
});

> 我可以向客户端发送带有错误的消息吗?我看到人们引用了 connection.sendStateless,但我如何从 onStoreDocument 中访问它?

sendStateless 在客户端上,并且可以通过 provider.sendStateless 访问。

对于服务器端,我们可以使用 document.broadcastStateless。因此,onStoredDocument 将类似于以下内容:

async onStoreDocument({ document }) {
  // 保存到数据库...
  document.broadcastStateless('已保存!')
}
英文:

I found the answers to my questions:

> Is there any way (i.e. method) to somehow mark that the document is not synced on client side?

You can get the boolean value from provider.hasUnsyncedChanges. For reactivity you can use unsyncedChanges event emitted on provider, i.e.:

provider.on('unsyncedChanges', (n: number) => {
  console.log('Number of changes to send to server:', n);
});

> Can i send a message with error to the client? I saw people reference connection.sendStateless but how can access it from onStoreDocument?

sendStateless is on client side and accessible via provider.sendStateless.

for the server side we can use document.broadcastStateless. So onStoredDocument will look something like that:

async onStoreDocument({ document }) {
  // saving to db...
  document.broadcastStateless('Saved!')
}

huangapple
  • 本文由 发表于 2023年7月17日 22:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定