如何勾勒出事件驱动的系统?

发布于 2024-10-11 13:01:26 字数 566 浏览 3 评论 0原文

我正在尝试在 Node.js 中设计一个系统(尝试解决 我之前的问题,使用 Node 的并发性),但我在弄清楚如何制定事物应该如何运行的计划时遇到了麻烦。

我对回调而不是返回值的思考感到非常困惑。流程不是线性的,这真的让我难以起草它。如何绘制事件驱动系统的操作流程?

我需要一些我可以看到的东西,然后说“好吧,是的,这就是它的工作原理。我会在这里重新开始,它会在这里给我返回这些结果。”

图片对此非常有帮助。谢谢。

编辑:我正在寻找比UML更精细的东西,具体来说,它将帮助我从我习惯的阻塞和面向对象的编程结构过渡到我不熟悉的非阻塞和事件驱动的结构。

I'm trying to design a system in Node.js (an attempt at solving one of my earlier problems, using Node's concurrency) but I'm running into trouble figuring out how to draw a plan of how the thing should operate.

I'm getting very tripped up thinking in terms of callbacks instead of returned values. The flow isn't linear, and it's really boggling my ability to draft it. How does one draw out an operational flow for an event-driven system?

I need something I can look at and say "Ok, yes, that's how it will work. I'll start it over here, and it will give me back these results over here."

Pictures would be very helpful for this one. Thanks.

Edit: I'm looking for something more granular than UML, specifically, something that will help me transition from a blocking and object-oriented programming structure, where I'm comfortable, to a non-blocking and event driven structure, where I'm unfamiliar.

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

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

发布评论

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

评论(2

哽咽笑 2024-10-18 13:01:26

基于 https://i.sstatic.net/9DDQP.png 你需要的是一个很好的流库,允许您在节点中管道同步和异步调用。

其中一个库是 https://github.com/isaacs/slide-flow-control (查看幻灯片预览也),您需要执行的操作的代码大纲如下。

它是自我文档化的,正如你所看到的,它非常简洁,不需要纯 Nodejs、uml、img 等。

var chain = require("slide/chain")
    , asyncMap = require("slide/async-map")
    ;

// start processing
main_loop(function() { 
    console.log("its done"); // when finished
});

function main_loop(cb) {
    var res = [];
    // each entry in chain below fires sequentially i.e. after
    // the previous function completes
    chain
        ( [ [start_update_q, "user-foo"]
          , [get_followed_users, chain.last]
          , [get_favorites, chain.last]
          , [calc_new_q]
          , [push_results, chain.last]
          ]
          , res
          , cb
        )
}

function get_favorites(users, cb) {
    function fn(user, cb_) {
        get_one_users_favorites(user, cb_);
    }
    // this will run thru get_favorites in parallel
    // and after all user favorites are gotten it will fire 
    // callback cb
    asyncMap(users, fn, cb);
}

// code in the various functions in chain here,
// remember to either return the callback on completion.
// or pass it as an arg to the async call you make within the
// function as above i.e. asyncMap will fire cb on completion

Based on https://i.sstatic.net/9DDQP.png what you need is a good flow library that allows you to pipeline sync and async calls in node.

One such library is https://github.com/isaacs/slide-flow-control (look at the slide preso there too) and the code outline for what you need to do is below.

It is self documenting and as you see it is quite concise, pure nodejs, uml, img's etc. not required.

var chain = require("slide/chain")
    , asyncMap = require("slide/async-map")
    ;

// start processing
main_loop(function() { 
    console.log("its done"); // when finished
});

function main_loop(cb) {
    var res = [];
    // each entry in chain below fires sequentially i.e. after
    // the previous function completes
    chain
        ( [ [start_update_q, "user-foo"]
          , [get_followed_users, chain.last]
          , [get_favorites, chain.last]
          , [calc_new_q]
          , [push_results, chain.last]
          ]
          , res
          , cb
        )
}

function get_favorites(users, cb) {
    function fn(user, cb_) {
        get_one_users_favorites(user, cb_);
    }
    // this will run thru get_favorites in parallel
    // and after all user favorites are gotten it will fire 
    // callback cb
    asyncMap(users, fn, cb);
}

// code in the various functions in chain here,
// remember to either return the callback on completion.
// or pass it as an arg to the async call you make within the
// function as above i.e. asyncMap will fire cb on completion
山有枢 2024-10-18 13:01:26

UML 可能比较合适。我会查看行为图。

UML might be appropriate. I'd look at the behavior diagrams.

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