从另一个数组中获取具有相同项目的项目的最佳实践是什么

发布于 2024-12-01 01:20:08 字数 1694 浏览 0 评论 0原文

好吧,我知道标题很复杂,但是这个问题也很难用一句话来表达……抱歉。

目标:我想在所有模块可用时运行回调 问题:最快的方法是什么? 示例:

问题始于函数“runCallbacks”,它获取所有新添加的对象,如下所示:

runCallbacks(newItems/*as array ['a','b','c'];*/){
    // now I would need to understand the dependencies of the callbacks
    // each callback might depend on one or more objects
    // iNeed(['a','b'], toRunThis);
    //           |          |-callback to run when those are ready
    //           |-are the dependencies

我想到的是:

 callbacks = [[[callbackFunction],['loadedItems'],['notLoadedItems']]]
              |-----------it's a single callback--------------------|

具有良好的性能还是您有更好的主意? 谢谢

另一个例子

this.use(['a', 'b'], function(){/* do something with 'a' and 'b' only when are ready */})
use: function(paths, callback, target/*not used in this case*/){
    // "a", "b", 'c' module is available
    // "d", 'e' module is not available
    this.callbacks.push([[target], ['a', 'b', 'c'], ['d', 'e']]);
    //                               |      |-not loaded
    //                               |-loaded
}

// then an object might be added
this.add({...})

// then will check if this new object may make some callbacks to run
function(newPaths/* ['d'] */){
    // loop all callbacks items
    // remove items from [not loaded array] and put to [loaded array] if
    // exists in newPaths
    // in this case the callback already has: a,b,c; but misses: d,e;
    // now it will add "d" to the loaded array
    // and now only miss the "e" path

,如果回调需要“a”和“b”但不存在,则在两者都准备好时保存回调 我添加了“a”和“b”模块,然后我想知道女巫回调是否已准备好运行;

回调可能有多个依赖项 “a”模块可能被多个回调使用 这就是为什么有点复杂

Ok, I know the title is quite complicated but the question is hard to make in one line too.. sorry.

Objective: I want to run a callback when all modules are available
Problem: what is the fastest way?
Example:

the problem begins in the function "runCallbacks" that gets all the new added objects like this:

runCallbacks(newItems/*as array ['a','b','c'];*/){
    // now I would need to understand the dependencies of the callbacks
    // each callback might depend on one or more objects
    // iNeed(['a','b'], toRunThis);
    //           |          |-callback to run when those are ready
    //           |-are the dependencies

what I was thinking of is this:

 callbacks = [[[callbackFunction],['loadedItems'],['notLoadedItems']]]
              |-----------it's a single callback--------------------|

has good performance or do you have any better idea?
thanks

Another example

this.use(['a', 'b'], function(){/* do something with 'a' and 'b' only when are ready */})
use: function(paths, callback, target/*not used in this case*/){
    // "a", "b", 'c' module is available
    // "d", 'e' module is not available
    this.callbacks.push([[target], ['a', 'b', 'c'], ['d', 'e']]);
    //                               |      |-not loaded
    //                               |-loaded
}

// then an object might be added
this.add({...})

// then will check if this new object may make some callbacks to run
function(newPaths/* ['d'] */){
    // loop all callbacks items
    // remove items from [not loaded array] and put to [loaded array] if
    // exists in newPaths
    // in this case the callback already has: a,b,c; but misses: d,e;
    // now it will add "d" to the loaded array
    // and now only miss the "e" path

so if a callback needs "a" and "b" but not exists saves the callback for when both are ready
I add the "a" and "b" module and then I want to know witch callbacks are ready to run;

a callback might have multiple dependences
the "a" module might be used by multiple callbacks
that's why is a little bit complicated

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

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

发布评论

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

评论(1

我不吻晚风 2024-12-08 01:20:08

如果您正在运行jquery,请查看$.when,否则还有像promise.js这样的库(此处说明:http://blogs.msdn.com/b/rbuckton/archive/2010/01/29/promises-and-futures-in-javascript.aspx)。这个想法是当依赖关系准备好并解决时,承诺会触发事件。

If you are running jquery have a look at $.when, otherwise there are libraries like promises.js (explination here: http://blogs.msdn.com/b/rbuckton/archive/2010/01/29/promises-and-futures-in-javascript.aspx). The idea is the promise fires the event when the dependacies are ready and resolved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文