英文:
@hocuspocus/server Notify user on backend fail
问题
我正在服务器配置中使用 onStoreDocument 钩子。从这个钩子中,我正在进行数据库查询以存储文档。
如果数据库保存更新失败,我想通知客户端 UI。
所以我有两个问题:
-
有没有一种方法(即方法)可以标记文档在客户端未同步?
-
我能否向客户端发送带有错误消息的消息?我看到有人引用了
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:
-
Is there any way (i.e. method) to somehow mark that the document is not synced on client side?
-
Can i send a message with error to the client? I saw people reference
connection.sendStatelessbut how can access it fromonStoreDocument?
答案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!')
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论