Firestore onSnapshot更新事件是否归因于本地客户端集? [英] Is Firestore onSnapshot update event due to local client Set?

查看:42
本文介绍了Firestore onSnapshot更新事件是否归因于本地客户端集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单版本:当我在Firestore中注册onSnapshot时,是否有办法知道我是否是推送更新的客户端?

Simple version: When I register to onSnapshot in Firestore, is there a way to know if I am the client that pushed the update?

详细版本:
当网页打开时,它会向名为 notes 的Firestore集合注册onSnapshot回调.然后下载该集合并将其存储在本地数组 var notes = [] 中.

Detailed Version:
When a webpage is opened, it registers an onSnapshot callback to a Firestore collection called notes. The collection is then downloaded and stored in a local array var notes = [].

var window.notes = []
let notesRef = refs.db.collection('notes')
notesRef.onSnapshot(querySnapshot => {
  querySnapshot.docChanges.forEach((docChange) => {
    switch (docChange.type) {
      case 'added':
        notes.push(docChange.doc.data())
        break
    }
  })
}

然后,我们在本地更新注释,并将其推送到数据库中.

We then update a note locally and push it to the database.

notes[0] =  'changed contents'
firestore.notesRef.doc(0).set(notes[0])

这会将注释发送到firestore,在远程数据库中对其进行更新,然后由于我们已将 notes 集合注册到 onSnapshot 事件,并进行修改更改.

This will send the note out to firestore, update it in the remote database, then because we're registered to onSnapshot for the notes collection we'll trigger that event and get a modified change.

let notesRef = refs.db.collection('notes')
notesRef.onSnapshot(querySnapshot => {
  querySnapshot.docChanges.forEach((docChange) => {
    switch (docChange.type) {
      case 'added':
        notes.push(docChange.doc.data())
        break
      case 'modified':
        // ********** We end up here!!! ***********
        // Probably want to do:
        notes[docChange.doc.id] = docChange.doc.data()
        break
    }
  })
}

是否有办法检测是否此相同的客户端调用了 set 并触发了 modified docChange?
关键是:如果用户自己进行更改,则他们不在乎更改广告,我们应该将其忽略.如果其他用户进行了更改,则应该为该用户提供是否要更新其本地版本的选择.

Is there a way to detect if this same client called the set that triggered the modified docChange?
The point is this: If the user made the change themself then they don't care about the change ad we should ignore it. If a different user made the change then this user should be given a choice of whether or not they want to update their local version.

以我的经验,当调用 firestore.notesRef.doc(0).set(notes [0])时,需要一些时间才能从服务器接收更新.如果本地用户这次再次修改了笔记,但尚未将其推送到服务器,则他们的更改将丢失(被服务器版本覆盖-他们最后发送到服务器的版本).本质上这是一个竞赛条件.

In my experience, when firestore.notesRef.doc(0).set(notes[0]) is called it takes some time to receive the update from the server. If the local user modifies the note again in this time but hasn't pushed it to the server yet then their changes are lost (overridden by the servers version - the version they last sent to the server). It's essentially a race condition.

因此,当我在Firestore中注册onSnapshot时,是否有办法知道我是否是推送更新的客户端?

So, when I register to onSnapshot in Firestore, is there a way to know if I am the client that pushed the update?

推荐答案

您可以通过以下方式查看它是本地更改还是远程更改:

You can see if it was a local or remote change with:

docChange.doc.metadata.hasPendingWrites

如果 hasPendingWrites 为true,则更新的起源是本地的.本地缓存已更新,并且对服务器的写入仍处于挂起状态.如果 hasPendingWrites 为false,则更新的源是远程的.

If hasPendingWrites is true, then origin of the update was local. The local cache has been updated and the write to the server is still pending. If hasPendingWrites is false, then the origin of the update was remote.

在此代码段中查看如何与 onSnapshot 一起使用:

See how it can be used with onSnapshot in this snippet:

let notesRef = refs.db.collection('notes')
notesRef.onSnapshot(querySnapshot => {
  querySnapshot.docChanges.forEach((docChange) => {
    switch (docChange.type) {
      case 'added':
        notes.push(docChange.doc.data())
        break
      case 'modified':
        // ********** New Code ***********
        let source = docChange.doc.metadata.hasPendingWrites ? 'Local' : 'Server'
        if (source === 'Server') {
          notes[docChange.doc.id] = docChange.doc.data()
        } else {
          // Do nothing, it's a local update so ignore it
        }
        // ********** New Code End ***********
        break
    }
  })
}

这篇关于Firestore onSnapshot更新事件是否归因于本地客户端集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆