Skip to content

Instantly share code, notes, and snippets.

@emilhein
Created March 25, 2022 12:33
Show Gist options
  • Save emilhein/c3c06b408a9d03e624fe4779aaef8cea to your computer and use it in GitHub Desktop.
Save emilhein/c3c06b408a9d03e624fe4779aaef8cea to your computer and use it in GitHub Desktop.
//CLIENT SIDE
var somelib = somelib || {};
somelib.queue = somelib.queue || [];
let pushObjectCorrect = { cmd: "someFunc1", args: "I am queeeued before the script is laoded" };
let pushObjectCorrect22 = { cmd: "someFunc1", args: "I am queeeued before the script is laoded" };
somelib.queue.push(pushObjectCorrect);
somelib.queue.push(pushObjectCorrect22);
let pushObjectWrong = { cmd: "noFunction", args: "myInput" };
somelib.queue.push(pushObjectWrong);
//**LIBRARY LOADED */
var somelib = (function () {
let internalLib = {
someFunc1: (msg) => console.log(`I AM CALLED with ---- > ${msg}`)
}
const runInternalFunction = (command) => {
if (!internalLib[command.cmd]) {
console.log(`could not find function ${command.cmd}`, 'error')
} else {
internalLib[command.cmd](command.args)
}
}
const immidateExecuteQueueItems = () => {
somelib.queue = {};
somelib.queue.push = function (command) {
runInternalFunction(command);
};
Object.freeze(somelib);
}
const buildlibAndCmdQueue = () => {
if (somelib === null) {
somelib = {};
immidateExecuteQueueItems();
return somelib;
}
if (typeof somelib == 'object' && Array.isArray(somelib.queue)) {
somelib.queue.forEach((command) => runInternalFunction(command));
immidateExecuteQueueItems();
return somelib;
}
throw new Error('Something wen wrong.');
}
var somelibApi = buildlibAndCmdQueue();
return somelibApi;
})();
/*************************************** END OF LIB */
//CLIENT SIDE
let pushObjectCorrect2 = { cmd: "someFunc1", args: "I am queued after the script is laoded" };
somelib.queue.push(pushObjectCorrect2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment