<commit_before>import { createArray } from "../utils/objects";

export type RenderedResourceList = {
    [name: string]: string;
}

const resourceSigns: { 
    [name: string]: boolean 
} = {};

export function registerResources(rs: string[] | null | undefined) {
    if (rs)
        for (const r of rs)
            resourceSigns[r] = true;
}

export const getRenderedResources = () => Object.keys(resourceSigns)

export async function loadResourceList(resources: RenderedResourceList | undefined) {
    if (!resources) return;

    var html = "";
    for (const name of Object.keys(resources)) {
        if (!/^__noname_\d+$/.test(name)) {
            if (resourceSigns[name]) continue;
            resourceSigns[name] = true;
        }
        html += resources[name] + " ";
    }

    if (html.trim() == "") {
        return;
    }
    else {
        const tmp = document.createElement("div");
        tmp.innerHTML = html;
        const elements = <HTMLElement[]>createArray(tmp.children);
        await loadResourceElements(elements);
    }
}

async function loadResourceElements(elements: HTMLElement[]) {
    for (let element of elements) {
        let waitForScriptLoaded = false;
        if (element.tagName.toLowerCase() == "script") {
            let originalScript = <HTMLScriptElement>element;
            let script = <HTMLScriptElement>document.createElement("script");
            if (originalScript.src) {
                script.src = originalScript.src;
                waitForScriptLoaded = true;
            }
            if (originalScript.type) {
                script.type = originalScript.type;
            }
            if (originalScript.text) {
                script.text = originalScript.text;
            }
            if (element.id) {
                script.id = element.id;
            }
            element = script;
        }
        else if (element.tagName.toLowerCase() == "link") {
            // create link
            var originalLink = <HTMLLinkElement>element;
            var link = <HTMLLinkElement>document.createElement("link");
            if (originalLink.href) {
                link.href = originalLink.href;
            }
            if (originalLink.rel) {
                link.rel = originalLink.rel;
            }
            if (originalLink.type) {
                link.type = originalLink.type;
            }
            element = link;
        }

        // load next script when this is finished
        const loadPromise = waitForElementLoaded(element);
        if (waitForScriptLoaded) {
            await loadPromise;
        }
    }
}


function waitForElementLoaded(element: HTMLElement) {
    return new Promise(resolve => {
        element.addEventListener("load", resolve);
        element.addEventListener("error", () => {
            console.warn(`Error loading resource`, element);
            resolve();
        });
    });
}
<commit_msg>JS Modules:Fixed bug in postbacks with resources<commit_after>import { createArray } from "../utils/objects";

export type RenderedResourceList = {
    [name: string]: string;
}

const resourceSigns: { 
    [name: string]: boolean 
} = {};

export function registerResources(rs: string[] | null | undefined) {
    if (rs)
        for (const r of rs)
            resourceSigns[r] = true;
}

export const getRenderedResources = () => Object.keys(resourceSigns)

export async function loadResourceList(resources: RenderedResourceList | undefined) {
    if (!resources) return;

    var html = "";
    for (const name of Object.keys(resources)) {
        if (!/^__noname_\d+$/.test(name)) {
            if (resourceSigns[name]) continue;
            resourceSigns[name] = true;
        }
        html += resources[name] + " ";
    }

    if (html.trim() == "") {
        return;
    }
    else {
        const tmp = document.createElement("div");
        tmp.innerHTML = html;
        const elements = <HTMLElement[]>createArray(tmp.children);
        await loadResourceElements(elements);
    }
}

async function loadResourceElements(elements: HTMLElement[]) {
    for (let element of elements) {
        let waitForScriptLoaded = false;
        if (element.tagName.toLowerCase() == "script") {
            let originalScript = <HTMLScriptElement>element;
            let script = <HTMLScriptElement>document.createElement("script");
            if (originalScript.src) {
                script.src = originalScript.src;
                waitForScriptLoaded = true;
            }
            if (originalScript.type) {
                script.type = originalScript.type;
            }
            if (originalScript.text) {
                script.text = originalScript.text;
            }
            if (element.id) {
                script.id = element.id;
            }
            element = script;
        }
        else if (element.tagName.toLowerCase() == "link") {
            // create link
            var originalLink = <HTMLLinkElement>element;
            var link = <HTMLLinkElement>document.createElement("link");
            if (originalLink.href) {
                link.href = originalLink.href;
            }
            if (originalLink.rel) {
                link.rel = originalLink.rel;
            }
            if (originalLink.type) {
                link.type = originalLink.type;
            }
            element = link;
        }

        // load next script when this is finished
        const loadPromise = waitForElementLoaded(element);
        document.head.appendChild(element);
        if (waitForScriptLoaded) {
            await loadPromise;
        }
    }
}


function waitForElementLoaded(element: HTMLElement) {
    return new Promise(resolve => {
        element.addEventListener("load", resolve);
        element.addEventListener("error", () => {
            console.warn(`Error loading resource`, element);
            resolve();
        });
    });
}

{'fraction_non_alphanumeric': 0.11320115234705982, 'fraction_numerical': 0.0, 'mean_word_length': 1.9188921859545005, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4555290', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1534, 'n_words': 343}