0

我有一个图形编辑器,用户可以选择创建一个节点。它与所有当前选定的节点连接。在谷歌文档中,看起来一个节点(其字符串标签)映射到逗号分隔的一组连接标签。所以,要添加一个节点,我首先创建一个空地图项

map.set(name, "");

然后,分别添加连接的项目

if (map.get(a) == null) throw new Error("node " + a + " does not exist") // fails here
if (map.get(b) == null) throw new Error("node " + b + " does not exist")
map.set(a, a_connections)
map.set(b, b_connections)

问题是 map.get 检测到该节点尚未添加到地图中。这需要一些时间。即使在单个 JS 客户端中,操作似乎也是非阻塞的(读我写不一致)。我应该如何处理它?

我在尝试建立两个连接时注意到了这种不一致(只是为了检测连接何时失败,因为可能会发生连接丢失并且我的所有编辑都不会传播到服务器,我想让用户知道这一点)。

4

1 回答 1

0

This page has some details on conflict resolution and things you can do in order to have your changes be applied together.

I'm a little confused from your example what the problem/expected behavior is.

If you do map.set("foo", "") followed by map.get("foo") on the same client from within the same synchronous block the get will always return what you set.

If you do it from different synchronous blocks, but on the same client, get will return something different only if another client made a change to the value of "foo".

If you are doing the set and get on different clients, then the value of "foo" can take an arbitrary amount of time to propagate to the other client. You should be able to register a listener to discover when it is set.

If you'd like to track when all changes have been persisted, you can listen to DocumentSaveStateChangedEvents

于 2016-06-01T15:28:49.220 回答