Skip to content

Library download and js scripts queue manager for complex javascript ecosystems. Handle priorities, hierarchies, dependencies among your internal and external libraries and third party website components

Notifications You must be signed in to change notification settings

giegi/Traffic-Light-Handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 

Repository files navigation

Traffic Light Handler - TLH


You might never need this tool in your whole programmer life. Most of the web developers won't have the need to use a queue/library download manager to handle which library to load first, and which libraries need to be waited before downloading another one. Many developers won't need to wait data from three, four, five other javascript sources.
But if you might need to do this, tlh is right for you.
TLH is a js/css library download handler for complex javascript ecosystems.

Let's draw a scenario.
An analytics system, in order to work properly, needs to have available a serie of objects and informations in page. These objects have different sources. A javascript library, the result of an asynchronous ajax call, the acceptance of the Gdpr Infoprivacy, the analytics sdk, its initialization, an execution of a script
Putting in order the script tags in the head tag is not enough to be sure that everything will work in the correct sequence. TLH does this for you.
Let's see a pratical example.

We have four libraries, A, B, C, D We have three scripts, Z, W and X A can be downloaded to initiate the chain (or A must wait the DomContentLoaded to be downloaded) Z needs A to be downloaded. B to be downloaded needs: Z to be executed and A to be downloaded C to be downloaded needs: B to be downloaded and executed X to be executed needs C to be downloaded and executed D needs everything before downloaded and executed to be downloaded. At the end of the execution of D, script W can be executed

  1. We create A TLH object.
  2. We create a Z TLH Object. We set A as dependency of Z
  3. We create B TLH object. We set object A to be a library dependency of B. We set object Z to be a dependency of B
  4. We create a C TLH Object. We set B as a dependency of C. Do not need to specify dependency from A and Z because already satisfied from the previous dependencies of B.
  5. We create a X TLH Object. We set C as dependency of X.
  6. We create a D TLH Object. We set X as dependency of D. No need to specify any other dependency as previous dependencies satisfy the needs.
In a so made scenario, Trafic Light Handler allows you to set complex hierarchies of objects. Every TLH object can be a library to download, a script to execute, an empty container, or both the firsts. For every TLH Object a couple of things can be set:
  • its core: a library to download or a script to execute
  • a callback function when library has been successfully downloaded (or script properly executed)
  • a callback function when library download or script execution caused an error
  • a callback function when the object is complete (lib downloaded or script executed, no other dependencies still active)
  • if an error occurs a "continue on failure" label can be set

To every TLH Object can be assigned two kinds of dependencies:

  • a dependency that blocks execution of the core (lib or script) _> core dependency
  • a dependency that blocks the state of the object (completed or not) after the core execution _> object dependency
Both kind of dependencies can be set at the same time.

As a dependency, there are many object can be assigned:

  • another TLH Object (resolves when it completes its state)
  • a condition to test
  • an empty dependency that has a label and must be resolved manually with a line of code
When created, an object doesn't do anything. It must be executed manually or be activated from one of its dependencies that change its own state and communicates this to its dependencies. When executed or activated, every tlh object analyzes its core dependencies and, if all of them are satisfied, it can download library or execute core script, and then, after verifying the object dependencies, completes the state and executes final callback. If one of the dependencies isn't satisfied, the object waits until it is.

SETUP CONFIGS => tlhControlObject(obj_cb, url_or_function, cb_success, cb_error, continueOnFailure)

kw_tlh_configs.adsetup = tlhControlObject(null, undefined, null, null, null);
kw_tlh_configs.nielsenStatic = tlhControlObject(null, "https://www.example.it/nielsen/nielsen_static.js", null, null, true);
kw_tlh_configs.chartbeat = tlhControlObject(function() { window.loadChartbeat(); }, "https://www.example.it/chartbeat/chartbeat.js", null, null, true);
kw_tlh_configs.webtrekk_mapping = tlhControlObject(null, "https://www.example.it/wt/wt_mapping_script.js?pageurl=blablablacurrentpage", null, null, true);
kw_tlh_configs.webtrekk = tlhControlObject(window.kw_webtrekk_complete, "https://www.exampe.it//config_webtrekk.php", window.kw_run_webtrekk, null, true);

SETUP OBJECTS ANBD DEPENDENCIES
window.kw_tlh.adsetup = new tlhl("adsetup", kw_tlh_configs.adsetup);
window.kw_tlh.adsetup.addRedLight("adsetupreal");
window.kw_tlh.adsetup.addRedLight("infoprivacy", function() { return window.kwdnt !== undefined; });
window.kw_tlh.nielsenStatic = new tlhl("nielsenStatic", kw_tlh_configs.nielsenStatic);
window.kw_tlh.nielsenStatic.execute();
window.kw_tlh.webtrekk_mapping = new tlhl("webtrekk_mapping", kw_tlh_configs.webtrekk_mapping);
window.kw_tlh.webtrekk_mapping.addLibRedLight("adsetup", window.kw_tlh.adsetup);
window.kw_tlh.webtrekk = new tlhl("webtrekk", kw_tlh_configs.webtrekk);
window.kw_tlh.webtrekk.addLibRedLight("webtrekk_mapping", window.kw_tlh.webtrekk_mapping);
window.kw_tlh.webtrekk.addLibRedLight("adsetup", window.kw_tlh.adsetup);
window.kw_tlh.webtrekk.addRedLight("wt_init");
window.kw_tlh.webtrekk.addRedLight("wt_send");
window.kw_tlh.chartbeat = new tlhl("chartbeat", kw_tlh_configs.chartbeat);
window.kw_tlh.chartbeat.addLibRedLight("webtrekk_mapping", window.kw_tlh.webtrekk_mapping);

About

Library download and js scripts queue manager for complex javascript ecosystems. Handle priorities, hierarchies, dependencies among your internal and external libraries and third party website components

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published