private void ThrowIfUnhandled(BlockedRequest request) { if (!_fetchFunctions.ContainsKey(request.RequestType) && !_asyncFetchFunctions.ContainsKey(request.RequestType)) { RaiseLogEntry(Error($"No handler for request type '{request.RequestType}' found.")); throw new Exception($"No handler for request type '{request.RequestType}' found."); } }
public async Task <Response> Fetch(BlockedRequest request) { ThrowIfUnhandled(request); if (_fetchFunctions.ContainsKey(request.RequestType)) { var handler = _fetchFunctions[request.RequestType]; return(await Task.Factory.StartNew(() => { var result = handler(request); request.Resolver.SetResult(result.Value); RaiseLogEntry(Info($"Fetched '{request.BindName}': {result.Value}")); return result; })); } var asyncHandler = _asyncFetchFunctions[request.RequestType]; var response = await asyncHandler(request); request.Resolver.SetResult(response.Value); RaiseLogEntry(Info($"Fetched '{request.BindName}': {response.Value}")); return(response); }
public override Haxl ToHaxlFetch(string bindTo, Scope scope) { Func <Task <object>, Haxl> DoneFromTask = t => Haxl.FromFunc((c, l) => Done.New(_ => scope.Add(bindTo, WarnIfNull(t.Result, l)))); return(Haxl.FromFunc((cache, logger) => { var cacheResult = cache.Lookup(request); return cacheResult.Match <Result> ( notFound => { var blocked = new BlockedRequest(request, request.GetType(), bindTo); cache.Insert(request, blocked); return Blocked.New( new List <BlockedRequest> { blocked }, DoneFromTask(blocked.Resolver.Task) ); }, found => { var task = found.Resolver.Task; if (task.IsCompleted) { return Done.New(_ => { var result = task.Result; return scope.Add(bindTo, WarnIfNull(result, logger)); }); } return Blocked.New( new List <BlockedRequest>(), DoneFromTask(task) ); } ); })); }