gpt4 book ai didi

meteor - 为什么meteor 会撤消对嵌套在观察者方法中的集合的更改?

转载 作者:行者123 更新时间:2023-12-01 03:59:51 24 4
gpt4 key购买 nike

我正在尝试实现这样的东西:

/* We use the command pattern to encode actions in
a 'command' object. This allows us to keep an audit trail
and is required to support 'undo' in the client app. */
CommandQueue.insert(command);

/* Queuing a command should trigger its execution. We use
an observer for this. */
CommandQueue
.find({...})
.observe({
added: function(command) {
/* While executing the action encoded by 'command'
we usually want to insert objects into other collections. */
OtherCollection.insert(...)
}
});

不幸的是, meteor 似乎保持了 OtherCollection 的先前状态在 CommandQueue 上执行交易时.临时更改 OtherCollection .交易一上 CommandQueue完成, OtherCollection 的先前状态但是,将被恢复,而我们的更改将消失。

任何想法为什么会发生这种情况?这是预期的行为还是错误?

最佳答案

这是预期的行为,虽然它有点微妙,并且不能保证(只是一个实现细节)。

当命令被插入到 CommandQueue 中时,要观察的回调立即触发。 .所以插入到 OtherCollection CommandQueue.insert 时发生方法正在运行,作为同一调用堆栈的一部分。这意味着 OtherCollection插入被认为是 CommandQueue 的本地“模拟”的一部分插入,并且不会发送到服务器。服务器运行 CommandQueue插入并返回结果,此时客户端丢弃模拟结果并应用服务器发送的结果,使得OtherCollection变化消失。

更好的方法是编写自定义方法。就像是:

Meteor.methods({
auditedCommand: function (command) {
CommandQueue.insert(command);

var whatever = someProcessing(command)
OtherCollection.insert(whatever);
}
});

然后:
Meteor.call('auditedCommand', command);

这将立即显示在客户端上(延迟补偿)并且更安全,因为客户端无法插入 CommandQueue不添加到 OtherCollection .

编辑 : 这可能会改变。添加的回调不应真正被视为 CommandQueue.insert 的本地模拟的一部分。这就是它现在的工作方式。也就是说,自定义方法可能仍然是更好的方法,即使其他人将命令添加到命令队列,它也能工作,并且更安全。

关于meteor - 为什么meteor 会撤消对嵌套在观察者方法中的集合的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14250259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com