Fire onComplete signal with zero length resources list
koutoftimer opened this issue · comments
Suppose we have app structure where each "stage's child" has its own resource list. It this case to load all required resources we need to call loader.add
on the list of unknown length and in case of zero length list there is no other choice to track resource loading rather additional checks on empty list manually.
It is a more intuitive way to fire onComplete
signal when all resources are loaded or resource list is empty.
Related to #98 , because subresources by themselves could have zero assert list, but their children could have a lot of required assets.
Example (live code snippet):
class GameScene extends BaseScene {
loadResources() {
// Suppose we want to make sure that all resources are loaded to continue.
// Lets return a promise that will call `resolve`.
return new Promise((resolve, reject) => {
// Collect all required resources for current scene.
let resourcesList = this.children
.map((child) => child['resourcesList'] || [])
.reduce((a, c) => a.concat(c))
// Check resources lenght and if it is not empty start loading process
if (resourcesList.lenght) {
// Add `onComplete` handler to call `resolve` when all resources will
// be loaded
loader.add(resourcesList)
.load(() => {
// DUPLICATE
this.setup()
resolve()
})
}
// else just call resolve
else {
// DUPLICATE
this.setup()
resolve()
}
})
}
}
To make sure I understand what you are asking for: you want onComplete to fire if you call .load()
without having called .add()
at all before?
If .add
receives an array, I'd like to have this
loadResources() {
return new Promise((resolve, reject) => {
loader.add(this.resources)
.load((loader, resources) => {
resolve(loader, resources)
})
})
}
to work like that
loadResources() {
return new Promise((resolve, reject) => {
let resources = this.resources
resources.length == 0
? resolve(null, null)
: loader.add(resources).load((loader, resources) => {
resolve(loader, resources)
})
})
}
without ternary operator with empty list of zero length, so .add()
should be able to fire onComplete
event on execution of the following code loader.add([]).load(() => { })
. As you can see, .add
is called before .load
but function inside it didn't get called.
Makes sense, can you take a look at #107 and see if that fixes your issue? I added some tests that pass, but want to make sure I am hitting your use case properly.
Thanks!
I'm using resource-loader
as part of pixi.js
and I'm lazy to rebuilt it with your changes, right now.
I'm not confident about what is async.queue
, but logic is absolutely right. I suppose Queue.idle
is a getter that returns true
if the queue is empty.
Thank you for so fast response and pull request, especially on holidays and Happy New Year.
I'm not confident about what is async.queue, but logic is absolutely right. I suppose Queue.idle is a getter that returns true if the queue is empty.
https://github.com/englercj/resource-loader/blob/master/src/async.js#L120-L122
Thank you for so fast response and pull request, especially on holidays and Happy New Year.
No problem, I'll get this merged and released soon.
Closed by 7e09257