Skip to content

Commit

Permalink
refactor: moved AudioContext HostObject injection into AudioContextHo…
Browse files Browse the repository at this point in the history
…stObject
  • Loading branch information
Maciej Makowski committed Jul 20, 2024
1 parent 573e2b8 commit 6ca8ab0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
11 changes: 3 additions & 8 deletions android/src/main/cpp/AudioContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ namespace audiocontext

using namespace facebook::jni;

AudioContext::AudioContext(jni::alias_ref<AudioContext::jhybridobject> &jThis,
jlong jsContext) : javaObject_(make_global(jThis)), jsContext_(jsContext) {}
AudioContext::AudioContext(jni::alias_ref<AudioContext::jhybridobject> &jThis) : javaObject_(make_global(jThis)){}

void AudioContext::install()
void AudioContext::install(jlong jsContext)
{
auto audioContextWrapper = std::make_shared<AudioContextWrapper>(std::shared_ptr<AudioContext>(this));
auto runtime = reinterpret_cast<jsi::Runtime *>(jsContext_);
auto hostObject = AudioContextHostObject::createFromWrapper(audioContextWrapper);

auto object = jsi::Object::createFromHostObject(*runtime, hostObject);
runtime->global().setProperty(*runtime, "__AudioContextProxy", std::move(object));
AudioContextHostObject::createAndInstallFromWrapper(audioContextWrapper, jsContext);
}

std::shared_ptr<OscillatorNode> AudioContext::createOscillator()
Expand Down
9 changes: 4 additions & 5 deletions android/src/main/cpp/AudioContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace audiocontext
public:
static auto constexpr kJavaDescriptor = "Lcom/audiocontext/context/AudioContext;";

static jni::local_ref<AudioContext::jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis, jlong jsContext)
static jni::local_ref<AudioContext::jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis)
{
return makeCxxInstance(jThis, jsContext);
return makeCxxInstance(jThis);
}

static void registerNatives()
Expand All @@ -40,15 +40,14 @@ namespace audiocontext
std::shared_ptr<OscillatorNode> createOscillator();
std::shared_ptr<AudioDestinationNode> getDestination();

void install();
void install(jlong jsContext);

private:
friend HybridBase;

global_ref<AudioContext::javaobject> javaObject_;
jlong jsContext_;

explicit AudioContext(jni::alias_ref<AudioContext::jhybridobject> &jThis, jlong jsContext);
explicit AudioContext(jni::alias_ref<AudioContext::jhybridobject> &jThis);
};

} // namespace audiocontext
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.audiocontext.nodes.oscillator.OscillatorNode
import com.facebook.jni.HybridData
import com.facebook.react.bridge.ReactApplicationContext

class AudioContext(reactContext: ReactApplicationContext) : BaseAudioContext {
class AudioContext() : BaseAudioContext {
override val sampleRate: Int = 44100
override val destination: AudioDestinationNode = AudioDestinationNode(this)
get() = field
Expand All @@ -19,12 +19,11 @@ class AudioContext(reactContext: ReactApplicationContext) : BaseAudioContext {
}

init {
mHybridData = initHybrid(reactContext.javaScriptContextHolder!!.get())
mHybridData = initHybrid()
}

external fun initHybrid(l: Long): HybridData?

external fun install()
external fun initHybrid(): HybridData?
external fun install(jsContext: Long)

override fun createOscillator(): OscillatorNode {
return OscillatorNode(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class AudioContextModule(private val reactContext: ReactApplicationContext) : Re
}

@ReactMethod(isBlockingSynchronousMethod = true)
fun initAudioContext() {
val audioContext = AudioContext(reactContext)
audioContext.install()
fun installAudioContext() {
val audioContext = AudioContext()
val jsContext = reactContext.javaScriptContextHolder!!.get()
audioContext.install(jsContext)
}

companion object {
Expand Down
7 changes: 5 additions & 2 deletions cpp/AudioContext/AudioContextHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ namespace audiocontext
public:
explicit AudioContextHostObject(std::shared_ptr<AudioContextWrapper> wrapper) : wrapper_(wrapper) {}

static std::shared_ptr<AudioContextHostObject> createFromWrapper(std::shared_ptr<AudioContextWrapper> wrapper) {
return std::make_shared<AudioContextHostObject>(wrapper);
static void createAndInstallFromWrapper(std::shared_ptr<AudioContextWrapper> wrapper, jlong jsContext) {
auto runtime = reinterpret_cast<jsi::Runtime *>(jsContext);
auto hostObject = std::make_shared<AudioContextHostObject>(wrapper);
auto object = jsi::Object::createFromHostObject(*runtime, hostObject);
runtime->global().setProperty(*runtime, "__AudioContextProxy", std::move(object));
}

jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare global {

export class AudioContext implements BaseAudioContext {
constructor() {
AudioContextModule.initAudioContext();
AudioContextModule.installAudioContext();
}

createOscillator(): Oscillator {
Expand Down

0 comments on commit 6ca8ab0

Please sign in to comment.