批量 Backbone.js 事件?

发布于 2024-12-08 19:07:25 字数 242 浏览 0 评论 0原文

我的应用程序的框架是围绕折叠骨干模型构建的,通过 websocket 发送数据并使用数据更新其他客户端上的模型。我的问题是,当某个操作连续触发 5 个更改时,我应该如何批量更新这些更新。

同步方法设置为更新任何更改,但如果我同时设置 5 个项目,我不希望它连续触发 5 次。

我想我可以对任何同步执行 setTimeout ,如果其他东西尝试在一秒钟内同步,则该同步会被清除。这看起来是最好的路线还是有更好的方法?

谢谢!

My app's framework is built around collapsing backbone models sending the data via websockets and updating models on other clients with the data. My question is how should I batch these updates for times when an action triggers 5 changes in a row.

The syncing method is set up to update on any change but if I set 5 items at the same time I don't want it to fire 5 times in a row.

I was thinking I could do a setTimeout on any sync that gets cleared if something else tries to sync within a second of it. Does this seem like the best route or is there a better way to do this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-12-15 19:07:25

我没有专门用主干来完成此操作,但我过去曾在其他分布式(客户端/服务器)应用程序中完成过这种批处理命令。

其要点是,如果您认为需要,您应该从超时开始并添加批量大小以进行进一步优化。

假设您的批次大小为 10。当您将 9 个项目塞入批次中,然后用户只是坐在那里,不做任何其他事情时,会发生什么?服务器永远不会收到用户想要做的事情的通知。

超时通常对于小批量来说效果很好。但是,如果您的操作会生成大量相关命令,您可能希望对所有命令进行批处理,并在准备好后立即将它们发送出去,而不是等待计时器。时间可能会在创建命令的过程中触发,并以导致问题的方式将事物分开,等等。

希望有所帮助。

i haven't done this with backbone specifically, but i've done this kind of batching of commands in other distributed (client / server) apps in the past.

the gist of it is that you should start with a timeout and add a batch size for further optimization, if you see the need.

say you have a batch size of 10. what happens when you get 9 items stuffed into the batch and then the user just sits there and doesn't do anything else? the server would never get notified of the things the user wanted to do.

timeout generally works well to get small batches. but if you have an action that generates a large number of related commands you may want to batch all of the commands and send them all across as soon as they are ready instead of waiting for a timer. the time may fire in the middle of creating the commands and split things apart in a manner that causes problems, etc.

hope that helps.

金兰素衣 2024-12-15 19:07:25

Underscore.js 是 Backbone.js 使用的实用程序库,它具有几个用于限制回调的函数

  • throttle 制作一个函数的版本,该函数最多每 X 毫秒执行一次。
  • debounce 制作一个函数版本,该函数仅在自上次调用以来经过 X 毫秒后才会执行
  • after 创建一个函数版本,该函数仅在被调用 X 次后才会执行。

因此,如果您知道有 5 个项目将被更改,您可以注册如下回调:

// only call callback after 5 change events
collection.on("change", _.after(5, callback));

但更有可能您不知道,并且您需要采用超时方法:

// only call callback 30 milliseconds after the last change event
collection.on("change", _.debounce(30, callback));

Underscore.js, the utility library that Backbone.js uses, has several functions for throttling callbacks:

  • throttle makes a version of a function that will execute at most once every X milliseconds.
  • debounce makes a version of a function that will only execute if X milliseconds elapse since the last time it was called
  • after makes a version of a function that will execute only after it has been called X times.

So if you know there are 5 items that will be changed, you could register a callback like this:

// only call callback after 5 change events
collection.on("change", _.after(5, callback));

But more likely you don't, and you'll want to go with a timeout approach:

// only call callback 30 milliseconds after the last change event
collection.on("change", _.debounce(30, callback));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文