public async void getAllKeys(ICallback callback)
        {
            var keys = new JArray();

            await _mutex.WaitAsync().ConfigureAwait(false);

            try
            {
                var storageFolder = await GetAsyncStorageFolder(false).ConfigureAwait(false);

                if (storageFolder != null)
                {
                    var items = await storageFolder.GetFilesAsync().ConfigureAwait(false);

                    foreach (var item in items)
                    {
                        var itemName = item.Name;
                        if (itemName.EndsWith(AsyncStorageHelpers.FileExtension))
                        {
                            keys.Add(AsyncStorageHelpers.GetKeyName(itemName));
                        }
                    }
                }
            }
            finally
            {
                _mutex.Release();
            }

            callback.Invoke(null, keys);
        }
        public async void clear(ICallback callback)
        {
            var error = default(JObject);

            await _mutex.WaitAsync().ConfigureAwait(false);

            try
            {
                var storageFolder = await GetAsyncStorageFolder(false).ConfigureAwait(false);

                if (storageFolder != null)
                {
                    await storageFolder.DeleteAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                error = AsyncStorageHelpers.GetError(ex);
            }
            finally
            {
                _mutex.Release();
            }

            if (error != null)
            {
                callback.Invoke(error);
            }
            else
            {
                callback.Invoke();
            }
        }
        public async void multiSet(string[][] keyValueArray, ICallback callback)
        {
            if (keyValueArray == null || keyValueArray.Length == 0)
            {
                callback.Invoke(AsyncStorageHelpers.GetInvalidKeyError(null));
                return;
            }

            var error = default(JObject);

            await _mutex.WaitAsync().ConfigureAwait(false);

            try
            {
                foreach (var pair in keyValueArray)
                {
                    if (pair.Length != 2)
                    {
                        error = AsyncStorageHelpers.GetInvalidValueError(null);
                        break;
                    }

                    if (pair[0] == null)
                    {
                        error = AsyncStorageHelpers.GetInvalidKeyError(null);
                        break;
                    }

                    if (pair[1] == null)
                    {
                        error = AsyncStorageHelpers.GetInvalidValueError(pair[0]);
                        break;
                    }

                    error = await SetAsync(pair[0], pair[1]).ConfigureAwait(false);

                    if (error != null)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                error = AsyncStorageHelpers.GetError(ex);
            }
            finally
            {
                _mutex.Release();
            }

            if (error != null)
            {
                callback.Invoke(error);
            }
            else
            {
                callback.Invoke();
            }
        }
        private async Task <JObject> SetAsync(string key, string value)
        {
            var storageFolder = await GetAsyncStorageFolder(true).ConfigureAwait(false);

            var file = await storageFolder.CreateFileAsync(AsyncStorageHelpers.GetFileName(key), CreationCollisionOption.ReplaceExisting).ConfigureAwait(false);

            await FileExtensions.WriteAllTextAsync(file, value).ConfigureAwait(false);

            return(default(JObject));
        }
Beispiel #5
0
        public async void multiRemove(string[] keys, ICallback callback)
        {
            if (keys == null || keys.Length == 0)
            {
                callback.Invoke(AsyncStorageHelpers.GetInvalidKeyError(null));
                return;
            }

            var error = default(JObject);

            await _mutex.WaitAsync().ConfigureAwait(false);

            try
            {
                foreach (var key in keys)
                {
                    if (key == null)
                    {
                        error = AsyncStorageHelpers.GetInvalidKeyError(null);
                        break;
                    }

                    error = await RemoveAsync(key).ConfigureAwait(false);

                    if (error != null)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                error = AsyncStorageHelpers.GetError(ex);
            }
            finally
            {
                _mutex.Release();
            }

            if (error != null)
            {
                RnLog.Warn(ReactConstants.RNW, $"Error in AsyncStorageModule.multiRemove: {error}");
                callback.Invoke(error);
            }
            else
            {
                callback.Invoke();
            }
        }
Beispiel #6
0
        public async void multiGet(string[] keys, ICallback callback)
        {
            if (keys == null)
            {
                callback.Invoke(AsyncStorageHelpers.GetInvalidKeyError(null), null);
                return;
            }

            var error = default(JObject);
            var data  = new JArray();

            await _mutex.WaitAsync().ConfigureAwait(false);

            try
            {
                foreach (var key in keys)
                {
                    if (key == null)
                    {
                        error = AsyncStorageHelpers.GetInvalidKeyError(null);
                        break;
                    }

                    var value = await GetAsync(key).ConfigureAwait(false);

                    data.Add(new JArray(key, value));
                }
            }
            catch (Exception ex)
            {
                error = AsyncStorageHelpers.GetError(ex);
            }
            finally
            {
                _mutex.Release();
            }

            if (error != null)
            {
                RnLog.Warn(ReactConstants.RNW, $"Error in AsyncStorageModule.multiGet: {error}");
                callback.Invoke(error);
            }
            else
            {
                callback.Invoke(null, data);
            }
        }
Beispiel #7
0
        private async Task <JObject> RemoveAsync(string key)
        {
            var storageFolder = await GetAsyncStorageFolder(false).ConfigureAwait(false);

            if (storageFolder != null)
            {
                var fileName    = AsyncStorageHelpers.GetFileName(key);
                var storageItem = await storageFolder.TryGetItemAsync(fileName).AsTask().ConfigureAwait(false);

                if (storageItem != null)
                {
                    await storageItem.DeleteAsync().AsTask().ConfigureAwait(false);
                }
            }

            return(null);
        }
        public async void getAllKeys(ICallback callback)
        {
            var error = default(JObject);
            var keys  = new JArray();
            var error = default(JObject);

            await _mutex.WaitAsync().ConfigureAwait(false);

            try
            {
                var storageFolder = await GetAsyncStorageFolder(false).ConfigureAwait(false);

                if (storageFolder != null)
                {
                    var items = await storageFolder.GetFilesAsync().ConfigureAwait(false);

                    foreach (var item in items)
                    {
                        var itemName = item.Name;
                        if (itemName.EndsWith(AsyncStorageHelpers.FileExtension))
                        {
                            keys.Add(AsyncStorageHelpers.GetKeyName(itemName));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                error = AsyncStorageHelpers.GetError(ex);
            }
            finally
            {
                _mutex.Release();
            }

            if (error != null)
            {
                callback.Invoke(error);
            }
            else
            {
                callback.Invoke(null, keys);
            }
        }
        public async void multiRemove(string[] keys, ICallback callback)
        {
            if (keys == null || keys.Length == 0)
            {
                callback.Invoke(AsyncStorageHelpers.GetInvalidKeyError(null));
                return;
            }

            var error = default(JObject);

            await _mutex.WaitAsync().ConfigureAwait(false);

            try
            {
                foreach (var key in keys)
                {
                    if (key == null)
                    {
                        error = AsyncStorageHelpers.GetInvalidKeyError(null);
                        break;
                    }

                    error = await RemoveAsync(key).ConfigureAwait(false);

                    if (error != null)
                    {
                        break;
                    }
                }
            }
            finally
            {
                _mutex.Release();
            }

            if (error != null)
            {
                callback.Invoke(error);
            }
            else
            {
                callback.Invoke();
            }
        }
        private async Task <JObject> MergeAsync(string key, string value)
        {
            var oldValue = await GetAsync(key).ConfigureAwait(false);

            var newValue = default(string);

            if (oldValue == null)
            {
                newValue = value;
            }
            else
            {
                var oldJson = new JObject(new JProperty(key, oldValue));
                var newJson = new JObject(new JProperty(key, value));
                AsyncStorageHelpers.DeepMergeInto(oldJson, newJson);
                newValue = ((JToken)oldJson[key]).ToString();
            }
            return(await SetAsync(key, newValue).ConfigureAwait(false));
        }
Beispiel #11
0
        private async Task <string> GetAsync(string key)
        {
            var storageFolder = await GetAsyncStorageFolder(false).ConfigureAwait(false);

            if (storageFolder != null)
            {
                var fileName    = AsyncStorageHelpers.GetFileName(key);
                var storageItem = await storageFolder.TryGetItemAsync(fileName).AsTask().ConfigureAwait(false);

                if (storageItem != null)
                {
                    var file = await storageFolder.GetFileAsync(fileName).AsTask().ConfigureAwait(false);

                    return(await FileIO.ReadTextAsync(file).AsTask().ConfigureAwait(false));
                }
            }

            return(null);
        }
        public async void multiGet(string[] keys, ICallback callback)
        {
            if (keys == null)
            {
                callback.Invoke(AsyncStorageHelpers.GetInvalidKeyError(null), null);
                return;
            }

            var error = default(JObject);
            var data  = new JArray();

            await _mutex.WaitAsync().ConfigureAwait(false);

            try
            {
                foreach (var key in keys)
                {
                    if (key == null)
                    {
                        error = AsyncStorageHelpers.GetInvalidKeyError(null);
                        break;
                    }

                    var value = await GetAsync(key).ConfigureAwait(false);

                    data.Add(new JArray(key, value));
                }
            }
            finally
            {
                _mutex.Release();
            }

            if (error != null)
            {
                callback.Invoke(error);
            }
            else
            {
                callback.Invoke(null, data);
            }
        }
Beispiel #13
0
        private async Task <JObject> SetAsync(string key, string value)
        {
            try
            {
                var storageFolder = await GetAsyncStorageFolder(true).ConfigureAwait(false);

                var file = await storageFolder.CreateFileAsync(AsyncStorageHelpers.GetFileName(key), CreationCollisionOption.OpenIfExists).ConfigureAwait(false);

                await FileExtensions.WriteAllTextAsync(file, value).ConfigureAwait(false);

                return(default(JObject));
            }
            catch (Exception ex)
            {
                return(new JObject {
                    { "message", ex.Message },
                    { "fileName", ex.ToString() }
                });
            }
        }
        private async Task <JObject> MergeAsync(string key, string value)
        {
            var oldValue = await GetAsync(key).ConfigureAwait(false);

            var newValue = default(string);

            if (oldValue == null)
            {
                newValue = value;
            }
            else
            {
                var oldJson = JObject.Parse(oldValue);
                var newJson = JObject.Parse(value);
                AsyncStorageHelpers.DeepMergeInto(oldJson, newJson);
                newValue = oldJson.ToString(Formatting.None);
            }

            return(await SetAsync(key, newValue).ConfigureAwait(false));
        }
        private async Task <JObject> RemoveAsync(string key)
        {
            var storageFolder = await GetAsyncStorageFolder(false).ConfigureAwait(false);

            if (storageFolder == null)
            {
                return(null);
            }

            var fileName    = AsyncStorageHelpers.GetFileName(key);
            var existsCheck = await storageFolder.CheckExistsAsync(fileName).ConfigureAwait(false);

            if (existsCheck == ExistenceCheckResult.FileExists)
            {
                var storageItem = await storageFolder.GetFileAsync(fileName).ConfigureAwait(false);

                await storageItem.DeleteAsync().ConfigureAwait(false);
            }

            return(null);
        }
        private async Task <string> GetAsync(string key)
        {
            var storageFolder = await GetAsyncStorageFolder(false).ConfigureAwait(false);

            if (storageFolder == null)
            {
                return(null);
            }

            var fileName    = AsyncStorageHelpers.GetFileName(key);
            var existsCheck = await storageFolder.CheckExistsAsync(fileName).ConfigureAwait(false);

            if (existsCheck == ExistenceCheckResult.FileExists)
            {
                var storageItem = await storageFolder.GetFileAsync(fileName).ConfigureAwait(false);

                return(await FileExtensions.ReadAllTextAsync(storageItem).ConfigureAwait(false));
            }

            return(null);
        }
Beispiel #17
0
        public async void clear(ICallback callback)
        {
            var error = default(JObject);

            await _mutex.WaitAsync().ConfigureAwait(false);

            try
            {
                var storageFolder = await GetAsyncStorageFolder(false).ConfigureAwait(false);

                if (storageFolder != null)
                {
                    await storageFolder.DeleteAsync().AsTask().ConfigureAwait(false);

                    _cachedFolder = null;
                }
            }
            catch (Exception ex)
            {
                error = AsyncStorageHelpers.GetError(ex);
            }
            finally
            {
                _mutex.Release();
            }

            if (error != null)
            {
                RnLog.Warn(ReactConstants.RNW, $"Error in AsyncStorageModule.clear: {error}");
                callback.Invoke(error);
            }
            else
            {
                callback.Invoke();
            }
        }