JSFiddle - React, Tailwind, and code Playground

by Danny Michaelis

JavaScript

function f1() {
    console.log("1");
}

function f2() {
    console.log("2");
}

function f3() {
    console.log("3");
}

function f4() {
    console.log("4");
}

function data_persister() {
    this.users_to_functions = {};
    this.add_user_and_function = function (user_name, update_function) {
        if (this.users_to_functions[user_name]) {
            var user_name_function_list = this.users_to_functions[user_name];
            user_name_function_list.push(update_function);
        } else {
            this.users_to_functions[user_name] = [update_function];
        }
    };
    this.update_all_elements_with_user_name = function (user_name) {
        var user_names_functions_list = this.users_to_functions[user_name];
        for(var index = 0; index < user_names_functions_list; index++){
            var update_function = user_names_functions_list[index];
            update_function();
        }
    };
}
var DP = new data_persister();
DP.add_user_and_function("Dan", f1);
DP.add_user_and_function("Dan", f2);
DP.add_user_and_function("Danny", f3);
DP.add_user_and_function("Danny", f4);
DP.update_all_elements_with_user_name("Dan");