-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: decode audio data source with promise (#220)
* feat: decode audio data source with promise * fix: linter
- Loading branch information
Showing
17 changed files
with
184 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,3 +83,4 @@ react-native-audio-api*.tgz | |
# Android | ||
.kotlin | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/react-native-audio-api/common/cpp/utils/JsiPromise.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "JsiPromise.h" | ||
|
||
#include <jsi/jsi.h> | ||
#include <ReactCommon/CallInvoker.h> | ||
#include <functional> | ||
|
||
namespace JsiPromise { | ||
|
||
using namespace facebook; | ||
|
||
jsi::Value PromiseVendor::createPromise(std::function<void(std::shared_ptr<Promise>)> func) { | ||
if (_runtime == nullptr) { | ||
throw new std::runtime_error("Runtime was null!"); | ||
} | ||
auto& runtime = *_runtime; | ||
auto callInvoker = _callInvoker; | ||
|
||
// get Promise constructor | ||
auto promiseCtor = runtime.global().getPropertyAsFunction(runtime, "Promise"); | ||
|
||
// create a "run" function (first Promise arg) | ||
auto runPromise = jsi::Function::createFromHostFunction(runtime, | ||
jsi::PropNameID::forUtf8(runtime, "runPromise"), | ||
2, | ||
[callInvoker, func](jsi::Runtime& runtime, | ||
const jsi::Value& thisValue, | ||
const jsi::Value* arguments, | ||
size_t count) -> jsi::Value { | ||
auto resolveLocal = arguments[0].asObject(runtime).asFunction(runtime); | ||
auto resolve = std::make_shared<jsi::Function>(std::move(resolveLocal)); | ||
auto rejectLocal = arguments[1].asObject(runtime).asFunction(runtime); | ||
auto reject = std::make_shared<jsi::Function>(std::move(rejectLocal)); | ||
|
||
auto resolveWrapper = [resolve, &runtime, callInvoker](jsi::Value value) -> void { | ||
auto valueShared = std::make_shared<jsi::Value>(std::move(value)); | ||
callInvoker->invokeAsync([resolve, &runtime, valueShared]() -> void { | ||
resolve->call(runtime, *valueShared); | ||
}); | ||
}; | ||
|
||
auto rejectWrapper = [reject, &runtime, callInvoker](const std::string& errorMessage) -> void { | ||
auto error = jsi::JSError(runtime, errorMessage); | ||
auto errorShared = std::make_shared<jsi::JSError>(error); | ||
callInvoker->invokeAsync([reject, &runtime, errorShared]() -> void { | ||
reject->call(runtime, errorShared->value()); | ||
}); | ||
}; | ||
|
||
auto promise = std::make_shared<Promise>(resolveWrapper, rejectWrapper); | ||
func(promise); | ||
|
||
return jsi::Value::undefined(); | ||
}); | ||
|
||
// return new Promise((resolve, reject) => ...) | ||
return promiseCtor.callAsConstructor(runtime, runPromise); | ||
} | ||
|
||
} // namespace JsiPromise |
42 changes: 42 additions & 0 deletions
42
packages/react-native-audio-api/common/cpp/utils/JsiPromise.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <ReactCommon/CallInvoker.h> | ||
#include <jsi/jsi.h> | ||
#include <utility> | ||
#include <memory> | ||
#include <string> | ||
|
||
namespace JsiPromise { | ||
|
||
using namespace facebook; | ||
|
||
class Promise { | ||
public: | ||
Promise(std::function<void(jsi::Value)> resolve, std::function<void(const std::string&)> reject): _resolve(std::move(resolve)), _reject(std::move(reject)) {} | ||
|
||
bool isResolved; | ||
void resolve(jsi::Value&& value) { | ||
_resolve(std::forward<jsi::Value>(value)); | ||
} | ||
void reject(const std::string& errorMessage) { | ||
_reject(errorMessage); | ||
} | ||
|
||
private: | ||
std::function<void(jsi::Value)> _resolve; | ||
std::function<void(const std::string&)> _reject; | ||
}; | ||
|
||
class PromiseVendor { | ||
public: | ||
PromiseVendor(jsi::Runtime* runtime, std::shared_ptr<react::CallInvoker> callInvoker): _runtime(runtime), _callInvoker(callInvoker) {} | ||
|
||
public: | ||
jsi::Value createPromise(std::function<void(std::shared_ptr<Promise>)> func); | ||
|
||
private: | ||
jsi::Runtime* _runtime; | ||
std::shared_ptr<react::CallInvoker> _callInvoker; | ||
}; | ||
|
||
} // namespace JsiPromise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.