diff --git a/android/src/main/cpp/AudioContext.cpp b/android/src/main/cpp/AudioContext.cpp index 65060fac..b6c007ab 100644 --- a/android/src/main/cpp/AudioContext.cpp +++ b/android/src/main/cpp/AudioContext.cpp @@ -6,17 +6,12 @@ namespace audiocontext using namespace facebook::jni; - AudioContext::AudioContext(jni::alias_ref &jThis, - jlong jsContext) : javaObject_(make_global(jThis)), jsContext_(jsContext) {} + AudioContext::AudioContext(jni::alias_ref &jThis) : javaObject_(make_global(jThis)){} - void AudioContext::install() + void AudioContext::install(jlong jsContext) { auto audioContextWrapper = std::make_shared(std::shared_ptr(this)); - auto runtime = reinterpret_cast(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 AudioContext::createOscillator() diff --git a/android/src/main/cpp/AudioContext.h b/android/src/main/cpp/AudioContext.h index b0929421..00982fd8 100644 --- a/android/src/main/cpp/AudioContext.h +++ b/android/src/main/cpp/AudioContext.h @@ -21,9 +21,9 @@ namespace audiocontext public: static auto constexpr kJavaDescriptor = "Lcom/audiocontext/context/AudioContext;"; - static jni::local_ref initHybrid(jni::alias_ref jThis, jlong jsContext) + static jni::local_ref initHybrid(jni::alias_ref jThis) { - return makeCxxInstance(jThis, jsContext); + return makeCxxInstance(jThis); } static void registerNatives() @@ -40,15 +40,14 @@ namespace audiocontext std::shared_ptr createOscillator(); std::shared_ptr getDestination(); - void install(); + void install(jlong jsContext); private: friend HybridBase; global_ref javaObject_; - jlong jsContext_; - explicit AudioContext(jni::alias_ref &jThis, jlong jsContext); + explicit AudioContext(jni::alias_ref &jThis); }; } // namespace audiocontext diff --git a/android/src/main/java/com/audiocontext/context/AudioContext.kt b/android/src/main/java/com/audiocontext/context/AudioContext.kt index c729570e..0ec325ba 100644 --- a/android/src/main/java/com/audiocontext/context/AudioContext.kt +++ b/android/src/main/java/com/audiocontext/context/AudioContext.kt @@ -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 @@ -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) diff --git a/android/src/main/java/com/audiocontext/nativemodules/AudioContextModule.kt b/android/src/main/java/com/audiocontext/nativemodules/AudioContextModule.kt index ad550c04..47f44586 100644 --- a/android/src/main/java/com/audiocontext/nativemodules/AudioContextModule.kt +++ b/android/src/main/java/com/audiocontext/nativemodules/AudioContextModule.kt @@ -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 { diff --git a/cpp/AudioContext/AudioContextHostObject.h b/cpp/AudioContext/AudioContextHostObject.h index 5205b53e..11b38d2a 100644 --- a/cpp/AudioContext/AudioContextHostObject.h +++ b/cpp/AudioContext/AudioContextHostObject.h @@ -17,8 +17,11 @@ namespace audiocontext public: explicit AudioContextHostObject(std::shared_ptr wrapper) : wrapper_(wrapper) {} - static std::shared_ptr createFromWrapper(std::shared_ptr wrapper) { - return std::make_shared(wrapper); + static void createAndInstallFromWrapper(std::shared_ptr wrapper, jlong jsContext) { + auto runtime = reinterpret_cast(jsContext); + auto hostObject = std::make_shared(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; diff --git a/src/index.ts b/src/index.ts index fba02ce9..50d9e4c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,7 @@ declare global { export class AudioContext implements BaseAudioContext { constructor() { - AudioContextModule.initAudioContext(); + AudioContextModule.installAudioContext(); } createOscillator(): Oscillator {