Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch console log messages to an editor, if one is active. #6

Merged
merged 5 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion dsp/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function shouldRender(prevState, nextState) {
// on the result of our `shouldRender` check.
globalThis.__receiveStateChange__ = (serializedState) => {
const state = JSON.parse(serializedState);

if (shouldRender(prevState, state)) {
let stats = core.render(...srvb({
key: 'srvb',
Expand Down
15 changes: 13 additions & 2 deletions native/PluginProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,20 @@ void EffectsPluginProcessor::initJavaScriptEngine()
return choc::value::Value();
});

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

for (size_t i = 0; i < args.numArgs; ++i) {
DBG(choc::json::toString(*args[i], true));
// Dispatch to the UI if it's available
if (auto* editor = static_cast<WebViewEditor*>(getActiveEditor())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general idea here is perfect, but I think we should change the implementation slightly– right now, this will evaluate a new expression in the webView for each argument passed to the __log__ function. I think instead of running the for loop over args.numArgs we should serialize args as its own array, and then change the console.log(%) expression to console.log(...JSON.parse(%)) so that we only evaluate the expression once, but we still pass the var args to console.log correctly

Copy link
Contributor Author

@bthj bthj Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! I think you know better how to implement this change :)

auto expr = juce::String(kDispatchScript).replace("%", elem::js::serialize(elem::js::serialize(choc::json::toString(*args[i], false)))).toStdString();
editor->getWebViewPtr()->evaluateJavascript(expr);
}
}

return choc::value::Value();
Expand Down
14 changes: 8 additions & 6 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ function requestParamValueUpdate(paramId, value) {
}
}

import.meta.hot.on('reload-dsp', () => {
console.log('Sending reload dsp message');
if (process.env.NODE_ENV !== 'production') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

import.meta.hot.on('reload-dsp', () => {
console.log('Sending reload dsp message');

if (typeof globalThis.__postNativeMessage__ === 'function') {
globalThis.__postNativeMessage__('reload');
}
});
if (typeof globalThis.__postNativeMessage__ === 'function') {
globalThis.__postNativeMessage__('reload');
}
});
}

globalThis.__receiveStateChange__ = function(state) {
store.setState(JSON.parse(state));
Expand Down