Skip to content

Commit

Permalink
use GA_destroy_json to prevent memory leaks, remove useless TwoFactor…
Browse files Browse the repository at this point in the history
…Call class
  • Loading branch information
mattiaferrari02 committed Feb 19, 2024
1 parent 889a9d5 commit 1bdb099
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 111 deletions.
1 change: 0 additions & 1 deletion android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ add_library(
../cpp/GdkHostObject.cpp
../cpp/json.cpp
../cpp/utils.cpp
../cpp/TwoFactorCall.cpp
../cpp/ThreadPool.cpp
)

Expand Down
84 changes: 54 additions & 30 deletions cpp/GdkHostObject.cpp

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion cpp/GdkHostObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "utils.hpp"
#include "json.hpp"
#include <ReactCommon/CallInvoker.h>
#include "TwoFactorCall.hpp"
#include "ThreadPool.hpp"

using namespace facebook;
Expand Down
31 changes: 0 additions & 31 deletions cpp/TwoFactorCall.cpp

This file was deleted.

31 changes: 0 additions & 31 deletions cpp/TwoFactorCall.hpp

This file was deleted.

16 changes: 12 additions & 4 deletions cpp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,24 @@ namespace utils {
return res;
}


json resolve(TwoFactorCall call) {
json resolve(GA_auth_handler* call) {
while (true) {
json obj = call.getStatus();
GA_json *resJson;
GA_auth_handler_get_status(call, &resJson);
char *stringJson;
GA_convert_json_to_string(resJson, &stringJson);
GA_destroy_json(resJson);
std::string s(stringJson);
json obj = json::parse(s);
std::string status = obj["status"];

if (status == "call") {
call.call();
GA_auth_handler_call(call);
} else if (status == "done") {
GA_destroy_auth_handler(call);
return obj;
} else {
GA_destroy_auth_handler(call);
return obj;
}
}
Expand All @@ -129,6 +136,7 @@ namespace utils {

char *stringJson;
GA_convert_json_to_string(details, &stringJson);
GA_destroy_json(details);
std::string s(stringJson);
json res = json::parse(s);

Expand Down
9 changes: 4 additions & 5 deletions cpp/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <gdk.h>
#include <jsi/jsi.h>
#include "TwoFactorCall.hpp"
#include "GdkHostObject.hpp"
#include "json.hpp"

Expand All @@ -29,8 +28,8 @@ namespace utils {
// debug utility
int consoleLog(jsi::Runtime &rt, std::string msg);

// resolve a TwoFactorCall
json resolve(TwoFactorCall call);
// resolve a auth handler call
json resolve(GA_auth_handler* call);


// handle notifications
Expand All @@ -42,7 +41,7 @@ namespace utils {
// definition of js JSON.stringify and JSON.parse
std::string stringify(jsi::Runtime &runtime, const jsi::Value &value);
jsi::Value parse(jsi::Runtime &runtime, std::string src);


// promise definition
struct Promise {
Expand All @@ -56,7 +55,7 @@ namespace utils {
jsi::Function reject_;
};
using Promised = std::function<void(jsi::Runtime &rt, std::shared_ptr<Promise>)>;

// creates and returns a promise given the funnction to be wrapped in promise
jsi::Value makePromise(jsi::Runtime &rt, const Promised func);

Expand Down
5 changes: 2 additions & 3 deletions docs/about_gdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ This is the struct that will hold the session informations. It is created using
It is stored as a member of the `GdkHostObject` class and it is used to call all GDK functions that require a session.
## `GA_auth_handler`
This is the struct that holds the reference to an authenticated function/network call. Use it wrapped with `TwoFactorCall` object and complete the call using `utils::resolve(twoFactorCall)`.
This is the struct that holds the reference to an authenticated function/network call. Use it wrapped `utils::resolve(call)`.
```cpp
GA_auth_handler *call; // create the auth handler
utils::wrapCall(GA_register_user(session, hw_device_json, details_json, &call)); // make the authenticated net call
TwoFactorCall twoFactorCall(call); // pass the call to the TwoFactorCall class
json res = utils::resolve(twoFactorCall); // resolve the call using the utility function
json res = utils::resolve(call); // resolve the call using the utility function
```

Expand Down
5 changes: 1 addition & 4 deletions docs/about_jsi.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ react-native-gdk
│ └── GdkPackage.java
└── cpp
├── GdkHostObject.cpp
├── TwoFactorCall.cpp
├── ThreadPool.cpp
├── json.cpp
└── utils.cpp
Expand All @@ -49,7 +48,6 @@ react-native-gdk
`cpp`
- Contains all the non platform specific code, the core of the application that will be compiled for both platforms
- `GdkHostObject.cpp` is the host object that will be created in the native runtime and exposed to JavaScript, its methods will be callable from JS
- `TwoFactorCall.cpp` resolves a GDK http request that requires authentication
- `utils.cpp` contains some utility functions to semplify the workflows
- `ThreadPool.cpp` implementation of a thread pool used to execute async tasks, source here https://github.com/OP-Engineering/op-sqlite/blob/main/cpp/ThreadPool.cpp
- `json.cpp` contains an implementation of JSON objects in cpp, source here https://github.com/nlohmann/json
Expand Down Expand Up @@ -136,8 +134,7 @@ return jsi::Function::createFromHostFunction(runtime, // create function using r
try {
GA_auth_handler *call;
utils::wrapCall(GA_get_receive_address(session, details, &call));
TwoFactorCall twoFactorCall(call);
json res = utils::resolve(twoFactorCall);
json res = utils::resolve(call);
// the long running task finishes and we need to resolve or reject the promise
// lock the weak pointer to the invoker to gain control of the runtime
std::shared_ptr<react::CallInvoker> c = invoker.lock();
Expand Down
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const App: React.FunctionComponent = () => {
}} />
<Button title="register" onPress={async() => {
try {
await gdk.register({}, { mnemonic, password: "" })
console.log(await gdk.register({}, { mnemonic, password: "" }))
} catch (error) {
console.log("ERROR", error)
}
Expand Down

0 comments on commit 1bdb099

Please sign in to comment.