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
- We create A TLH object.
- We create a Z TLH Object. We set A as dependency of Z
- 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
- 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.
- We create a X TLH Object. We set C as dependency of X.
- 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.
- 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
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
SETUP CONFIGS => tlhControlObject(obj_cb, url_or_function, cb_success, cb_error, continueOnFailure)
kw_tlh_configs.adsetup = tlhControlObject(null, undefined, null, null, null);SETUP OBJECTS ANBD DEPENDENCIES
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);
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);