这种模式有名字吗?

发布于 2024-09-26 21:58:48 字数 1218 浏览 0 评论 0原文

我基本上非常确定这种模式一定存在并拥有一个名称...现在我将其称为“门模式”...

这里是:

在我的网页的 javascript 中,我必须触发各种异步进程。我们不讨论真正的异步js,但无论如何我必须触发2或3个AJAX调用,必须确定,UI构建已经完成,等等。

只有这样,当所有这些过程完成后,我才想运行某个函数。并且恰好一次。

示例

1: cropStore loaded()
2: resizeEvent()
3: productStore loaded()

模式: 在每次(成功的)Ajax加载回调结束时,GUI构建例程结束时,等等...我将相应的标志从 false 设置为 true 并调用 atedAction()

onEvent( 'load',
{
   ....  // whatever has to happen in response to cropStored, resized, etc...
   // lastly:
   f1 = true; //resp f2, f3, ...
   gatedAction();
}

Gate 将检查标志,如果有标志则返回仍未设置,仅在所有标志(或我称之为:门)打开的情况下调用目标函数。如果我所有的异步先决条件都调用了 atedAction() 一次,我希望我可以确定,实际的 targetFunction 被调用了一次()。

gatedAction ()
{
   // Gate
   if ( ! f1) return;
   if ( ! f2) return;
   if ( ! f3) return;

   // actual Action ( <=> f1==f2==f3==true )
   targetFunction();
}

实际上它工作可靠。旁注:我认为 java 典型(不是 js 典型)同步/易失性问题可以忽略,因为 javascript 并不是真正的多线程。 Afaik 一个函数永远不会在中间停止,只是为了在同一文档运行时授予另一个 javascript 函数...

那么,有人知道这个有名字吗? :-)

我实际上经常需要这种模式,特别是对于复杂的后端 UI..(是的,我想,我会将上面的丑陋实现变成一个更可重用的 JavaScript...使用门数组和目标函数.)

I am basically quite sure this pattern must exist and possess a name... for now I will call it "gate pattern"...

Here it is:

In my webpage's javascript, I have to trigger various asynchronous processes. Let's not discuss how trully async js is, but anyway I have to trigger 2 or 3 AJAX calls, must be sure, the UI build-up has finished, and so on.

Only then, when all these processes have finished, I want to do run a certain function. And precisely once.

Example

1: cropStore loaded()
2: resizeEvent()
3: productStore loaded()

The Pattern:
At the end of every (sucessful) Ajax-load-callback, the end of the GUI construction routine, etc... I set a respective flag from false to true and call gatedAction()

onEvent( 'load',
{
   ....  // whatever has to happen in response to cropStored, resized, etc...
   // lastly:
   f1 = true; //resp f2, f3, ...
   gatedAction();
}

Gate will check the flags, return if any flag is still unset, only calling the target function, if all flags (or as I call them: gates) are open. If all my async pre-conditions call gatedAction() exactly once, I hope I can be sure, the actual targetFunction is called exactly once().

gatedAction ()
{
   // Gate
   if ( ! f1) return;
   if ( ! f2) return;
   if ( ! f3) return;

   // actual Action ( <=> f1==f2==f3==true )
   targetFunction();
}

In practice it works reliably. On a side-note: I think java-typical (not js-typical) synchronization/volatile concerns can be ignored, because javascript is not truly multithreading. Afaik a function is never stopped in the middle of it, just to grant another javascript function in the same document run-time...

So, anyone, is there a name for this? :-)

I need this pattern actually quite often, especially with complex backend UIs.. (and yes, I think, I will turn the above butt-ugly implementation into a more reusable javascript... With a gates array and a target function.)

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

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

发布评论

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

评论(4

只是我以为 2024-10-03 21:58:48

对我来说,这听起来像是犹豫模式

It sounds like Balking pattern to me.

八巷 2024-10-03 21:58:48

它类似于 Rendezvous 模式,尽管该模式通常在上下文中使用多线程实时系统。

It is similar to the Rendezvous pattern, although that pattern is generally used in the context of multithreaded real-time systems.

∞梦里开花 2024-10-03 21:58:48

我不知道你的模式是否有一个特殊的名称,但它似乎相当于只使用一个计数信号量,它会阻塞启动所有其他操作的线程,直到它们都进行 V 调用。当然,JavaScript 中没有线程和信号量,但您可以只使用一个整数进行计数,而不是使用许多布尔变量。

I have no idea, if your pattern has a special name, but it seems equivalent to just using a counting semaphore, which blocks the thread, which started all those other actions, until they all made a V-invocation. Of course, there are no threads and semaphores in JavaScript, but instead of using many boolean variables you could use just one integer for counting.

叹沉浮 2024-10-03 21:58:48

除了问题的实际答案之外,您可能对 Javascript 的 Rx 框架感兴趣。它是 .NET 版本的一个端口,允许您编写事件,因此您不必使用大量的标志变量。它是为这类事情而设计的。

http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx

In addition to the actual answer to your question, you might be interested in the Rx framework for Javascript. It's a port of the .NET version and allows you to compose events, so you don't have to work with tons of flag variables. It's meant for this sort of thing.

http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx

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