Skip to content

Commit bc585f3

Browse files
author
Kai Guo
committed
CR feedback
1 parent 060cf02 commit bc585f3

File tree

6 files changed

+796
-295
lines changed

6 files changed

+796
-295
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
"flow-bin": "0.92.0",
5858
"jest": "24.8.0",
5959
"metro-react-native-babel-preset": "0.54.1",
60-
"react": "16.9.0",
61-
"react-dom": "16.9.0",
62-
"react-native": "0.61.5",
60+
"react": "16.6.3",
61+
"react-dom": "16.6.3",
62+
"react-native": "0.59.10",
6363
"react-native-web": "~0.12.0",
6464
"react-native-macos": "0.60.0-microsoft.50",
6565
"react-test-renderer": "16.8.3",

windows/ReactNativeAsyncStorage/DBStorage.cpp

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace winrt {
55
using namespace Microsoft::ReactNative;
6+
using namespace Windows::ApplicationModel::Core;
67
using namespace Windows::Foundation;
78
using namespace Windows::Storage;
89
} // namespace winrt
@@ -194,13 +195,45 @@ namespace {
194195
return CheckSQLiteResult(db, callback, sqlite3_bind_text(stmt.get(), index, str.c_str(), -1, SQLITE_TRANSIENT));
195196
}
196197

198+
struct slim_shared_lock_guard
199+
{
200+
explicit slim_shared_lock_guard(winrt::slim_mutex& m) noexcept :
201+
m_mutex(m)
202+
{
203+
m_mutex.lock_shared();
204+
}
205+
206+
~slim_shared_lock_guard() noexcept
207+
{
208+
m_mutex.unlock_shared();
209+
}
210+
211+
private:
212+
winrt::slim_mutex& m_mutex;
213+
};
214+
197215
} // namespace
198216

217+
const winrt::hstring DBStorage::s_dbPathProperty = L"React-Native-Community-Async-Storage-Database-Path";
218+
199219
DBStorage::DBStorage() {
200-
auto const localAppDataParh = winrt::ApplicationData::Current().LocalFolder().Path();
201-
std::wstring wPath(localAppDataParh.data());
202-
wPath += L"\\AsyncStorage.db";
203-
auto const path = ConvertWstrToStr(wPath);
220+
std::string path;
221+
if (auto pathInspectable = winrt::CoreApplication::Properties().TryLookup(s_dbPathProperty)) {
222+
auto pathHstring = winrt::unbox_value<winrt::hstring>(pathInspectable);
223+
path = ConvertWstrToStr(std::wstring(pathHstring.c_str()));
224+
}
225+
else {
226+
try {
227+
auto const localAppDataPath = winrt::ApplicationData::Current().LocalFolder().Path();
228+
std::wstring wPath(localAppDataPath.data());
229+
wPath += L"\\AsyncStorage.db";
230+
path = ConvertWstrToStr(wPath);
231+
}
232+
catch (winrt::hresult_error const&) {
233+
throw std::runtime_error("Please specify 'React-Native-Community-Async-Storage-Database-Path' in CoreApplication::Properties");
234+
}
235+
}
236+
204237
if (sqlite3_open_v2(
205238
path.c_str(),
206239
&m_db,
@@ -230,14 +263,14 @@ DBStorage::DBStorage() {
230263
}
231264

232265
DBStorage::~DBStorage() {
233-
decltype(m_action) action;
266+
decltype(m_tasks) tasks;
234267
{
235268
// If there is an in-progress async task, cancel it and wait on the
236269
// condition_variable for the async task to acknowledge cancellation by
237270
// nulling out m_action. Once m_action is null, it is safe to proceed
238271
// wth closing the DB connection
239-
winrt::slim_lock_guard guard{ m_lock };
240-
m_tasks.clear();
272+
slim_shared_lock_guard guard{ m_lock };
273+
swap(tasks, m_tasks);
241274
if (m_action) {
242275
m_action.Cancel();
243276
m_cv.wait(m_lock, [this]() { return m_action == nullptr; });

windows/ReactNativeAsyncStorage/DBStorage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class DBStorage {
4646
winrt::slim_condition_variable m_cv;
4747
winrt::Windows::Foundation::IAsyncAction m_action{ nullptr };
4848
std::vector<DBTask> m_tasks;
49+
const static winrt::hstring s_dbPathProperty;
4950

5051
std::string ConvertWstrToStr(const std::wstring& wstr);
5152
};

windows/ReactNativeAsyncStorage/RNCAsyncStorage.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace ReactNativeAsyncStorage
1717
REACT_METHOD(multiGet);
1818
void multiGet(std::vector<JSValue>& keys, std::function<void(JSValueArray const& errors, JSValueArray const& results)>&& callback) noexcept {
1919
dbStorage.AddTask(DBStorage::DBTask::Type::multiGet, std::move(keys),
20-
[callback](std::vector<JSValue> const& callbackParams) {
20+
[callback{ std::move(callback) }](std::vector<JSValue> const& callbackParams) {
2121
if (callbackParams.size() > 0) {
2222
auto& errors = callbackParams[0].AsArray();
2323
if (callbackParams.size() > 1) {
@@ -33,7 +33,7 @@ namespace ReactNativeAsyncStorage
3333
REACT_METHOD(multiSet);
3434
void multiSet(std::vector<JSValue>& pairs, std::function<void(JSValueArray const&)>&& callback) noexcept {
3535
dbStorage.AddTask(DBStorage::DBTask::Type::multiSet, std::move(pairs),
36-
[callback](std::vector<JSValue> const& callbackParams) {
36+
[callback{ std::move(callback) }](std::vector<JSValue> const& callbackParams) {
3737
if (callbackParams.size() > 0) {
3838
auto& errors = callbackParams[0].AsArray();
3939
callback(errors);
@@ -50,7 +50,7 @@ namespace ReactNativeAsyncStorage
5050
newValues.push_back(pair.AsArray()[1].AsString());
5151
}
5252

53-
multiGet(std::move(keys), [newValues, callback, this](JSValueArray const& errors, JSValueArray const& results) {
53+
multiGet(std::move(keys), [newValues{ std::move(newValues) }, callback{ std::move(callback) }, this](JSValueArray const& errors, JSValueArray const& results) {
5454
if (errors.size() > 0) {
5555
callback(errors);
5656
return;
@@ -88,7 +88,7 @@ namespace ReactNativeAsyncStorage
8888
}
8989
}
9090

91-
multiSet(std::move(mergedResults), [callback](JSValueArray const& errors) {
91+
multiSet(std::move(mergedResults), [callback{ std::move(callback) }](JSValueArray const& errors) {
9292
callback(errors);
9393
});
9494
});
@@ -97,7 +97,7 @@ namespace ReactNativeAsyncStorage
9797
REACT_METHOD(multiRemove);
9898
void multiRemove(std::vector<JSValue>& keys, std::function<void(JSValueArray const&)>&& callback) noexcept {
9999
dbStorage.AddTask(DBStorage::DBTask::Type::multiRemove, std::move(keys),
100-
[callback](std::vector<JSValue> const& callbackParams) {
100+
[callback{ std::move(callback) }](std::vector<JSValue> const& callbackParams) {
101101
if (callbackParams.size() > 0) {
102102
auto& errors = callbackParams[0].AsArray();
103103
callback(errors);
@@ -108,7 +108,7 @@ namespace ReactNativeAsyncStorage
108108
REACT_METHOD(getAllKeys);
109109
void getAllKeys(std::function<void(JSValueArray const& errors, JSValueArray const& keys)>&& callback) noexcept {
110110
dbStorage.AddTask(DBStorage::DBTask::Type::getAllKeys,
111-
[callback](std::vector<JSValue> const& callbackParams) {
111+
[callback{ std::move(callback) }](std::vector<JSValue> const& callbackParams) {
112112
if (callbackParams.size() > 0) {
113113
auto& errors = callbackParams[0].AsArray();
114114
if (callbackParams.size() > 1) {
@@ -124,7 +124,7 @@ namespace ReactNativeAsyncStorage
124124
REACT_METHOD(clear);
125125
void clear(std::function<void(JSValueArray const&)>&& callback) noexcept {
126126
dbStorage.AddTask(DBStorage::DBTask::Type::clear,
127-
[callback](std::vector<JSValue> const& callbackParams) {
127+
[callback{ std::move(callback) }](std::vector<JSValue> const& callbackParams) {
128128
if (callbackParams.size() > 0) {
129129
auto& errors = callbackParams[0].AsArray();
130130
callback(errors);

windows/ReactNativeAsyncStorage/pch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <unknwn.h>
66
#include <functional>
7+
#include <winrt/Windows.ApplicationModel.Core.h>
78
#include <winrt/Windows.Data.Json.h>
89
#include <winrt/Windows.Foundation.h>
910
#include <winrt/Windows.Foundation.Collections.h>

0 commit comments

Comments
 (0)