<commit_before>import resource
import tables
import json
import strutils
import image
import streams
import system_logger
import types
import sequtils

type ResourceLoader* = ref object
    totalSize : int
    loadedSize: int
    itemsToLoad: int
    onComplete*: proc()
    resourcesToLoad: seq[string]

proc getFileExtension(name: string): string =
    let p = name.rfind('.')
    if p != -1:
        result = name.substr(p + 1)

proc onResourceLoaded(ld: ResourceLoader) =
    dec ld.itemsToLoad
    if ld.itemsToLoad == 0:
        ld.onComplete()

proc startPreloadingResource(ld: ResourceLoader, name: string) =
    case name.getFileExtension()
    of "png", "jpg", "jpeg", "gif", "tif", "tiff", "tga":
        when defined(js):
            proc handler(r: ref RootObj) =
                let image = imageWithSize(newSize(0, 0))
                {.emit: """
                var imgUrl = window.URL.createObjectURL(`r`);
                var im = new Image();
                im.src = imgUrl;
                `image`.__image = im;
                """.}
                registerImageInCache(name, image)
                ld.onResourceLoaded()

            loadJSResourceAsync(name, "blob", nil, nil, handler)
        else:
            registerImageInCache(name, imageWithResource(name))<commit_msg>Fixed async image preloading in JS<commit_after>import resource
import tables
import json
import strutils
import image
import streams
import system_logger
import types
import sequtils

type ResourceLoader* = ref object
    totalSize : int
    loadedSize: int
    itemsToLoad: int
    onComplete*: proc()
    resourcesToLoad: seq[string]

proc getFileExtension(name: string): string =
    let p = name.rfind('.')
    if p != -1:
        result = name.substr(p + 1)

proc onResourceLoaded(ld: ResourceLoader) =
    dec ld.itemsToLoad
    if ld.itemsToLoad == 0:
        ld.onComplete()

proc startPreloadingResource(ld: ResourceLoader, name: string) =
    case name.getFileExtension()
    of "png", "jpg", "jpeg", "gif", "tif", "tiff", "tga":
        when defined(js):
            proc handler(r: ref RootObj) =
                var onImLoad = proc (im: ref RootObj) =
                    var w, h: Coord
                    {.emit: "`w` = im.width; `h` = im.height;".}
                    let image = imageWithSize(newSize(w, h))
                    {.emit: "`image`.__image = im;".}
                    registerImageInCache(name, image)
                    ld.onResourceLoaded()
                {.emit:"""
                var im = new Image();
                im.onload = function(){`onImLoad`(im);};
                im.src = window.URL.createObjectURL(`r`);
                """.}

            loadJSResourceAsync(name, "blob", nil, nil, handler)
        else:
            registerImageInCache(name, imageWithResource(name))
{'fraction_non_alphanumeric': 0.1152073732718894, 'fraction_numerical': 0.002835873803615739, 'mean_word_length': 2.16013437849944, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '4857206', 'n_tokens_mistral': 840, 'n_tokens_neox': 801, 'n_words': 223}