-
Notifications
You must be signed in to change notification settings - Fork 91
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
Bug when deriving state inside an object #348
Comments
I think the line: layout.settings.scalingRatio = logslider(layout.settings.scalingRatio.value.val) is problematic. Let's break down what this line does:
Thus, the end result is, you're registering |
https://jsfiddle.net/jsg0ukqv/1/ @nbonfils, сould you please give more context, I have not been able to reproduce the error |
Thank you both for your quick replies! @Tao-VanJS Actually the line is: layout.settings.scalingRatio = logslider(settings.scalingRatio.value.val); We could even simplify it as: layout.settings.scalingRatio = settings.scalingRatio.value.val; So @sirenkovladd That example looks pretty much like what I have, tomorrow I'll push it on a repo so I can share the full context of this code. In the meantime here is the full file: As you can see I am currently working around the issue by doing this: // Some code above ...
let layout;
let sigmaInstance;
van.derive(() => {
if (!graph.val) return;
if (layout) layout.kill();
layout = new FA2Layout(graph.val, {
settings: {
gravity: logslider(settings.gravity.value.rawVal),
scalingRatio: logslider(settings.scalingRatio.value.rawVal),
},
});
if (sigmaInstance) sigmaInstance.kill();
sigmaInstance = new Sigma(graph.val, sigmaContainer, {
itemSizesReference: "positions",
zoomToSizeRatioFunction: (x) => x,
});
layout.start();
layoutRunning.val = true;
});
// More code in-between ...
const settings = {
scalingRatio: {
value: van.state(70),
min: 0,
max: 100,
label: "Scaling (overall graph size)",
},
gravity: {
value: van.state(20),
min: 0,
max: 100,
label: "Gravity (graph compactness)",
},
labelSize: {
value: van.state(14),
min: 0,
max: 100,
label: "Label size",
},
labelDensity: {
value: van.state(1),
min: 0,
max: 10,
label: "Label density",
},
labelRenderThreshold: {
value: van.state(6),
min: 0,
max: 10,
label: "Label render threshold",
},
nodeScale: {
value: van.state(100),
min: 1,
max: 200,
label: "Node scale",
},
};
van.derive(() => {
settings.scalingRatio.value.val;
if (layout)
layout.settings.scalingRatio = logslider(settings.scalingRatio.value.val);
});
van.derive(() => {
settings.gravity.value.val;
if (layout) layout.settings.gravity = logslider(settings.gravity.value.val);
});
van.derive(() => {
settings.labelSize.value.val;
sigmaInstance?.setSetting("labelSize", settings.labelSize.value.val);
});
van.derive(() => {
settings.labelDensity.value.val;
sigmaInstance?.setSetting("labelDensity", settings.labelDensity.value.val);
});
van.derive(() => {
settings.labelRenderThreshold.value.val;
sigmaInstance?.setSetting(
"labelRenderedSizeThreshold",
settings.labelRenderThreshold.value.val,
);
});
van.derive(() => {
settings.nodeScale.value.val;
graph.rawVal?.forEachNode((nodeRef, attr) =>
graph.rawVal.mergeNodeAttributes(nodeRef, {
size: attr.origSize * (settings.nodeScale.value.val / 100),
}),
);
});
// Even more code below ... Each of those derive statement don't run without the explicit |
Ah..., I see. For the derivation here: van.derive(() => {
// Without this line: settings.scalingRatio.value.val;
if (layout)
layout.settings.scalingRatio = logslider(settings.scalingRatio.value.val);
});
|
Yes, so |
Let's take a look at the example below: let layout = undefined;
van.derive(() => {
// Without this line: settings.scalingRatio.value.val;
if (layout)
layout.settings.scalingRatio = logslider(settings.scalingRatio.value.val);
}); When If the code is like that: let layout = undefined;
van.derive(() => {
settings.scalingRatio.value.val;
if (layout)
layout.settings.scalingRatio = logslider(settings.scalingRatio.value.val);
});
|
Ahh that makes sense thank you for this explaination! But what about this other case: van.derive(() => {
// settings.labelSize.value.val;
sigmaInstance?.setSetting("labelSize", settings.labelSize.value.val);
}); With Forgive me if I missed this in the docs but does the "registration" occur when the function inside the |
sigmaInstance?.setSetting("labelSize", settings.labelSize.value.val) is just the syntax sugar of if (sigmaInstance)
sigmaInstance.setSetting("labelSize", settings.labelSize.value.val) Every time when the derivation function is called, all the states accessed in the derivation will be registered as the dependencies of derivation. When any of the dependency states changes, the derivation will be triggered, and a new set of dependencies will be registered upon the execution of the derivation. I think the example in this section illustrates the mechanism well. |
Got it! So the derive statement not triggering stems from the fact that that Am I understanding this correctly? So another way to go about this would be to have another state, let's call it van.derive(() => {
if (sigmaInstanceAvailable.val)
sigmaInstance.setSetting("labelSize", settings.labelSize.value.val)
}); |
Yes, that would be a very nice optimized code |
Alternatively, you can make van.derive(() => {
sigmaInstance.val?.setSetting("labelSize", settings.labelSize.value.val)
}) |
Thank you very much for taking the time to explain it to me! And I don't know if it's of interest to you to see where van is used, but here is the project that we are going to release in the coming weeks: We switched from Alpine to Van and I am not looking back, it's honestly a great framework when you don't want to deal with the absurd complexity of everything else out there! |
I encountered a weird but that has me scratching my head...
Let's look at the following piece of code:
If I uncomment the
console.log
statement the derivation runs properly each timesettings.scalingRatio.value
changes, but if I leave it commented like in the screenshot it doesn't run.Any help or explaination as to what is happening would be highly appreciated!
Thanks for making van, it's really a great piece of software, been having a lot of fun making reactive webapps while being close to vanilla js!
The text was updated successfully, but these errors were encountered: