public async Task <T> Run <T>(Func <T> method, CancellationToken cancellationToken = default) { var future = new SynchronousFuture <T>(method, cancellationToken); await _channel.Writer.WriteAsync(future, cancellationToken).ConfigureAwait(false); return(await future.Completed.ConfigureAwait(false)); }
public override Future <T> loadStructuredData <T>(string key, Func <string, Future <T> > parser) { D.assert(key != null); D.assert(parser != null); if (_structuredDataCache.ContainsKey(key)) { return(_structuredDataCache[key].to <T>()); } Completer completer = null; Future <T> result = null; loadString(key, cache: false).then_ <T>(value => parser(value)).then_ <object>((T value) => { result = new SynchronousFuture <T>(value); _structuredDataCache[key] = result; if (completer != null) { // We already returned from the loadStructuredData function, which means // we are in the asynchronous mode. Pass the value to the completer. The // completer's future is what we returned. completer.complete(FutureOr.value(value)); } return(FutureOr.nil); }); if (result != null) { // The code above ran synchronously, and came up with an answer. // Return the SynchronousFuture that we created above. return(result); } // The code above hasn't yet run its "then" handler yet. Let's prepare a // completer for it to use when it does run. completer = Completer.create(); _structuredDataCache[key] = result = completer.future.to <T>(); return(result); }
public override Future <_SizeAwareCacheKey> obtainKey(ImageConfiguration configuration) { Completer completer = null; SynchronousFuture <_SizeAwareCacheKey> result = null; imageProvider.obtainKey(configuration).then((object key) => { // TODO: completer is always null? if (completer == null) { result = new SynchronousFuture <_SizeAwareCacheKey>(new _SizeAwareCacheKey(key, width, height)); } else { completer.complete(FutureOr.value(new _SizeAwareCacheKey(key, width, height))); } }); if (result != null) { return(result); } completer = Completer.create(); return(completer.future.to <_SizeAwareCacheKey>()); }