mirror of
https://github.com/esiur/esiur-dart.git
synced 2026-04-02 09:38:20 +00:00
package
This commit is contained in:
58
lib/src/Core/AsyncBag.dart
Normal file
58
lib/src/Core/AsyncBag.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
import 'AsyncReply.dart';
|
||||
|
||||
class AsyncBag<T> extends AsyncReply<List<T>>
|
||||
{
|
||||
|
||||
List<AsyncReply<T>> _replies = new List<AsyncReply<T>>();
|
||||
List<T> _results = new List<T>();
|
||||
|
||||
int _count = 0;
|
||||
bool _sealedBag = false;
|
||||
|
||||
seal()
|
||||
{
|
||||
if (_sealedBag)
|
||||
return;
|
||||
|
||||
_sealedBag = true;
|
||||
|
||||
if (_results.length == 0)
|
||||
trigger(new List<T>());
|
||||
|
||||
for (var i = 0; i < _results.length; i++)
|
||||
{
|
||||
var k = _replies[i];
|
||||
var index = i;
|
||||
|
||||
k.then((r)
|
||||
{
|
||||
_results[index] = r;
|
||||
_count++;
|
||||
if (_count == _results.length)
|
||||
trigger(_results);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
add(AsyncReply<T> reply)
|
||||
{
|
||||
if (!_sealedBag)
|
||||
{
|
||||
_results.add(null);
|
||||
_replies.add(reply);
|
||||
}
|
||||
}
|
||||
|
||||
addBag(AsyncBag<T> bag)
|
||||
{
|
||||
bag._replies.forEach((r) {
|
||||
add(r);
|
||||
});
|
||||
}
|
||||
|
||||
AsyncBag()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user