/// <summary> /// Refreshes if the prefix has timed out and/or the localkey has timed out. /// Flushes all items if the time out has occurred /// Returns true if data was retrieved /// </summary> /// </summary> public static async Task <bool> WithTimedRefreshForPrefixAsync <T>(this IDataCache dataCache, bool allowStale, string prefixKey, string localKey, int maximumStaleSeconds, FetchedDelegate <T> onRefreshed, Action <bool> onRefreshing, Func <Task <T> > createMethod) where T : class { TimedDataCacheFilter timeFilter = EnsureTimedLifetimeFilter(dataCache); bool forceRefresh = (maximumStaleSeconds <= 0); if (!forceRefresh) { // use the passed in time to override the default forceRefresh = timeFilter.RefreshRequired(prefixKey, maximumStaleSeconds); } if (forceRefresh) { timeFilter.AddClearBeforeSave(localKey, prefixKey); } FetchedDelegate <T> onRefreshedWrapper = (freshData, data) => { if (freshData) { dataCache.AddToCache(prefixKey, data); timeFilter.OnAfterItemSavedToCache(dataCache, prefixKey, data); } if (onRefreshed != null) { onRefreshed(freshData, data); } }; return(await dataCache.WithRefreshAsync <T>(prefixKey + localKey, allowStale, forceRefresh, onRefreshedWrapper, onRefreshing, createMethod)); }
private ClrFunctionInstance WrapDelegate(FetchedDelegate _delegate) { return(Engine.AsJsFunction(new Func <JsValue, JsValue[], JsValue>((thisObject, args) => { _delegate(((args[0].IsNull() || args[0].IsUndefined()) ? null : ManticoreException.NativeInstanceForObject(args[0].AsObject())), Engine.Converter.AsNativeObject(args[1])); return JsValue.Undefined; }))); }
} /** * Fetch some JSON from httpbin.org */ public void GoFetch(FetchedDelegate callback) { JsValue[] args = new JsValue[] { WrapDelegate(callback) }; var func = this.impl.Get("goFetch").As <FunctionInstance>(); Engine.Js(() => { func.Call(this.impl, args); }); } /**
public static async Task <bool> WithTimedRefreshAsync <T>(this IDataCache dataCache, string key, int maximumStaleSeconds, FetchedDelegate <T> onRefreshed, Action <bool> onRefreshing, Func <Task <T> > createMethod) where T : class { TimedDataCacheFilter timeFilter = EnsureTimedLifetimeFilter(dataCache); bool forceRefresh = (maximumStaleSeconds <= 0); bool allowStale = (maximumStaleSeconds != 0); if (!forceRefresh) { // use the passed in time to override the default forceRefresh = timeFilter.RefreshRequired(key, maximumStaleSeconds); } return(await dataCache.WithRefreshAsync <T>(key, allowStale, forceRefresh, onRefreshed, onRefreshing, createMethod)); }
private Action <dynamic, dynamic> WrapDelegate(FetchedDelegate _delegate) { return(new Action <dynamic, dynamic>((error, response) => { _delegate((Engine.IsNullOrUndefined(error) ? null : ManticoreException.NativeInstanceForObject(error)), Engine.Converter.AsNativeObject(response)); })); }
/** * Fetch some JSON from httpbin.org */ public void GoFetch(FetchedDelegate callback) { Engine.Js(() => { this.impl.goFetch(WrapDelegate(callback)); }); }
/// <summary> /// Fetches scalar data with support for cached data and timeouts /// </summary> protected virtual Task <bool> FetchItemAsync <T>(bool allowStale, string cachePrefixKey, string cacheSpecificKey, int staleLimit, FetchedDelegate <ItemResult <T> > onFetched, Action <bool> onFetching, Func <Task <ItemResult <T> > > fetchMethod) { try { return(this.DataCache.WithTimedRefreshForPrefixAsync <ItemResult <T> >(allowStale, cachePrefixKey, cacheSpecificKey, staleLimit, onFetched, onFetching, async delegate() { return await fetchMethod(); })); } catch (Exception ex) { EndpointException endpointException = ex.FirstExceptionOfType <EndpointException>(); if (endpointException != null) { switch (endpointException.StatusCode) { case HttpStatusCode.BadRequest: case HttpStatusCode.Unauthorized: case HttpStatusCode.Forbidden: case HttpStatusCode.NotFound: case HttpStatusCode.RequestTimeout: case HttpStatusCode.InternalServerError: case HttpStatusCode.NotImplemented: case HttpStatusCode.ServiceUnavailable: this.ViewPlatform.ShowToast("Error connecting to server. Please check connection."); break; default: break; } } this.ProcessExecuteException(ex, "FetchItemAsync"); throw; } }
public Task <bool> WithRefreshAsync <T>(string key, bool allowStaleData, bool forceRefresh, FetchedDelegate <T> onRefreshed, Action <bool> onRefreshing, Func <Task <T> > createMethod) where T : class { FetchedRequestDelegate <T> wrapper = null; if (onRefreshed != null) { wrapper = (token, freshData, data) => { onRefreshed(freshData, data); }; } return(this.WithRefreshAsync <T>(RequestToken.Empty, key, allowStaleData, forceRefresh, wrapper, onRefreshing, createMethod)); }