public static Future <R> compute <Q, R>(ComputeCallback <Q, R> callback, Q message, string debugLabel = null) { var completer = Completer.create(); var isolate = Isolate.current; var backgroundWorker = new BackgroundWorker(); backgroundWorker.DoWork += (sender, args) => { args.Result = callback((Q)args.Argument); }; backgroundWorker.RunWorkerCompleted += (o, a) => { if (!isolate.isValid) { return; } using (Isolate.getScope(isolate)) { if (a.Error != null) { completer.completeError(a.Error); } else { completer.complete(FutureOr.value((R)a.Result)); } } }; backgroundWorker.RunWorkerAsync(message); return(completer.future.to <R>()); }
public override void dispose() { D.assert(!_transitionCompleter.isCompleted, () => $"Cannot dispose a {GetType()} twice."); _controller?.dispose(); _transitionCompleter.complete(FutureOr.value(_result)); base.dispose(); }
internal static Future <Dictionary <Type, object> > _loadAll( Locale locale, IEnumerable <LocalizationsDelegate> allDelegates) { Dictionary <Type, object> output = new Dictionary <Type, object>(); List <_Pending> pendingList = null; HashSet <Type> types = new HashSet <Type>(); List <LocalizationsDelegate> delegates = new List <LocalizationsDelegate>(); foreach (LocalizationsDelegate del in allDelegates) { if (!types.Contains(del.type) && del.isSupported(locale)) { types.Add(del.type); delegates.Add(del); } } foreach (LocalizationsDelegate del in delegates) { Future <object> inputValue = del.load(locale).to <object>(); object completedValue = null; Future <object> futureValue = inputValue.then_(value => { completedValue = value; return(FutureOr.value(completedValue)); }).to <object>(); if (completedValue != null) { Type type = del.type; D.assert(!output.ContainsKey(type)); output[type] = completedValue; } else { pendingList = pendingList ?? new List <_Pending>(); pendingList.Add(new _Pending(del, futureValue)); } } if (pendingList == null) { return(new SynchronousFuture <Dictionary <Type, object> >(output)); } return(Future.wait <object>(LinqUtils <Future <object>, _Pending> .SelectList(pendingList, (p => p.futureValue))) .then(values => { var list = (List <object>)values; D.assert(list.Count == pendingList.Count); for (int i = 0; i < list.Count; i += 1) { Type type = pendingList[i].del.type; D.assert(!output.ContainsKey(type)); output[type] = list[i]; } return output; }).to <Dictionary <Type, object> >()); }
public static Future <ui.Image> decodeImageFromList(byte[] bytes) { Future <Codec> codec = PaintingBinding.instance.instantiateImageCodec(bytes); Future <FrameInfo> frameInfo = codec.then_ <FrameInfo>(code => code.getNextFrame()); var result = frameInfo.then_ <Image>(frame => FutureOr.value(frame.image)); return(result); }
public override Future whenComplete(Func <FutureOr> action) { try { FutureOr result = action(); if (result.isFuture) { return(result.f.then((value) => FutureOr.value(_value))); } return(this); } catch (Exception e) { return(error(e)); } }
Future <byte[]> _sendPlatformMessage(string channel, byte[] message) { Completer completer = Completer.create(); Window.instance.sendPlatformMessage(channel, message, (reply) => { try { completer.complete(FutureOr.value(reply)); } catch (Exception exception) { UIWidgetsError.reportError(new UIWidgetsErrorDetails( exception: exception, library: "services library", context: new ErrorDescription("during a platform message response callback") )); } }); return(completer.future.to <byte[]>()); }
Future <Codec> _loadAsync(NetworkImage key, DecoderCallback decode) { var completer = Completer.create(); var isolate = Isolate.current; var panel = UIWidgetsPanelWrapper.current.window; if (panel.isActive()) { panel.startCoroutine(_loadCoroutine(key.url, completer, isolate)); return(completer.future.to <byte[]>().then_ <byte[]>(data => { if (data != null && data.Length > 0) { return decode(data); } throw new Exception("not loaded"); }).to <Codec>()); } return(new Future <Codec>(Future.create(() => FutureOr.value(null)))); }
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>()); }
protected override Future <ClipboardData> getClipboardData(string format) { var data = new ClipboardData(text: GUIUtility.systemCopyBuffer); return(Future.value(FutureOr.value(data)).to <ClipboardData>()); }
public override Future timeout(TimeSpan timeLimit, Func <FutureOr> onTimeout = null) { return(value(FutureOr.value(_value)).timeout(timeLimit, onTimeout: onTimeout)); }
void load(Locale locale) { var delegates = widget.delegates; if (delegates == null || delegates.isEmpty()) { _locale = locale; return; } Dictionary <Type, object> typeToResources = null; Future <Dictionary <Type, object> > typeToResourcesFuture = _Pending._loadAll(locale, delegates) .then(value => { return(FutureOr.value(typeToResources = (Dictionary <Type, object>)value)); }).to <Dictionary <Type, object> >(); if (typeToResources != null) { _typeToResources = typeToResources; _locale = locale; } else { typeToResourcesFuture.then(value => { if (mounted) { setState(() => { _typeToResources = (Dictionary <Type, object>)value; _locale = locale; }); } }); } }