Async asset handling for Kha. The code is heavily based on iron's Data class, but uses promises instead of callbacks. It also features a simple scoping (i.e. reference counting) mechanism. All assets are cached, whether already loaded or still in progress.
- tink_core_ext for
Promises.multi()
- tink_await if you prefer to use javascript style
@:await
instead of manually handling the promises
using tink.CoreApi;
var blobs = new kex.io.BlobIO();
blobs.get('a.txt').handle(function( o ) switch o {
case Success(blob):
trace(blob.toString());
case Failure(err):
});
(blobs.get('foo.txt') && blobs.get('bar.txt')).handle(function( o ) switch o {
case Success(data):
trace(data.a.toString()); // foo
trace(data.b.toString()); // bar
case Failure(err);
});
Promise.inParallel([
blobs.get('a.txt'),
blobs.get('b.txt'),
blobs.get('c.txt'),
]).handle(function( o ) switch o {
case Success(datas):
for (data in datas) {
trace(data.toString());
}
case Failure(err);
});
typedef Foo = {
final someInt: Int;
final someString: String;
}
blobs.get('foo.json').next(function( b ) {
var foo: Foo = tink.Json.parse(b.toString());
return foo;
}).handle(function( o ) switch o {
case Success(foo):
trace(foo.someInt);
trace(foo.someString);
case Failure(err):
});
Promise.inParallel([
blobs.get('a.txt', { scope: 'scope-a' }),
blobs.get('b.txt', { scope: 'scope-a' }),
blobs.get('b.txt', { scope: 'scope-b' }), // uses cached b.txt
blobs.get('c.txt', { scope: 'scope-c' }),
]).handle(function( o ) switch o {
case Success(datas):
blobs.unloadScope('scope-a');
// will unload `a.txt`
// keeps `b.txt` around, as it is also required in `scope-b`
case Failure(err);
});
Promises.multi({
hello: blobs.get('a.txt'),
world: blobs.get('b.txt'),
]).handle(function( o ) switch o {
case Success(datas):
trace(datas.hello);
trace(datas.world);
case Failure(err);
});
Use the initialization macro --macro kex.io.AssetLog.level(X)
to enable more verbose logging (default is errors
).
If you want to use some custom logging mechanism, define kex_io_custom_log
to disable the provided logger, add a kex.io.AssetLog
class somewhere in your classpath and define the 4 required macros (asset_debug
, asset_info
, asset_warn
, asset_err
).
X | name |
---|---|
0 | no logging code will be generated |
1 | errors |
2 | warnings |
3 | info |
4 | debug |
In case you don't supply a scope for your assets, the default *
is used.