<commit_before>
import debug from 'debug/browser'
let log = debug('scintillator:loader')

import $ from 'jquery'
import url from 'url'
import co from 'co'
import PIXI from 'pixi.js'

import Resources from './resources'
import Compiler from './compiler'

export function load(xmlPath, task) {
  return co(function*() {

    let notify = task ? o => task.update(o) : () => {}

    log('load XML from %s', xmlPath)
    let $xml = yield loadXml(xmlPath)

    // scan all images
    let resources = new Resources()
    let images = Array.from($xml.children('image'))
    for (let image of images) {
      let src = $(image).attr('src')
      let imageUrl = url.resolve(xmlPath, src)
      resources.add(src, imageUrl)
    }

    // load all images + progress reporting
    log('loading resources')
    let loaded  = 1
    let total   = 1 + images.length
    let update  = () => notify({ progress: loaded / total })
    update()
    yield loadResources(resources, () => { loaded += 1; update() })

    // compile the skin
    log('compiling')
    let skin = new Compiler({ resources }).compile($xml)

    return skin

  })
}

function loadXml(url) {
  return Promise.resolve($.ajax({ url: url, dataType: 'xml' }))
    .then(xml => $(xml.documentElement))
}

function loadResources(resources, onprogress) {
  log('loading resources')
  return new Promise(function(resolve) {
    if (resources.urls.length === 0) return resolve()
    let loader = new PIXI.AssetLoader(resources.urls)
    loader.on('onComplete', function() {
      log('resources finished loading')
      resolve()
    })
    if (onprogress) loader.on('onProgress', onprogress)
    loader.load()
  })
}
<commit_msg>feat(scintillator/loader): more detailed progress reporting<commit_after>
import debug from 'debug/browser'
let log = debug('scintillator:loader')

import $ from 'jquery'
import url from 'url'
import co from 'co'
import PIXI from 'pixi.js'

import Resources from './resources'
import Compiler from './compiler'

export function load(xmlPath, task) {
  return co(function*() {

    let notify = task ? o => task.update(o) : () => {}

    log('load XML from %s', xmlPath)
    let $xml = yield loadXml(xmlPath)

    // scan all images
    let resources = new Resources()
    let images = Array.from($xml.children('image'))
    for (let image of images) {
      let src = $(image).attr('src')
      let imageUrl = url.resolve(xmlPath, src)
      resources.add(src, imageUrl)
    }

    // load all images + progress reporting
    log('loading resources')
    let loaded  = 1
    let total   = 1 + images.length
    let update  = () => notify({
                    progress: loaded / total,
                    current:  loaded + '',
                    total:    total + ' resources',
                  })
    update()
    yield loadResources(resources, () => { loaded += 1; update() })

    // compile the skin
    log('compiling')
    let skin = new Compiler({ resources }).compile($xml)

    return skin

  })
}

function loadXml(url) {
  return Promise.resolve($.ajax({ url: url, dataType: 'xml' }))
    .then(xml => $(xml.documentElement))
}

function loadResources(resources, onprogress) {
  log('loading resources')
  return new Promise(function(resolve) {
    if (resources.urls.length === 0) return resolve()
    let loader = new PIXI.AssetLoader(resources.urls)
    loader.on('onComplete', function() {
      log('resources finished loading')
      resolve()
    })
    if (onprogress) loader.on('onProgress', onprogress)
    loader.load()
  })
}

{'fraction_non_alphanumeric': 0.1398184912081679, 'fraction_numerical': 0.0022688598979013048, 'mean_word_length': 2.976324689966178, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, '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': '4482063', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1037, 'n_words': 311}