Skip to content

Commit

Permalink
Something like this
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed Jan 1, 2024
1 parent f09f136 commit 7718e14
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
72 changes: 46 additions & 26 deletions native/PluginProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,31 +265,14 @@ void EffectsPluginProcessor::initJavaScriptEngine()
});

jsContext.registerFunction("__log__", [this](choc::javascript::ArgumentList args) {
const auto* kDispatchScript = R"script(
(function() {
console.log(...JSON.parse(%));
return true;
})();
)script";

// Forward logs to the editor if it's available; then logs show up in one place.
//
// If not available, we fall back to std out.
if (auto* editor = static_cast<WebViewEditor*>(getActiveEditor())) {
auto v = choc::value::createEmptyArray();

for (size_t i = 0; i < args.numArgs; ++i) {
v.addArrayElement(*args[i]);
}
auto v = choc::value::createEmptyArray();

auto expr = juce::String(kDispatchScript).replace("%", elem::js::serialize(choc::json::toString(v))).toStdString();
editor->getWebViewPtr()->evaluateJavascript(expr);
} else {
for (size_t i = 0; i < args.numArgs; ++i) {
DBG(choc::json::toString(*args[i]));
}
for (size_t i = 0; i < args.numArgs; ++i) {
v.addArrayElement(*args[i]);
}

logEmbeddedMessage(choc::json::toString(v));

return choc::value::Value();
});

Expand Down Expand Up @@ -320,15 +303,25 @@ void EffectsPluginProcessor::initJavaScriptEngine()
auto dspEntryFile = getAssetsDirectory().getChildFile("dsp.main.js");
auto dspEntryFileContents = dspEntryFile.loadFileAsString().toStdString();
#endif
jsContext.evaluate(dspEntryFileContents);
try {
jsContext.evaluate(dspEntryFileContents);
} catch (choc::javascript::Error const& e) {
// TODO: Something like this... need to fix the serialization
logEmbeddedMessage(std::string(e.what()));
}

// Re-hydrate from current state
const auto* kHydrateScript = R"script(
(function() {
if (typeof globalThis.__receiveHydrationData__ !== 'function')
return false;
globalThis.__receiveHydrationData__(%);
try {
globalThis.__receiveHydrationData__(%);
} catch (e) {
console.error(e.name, e.message);
}
return true;
})();
)script";
Expand All @@ -344,7 +337,12 @@ void EffectsPluginProcessor::dispatchStateChange()
if (typeof globalThis.__receiveStateChange__ !== 'function')
return false;
globalThis.__receiveStateChange__(%);
try {
globalThis.__receiveStateChange__(%);
} catch (e) {
console.error(e.name, e.message);
}
return true;
})();
)script";
Expand Down Expand Up @@ -378,7 +376,12 @@ void EffectsPluginProcessor::dispatchError(std::string const& name, std::string
let e = new Error(%);
e.name = @;
globalThis.__receiveError__(e);
try {
globalThis.__receiveError__(e);
} catch (e) {
console.error(e.name, e.message);
}
return true;
})();
)script";
Expand All @@ -397,6 +400,23 @@ void EffectsPluginProcessor::dispatchError(std::string const& name, std::string
jsContext.evaluate(expr);
}

//==============================================================================
void EffectsPluginProcessor::logEmbeddedMessage(std::string const& serializedMessage)
{
const auto* kDispatchScript = R"script(
(function() {
console.log(...JSON.parse(%));
return true;
})();
)script";

// Forward logs to the editor if it's available; then all logs show up in one place.
if (auto* editor = static_cast<WebViewEditor*>(getActiveEditor())) {
auto expr = juce::String(kDispatchScript).replace("%", elem::js::serialize(serializedMessage)).toStdString();
editor->getWebViewPtr()->evaluateJavascript(expr);
}
}

//==============================================================================
void EffectsPluginProcessor::getStateInformation (juce::MemoryBlock& destData)
{
Expand Down
3 changes: 3 additions & 0 deletions native/PluginProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class EffectsPluginProcessor
void dispatchError(std::string const& name, std::string const& message);

private:
//==============================================================================
void logEmbeddedMessage(std::string const& serializedMessage);

//==============================================================================
std::atomic<bool> shouldInitialize { false };
double lastKnownSampleRate = 0;
Expand Down

0 comments on commit 7718e14

Please sign in to comment.