Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a name for this pattern?

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.)

like image 315
Frank Nocke Avatar asked Oct 14 '22 22:10

Frank Nocke


2 Answers

It sounds like Balking pattern to me.

like image 75
reko_t Avatar answered Oct 16 '22 16:10

reko_t


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

like image 38
Kristopher Johnson Avatar answered Oct 16 '22 15:10

Kristopher Johnson