Skip to content

Commit babd400

Browse files
committed
add harmony support
1 parent cb28df7 commit babd400

23 files changed

+1133
-7
lines changed
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules
2+
/oh_modules
3+
/.preview
4+
/build
5+
/.cxx
6+
/.test
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"apiType": "stageMode",
3+
"targets": [
4+
{
5+
"name": "default"
6+
}
7+
]
8+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
2+
export { harTasks } from '@ohos/hvigor-ohos-plugin';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./ts"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Define project specific obfuscation rules here.
2+
# You can include the obfuscation configuration files in the current module's build-profile.json5.
3+
#
4+
# For more details, see
5+
# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md
6+
7+
# Obfuscation options:
8+
# -disable-obfuscation: disable all obfuscations
9+
# -enable-property-obfuscation: obfuscate the property names
10+
# -enable-toplevel-obfuscation: obfuscate the names in the global scope
11+
# -compact: remove unnecessary blank spaces and all line feeds
12+
# -remove-log: remove all console.* statements
13+
# -print-namecache: print the name cache that contains the mapping from the old names to new names
14+
# -apply-namecache: reuse the given cache file
15+
16+
# Keep options:
17+
# -keep-property-name: specifies property names that you want to keep
18+
# -keep-global-name: specifies names that you want to keep in the global scope
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"license": "",
3+
"devDependencies": {
4+
"rnoh": "file:../rnoh"
5+
},
6+
"author": "",
7+
"name": "rnoh-async-storage",
8+
"description": "",
9+
"main": "",
10+
"version": "2.5.1",
11+
"dependencies": {}
12+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2023 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include "RNOH/Package.h"
26+
#include "RNAsyncStorageTurboModule.h"
27+
28+
using namespace rnoh;
29+
using namespace facebook;
30+
class NativeRNAsyncStorageFactoryDelegate : public TurboModuleFactoryDelegate {
31+
public:
32+
SharedTurboModule createTurboModule(Context ctx,const std::string &name) const override {
33+
if (name == "RNCAsyncStorage") {
34+
return std::make_shared<RNAsyncStorageTurboModule>(ctx, name);
35+
}
36+
return nullptr;
37+
};
38+
};
39+
40+
namespace rnoh {
41+
class AsyncStoragePackage : public Package {
42+
public:
43+
AsyncStoragePackage(Package::Context ctx) : Package(ctx) {}
44+
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
45+
return std::make_unique<NativeRNAsyncStorageFactoryDelegate>();
46+
}
47+
};
48+
} // namespace rnoh
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# the minimum version of CMake.
2+
cmake_minimum_required(VERSION 3.13)
3+
set(CMAKE_VERBOSE_MAKEFILE on)
4+
5+
file(GLOB rnoh_async_storage_SRC CONFIGURE_DEPENDS *.cpp)
6+
add_library(rnoh_async_storage SHARED ${rnoh_async_storage_SRC})
7+
target_include_directories(rnoh_async_storage PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
8+
target_link_libraries(rnoh_async_storage PUBLIC rnoh)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2023 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include "RNAsyncStorageTurboModule.h"
26+
#include "RNOH/ArkTSTurboModule.h"
27+
28+
using namespace rnoh;
29+
using namespace facebook;
30+
31+
static jsi::Value __hostFunction_RNAsyncStorageTurboModule_multiGet(jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
32+
return static_cast<ArkTSTurboModule &>(turboModule).call(rt, "multiGet", args, count);
33+
}
34+
35+
static jsi::Value __hostFunction_RNAsyncStorageTurboModule_multiSet(jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
36+
return static_cast<ArkTSTurboModule &>(turboModule).call(rt, "multiSet", args, count);
37+
}
38+
39+
static jsi::Value __hostFunction_RNAsyncStorageTurboModule_multiRemove(jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
40+
return static_cast<ArkTSTurboModule &>(turboModule).call(rt, "multiRemove", args, count);
41+
}
42+
43+
static jsi::Value __hostFunction_RNAsyncStorageTurboModule_multiMerge(jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
44+
return static_cast<ArkTSTurboModule &>(turboModule).call(rt, "multiMerge", args, count);
45+
}
46+
47+
static jsi::Value __hostFunction_RNAsyncStorageTurboModule_getAllKeys(jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
48+
return static_cast<ArkTSTurboModule &>(turboModule).call(rt, "getAllKeys", args, count);
49+
}
50+
51+
static jsi::Value __hostFunction_RNAsyncStorageTurboModule_clear(jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
52+
return static_cast<ArkTSTurboModule &>(turboModule).call(rt, "clear", args, count);
53+
}
54+
55+
RNAsyncStorageTurboModule::RNAsyncStorageTurboModule(const ArkTSTurboModule::Context ctx, const std::string name)
56+
: ArkTSTurboModule(ctx, name) {
57+
methodMap_["multiGet"] = MethodMetadata{2, __hostFunction_RNAsyncStorageTurboModule_multiGet};
58+
methodMap_["multiSet"] = MethodMetadata{2, __hostFunction_RNAsyncStorageTurboModule_multiSet};
59+
methodMap_["multiRemove"] = MethodMetadata{2, __hostFunction_RNAsyncStorageTurboModule_multiRemove};
60+
methodMap_["multiMerge"] = MethodMetadata{2, __hostFunction_RNAsyncStorageTurboModule_multiMerge};
61+
methodMap_["getAllKeys"] = MethodMetadata{2, __hostFunction_RNAsyncStorageTurboModule_getAllKeys};
62+
methodMap_["clear"] = MethodMetadata{1, __hostFunction_RNAsyncStorageTurboModule_clear};
63+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2023 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#pragma once
26+
27+
#include "RNOH/ArkTSTurboModule.h"
28+
29+
namespace rnoh {
30+
31+
class JSI_EXPORT RNAsyncStorageTurboModule : public ArkTSTurboModule {
32+
public:
33+
RNAsyncStorageTurboModule(const ArkTSTurboModule::Context ctx, const std::string name);
34+
};
35+
36+
} // namespace rnoh
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2023 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
import relationalStore from '@ohos.data.relationalStore';
26+
27+
import CommonConstants from './CommonConstants';
28+
import Logger from './Logger';
29+
import ReactDatabaseSupplier from './ReactDatabaseSupplier'
30+
31+
export default class AsyncLocalStorageUtil {
32+
33+
static buildKeySelection(keys: string[], keyStart: number, keyCount: number) {
34+
let selectedKeys: string[] = [];
35+
for (let keyIndex = 0; keyIndex < keyCount; keyIndex++) {
36+
Logger.debug(CommonConstants.TAG, `buildKeySelection(), selectedKey: ${keys[keyStart + keyIndex]}`)
37+
selectedKeys.push(keys[keyStart + keyIndex]);
38+
}
39+
return selectedKeys;
40+
}
41+
42+
static async getItemImpl(db: relationalStore.RdbStore, key: string): Promise<string | null> {
43+
let columns: string[] = new Array(ReactDatabaseSupplier.VALUE_COLUMN);
44+
let predicates = new relationalStore.RdbPredicates(ReactDatabaseSupplier.TABLE_CATALYST);
45+
predicates.equalTo(ReactDatabaseSupplier.KEY_COLUMN, key);
46+
let cursor = await db.query(predicates, columns);
47+
Logger.debug(CommonConstants.TAG, `getItemImpl(), ResultSet isAtFirstRow: ${cursor.isAtFirstRow}, row count: ${cursor.rowCount}`);
48+
try {
49+
if(!cursor.goToFirstRow()){
50+
return null;
51+
} else{
52+
return cursor.getString(cursor.getColumnIndex('VALUE'));
53+
}
54+
} catch (e) {
55+
Logger.error(CommonConstants.TAG, `getItemImpl() error: ${e.message}`)
56+
return null;
57+
} finally {
58+
cursor.close();
59+
}
60+
}
61+
62+
/**
63+
* Sets the value for the key given, returns true if successful, false otherwise.
64+
*/
65+
static async setItemImpl(db: relationalStore.RdbStore, key: string, value:string): Promise<boolean> {
66+
Logger.debug(CommonConstants.TAG, `setItemImpl(), key: ${key}, newValue: ${value}`)
67+
let obj: relationalStore.ValuesBucket = {};
68+
obj.key = key;
69+
obj.value = value;
70+
try {
71+
db.beginTransaction();
72+
let inserted = await db.insert(ReactDatabaseSupplier.TABLE_CATALYST, obj, relationalStore.ConflictResolution.ON_CONFLICT_REPLACE);
73+
db.commit();
74+
return (-1 !== inserted);
75+
} catch (e) {
76+
Logger.error(CommonConstants.TAG, `setItemImpl() error: ${e.message}`)
77+
}
78+
return false;
79+
}
80+
81+
/**
82+
* Does the actual merge of the (key, value) pair with the value stored in the database.
83+
* NB: This assumes that a database lock is already in effect!
84+
* @return the errorCode of the operation
85+
*/
86+
static async mergeImpl(db: relationalStore.RdbStore, key: string, value:string): Promise<boolean> {
87+
let oldValue = await AsyncLocalStorageUtil.getItemImpl(db, key);
88+
let newValue: string;
89+
90+
try {
91+
if (oldValue == null) {
92+
newValue = value;
93+
} else {
94+
let oldJSON: Record<string, string | Object> = JSON.parse(oldValue);
95+
let newJSON: Record<string, string | Object> = JSON.parse(value);
96+
97+
await AsyncLocalStorageUtil.deepMergeInto(oldJSON, newJSON);
98+
newValue = JSON.stringify(oldJSON);
99+
}
100+
let isSuccess = await AsyncLocalStorageUtil.setItemImpl(db, key, newValue);
101+
return isSuccess;
102+
} catch (e) {
103+
Logger.error(CommonConstants.TAG, `mergeImpl() error: ${e.message}`)
104+
}
105+
return false;
106+
}
107+
108+
/**
109+
* Merges two {@link JSONObject}s. The newJSON object will be merged with the oldJSON object by
110+
* either overriding its values, or merging them (if the values of the same key in both objects
111+
* are of type {@link JSONObject}). oldJSON will contain the result of this merge.
112+
*/
113+
private static async deepMergeInto(oldJSON: Record<string, string | Object>, newJSON: Record<string, string | Object>) {
114+
let keys: string[] = Object.keys(newJSON);
115+
for(let idx = 0; idx < keys.length; idx++) {
116+
let key = keys[idx];
117+
let newValue = JSON.stringify(newJSON[key]);
118+
let oldValue = JSON.stringify(oldJSON[key]);
119+
120+
let newJSONObject = AsyncLocalStorageUtil.optJSONObject(newValue);
121+
let oldJSONObject = AsyncLocalStorageUtil.optJSONObject(oldValue);
122+
Logger.debug(CommonConstants.TAG, `deepMergeInto(), newJSONObject: ${JSON.stringify(newJSONObject)}, oldJSONObject: ${JSON.stringify(oldJSONObject)}`)
123+
124+
if (newJSONObject !== null && oldJSONObject !== null) {
125+
await AsyncLocalStorageUtil.deepMergeInto(oldJSONObject, newJSONObject);
126+
oldJSON[key] = oldJSONObject;
127+
} else {
128+
oldJSON[key] = newJSON[key];
129+
}
130+
}
131+
}
132+
133+
private static optJSONObject(value: string) {
134+
try {
135+
let JSONObject: Record<string, string | Object> = JSON.parse(value);
136+
if (typeof JSONObject == "object" && JSONObject && !Array.isArray(JSONObject)) {
137+
return JSONObject;
138+
}
139+
} catch (e) {
140+
Logger.error(CommonConstants.TAG, `optJSONObject() error: ${e.message}`)
141+
}
142+
return null;
143+
}
144+
}

0 commit comments

Comments
 (0)