Data engine update issue in Javascript
Maik Beckmann
beckmann.maik at googlemail.com
Fri Feb 10 09:57:52 UTC 2012
I figured the problem(s) out. Here it goes..
Here source code from the initial email without the print statements.
var layout = new LinearLayout(plasmoid);
l = new Label();
l.text = "---";
layout.addItem(l);
var smDataEngine = dataEngine("systemmonitor")
smDataEngine.sourceAdded.connect(function(name) {
if (name.toString() == "cpu/system/TotalLoad") {
smDataEngine.connectSource(name, plasmoid, 1000);
});
plasmoid.dataUpdated = function(name, d) {
if ( d["value"] ) {
l.text = d["value"];
}
};
There are three bugs in there, where the first hides the remaining two.
The first bug is that sourceAdded isn't called when the applet is added to a
running plasma-desktop session. If you'd restart plasma-deskop, then it works
as advertised. I assume that the the process loads all applet and then starts
the data engines, which then causes the sourceAdded slot to be called. Adding
a new applet ofc doesn't restart the data engines and there we have it. Note
that for the same reason this bug doesn't show up when you're using
plasmoidviewer (which is why it took me a while to figure it out).
Now lets change the source code to work around this problem. I'll get rid of
sourceAdded altogether, since it's unlikely that the CPU will be plugged out at
runtime.
var layout = new LinearLayout(plasmoid);
l = new Label();
l.text = "---";
layout.addItem(l);
var smDataEngine = dataEngine("systemmonitor")
smDataEngine.connectSource("cpu/system/TotalLoad", plasmoid, 1000);
plasmoid.dataUpdated = function(name, d) {
if ( d["value"] ) {
l.text = d["value"];
}
};
This reliably _never_ works, because the second bug kicks in. When
connetSource is called the updateData slot isn't filled yet. This causes
connetSource to fail. This failure doesn't cause an debug message to be
emitted (which is good IMO), but connectSource returns 'undefined' rather than
'true'. So the two remaining bugs are:
1. 'plasmoid.dataUpdated = ..' isn't located before
'smDataEngine.connectSource(..)' in the source file.
2. The return value of connectSource isn't checked.
Here the final and hopefully correct version
var layout = new LinearLayout(plasmoid);
l = new Label();
l.text = "---";
layout.addItem(l);
plasmoid.dataUpdated = function(name, d) {
if ( d["value"] ) {
l.text = d["value"];
}
};
var smDataEngine = dataEngine("systemmonitor");
if (!smDataEngine.connectSource("cpu/system/TotalLoad", plasmoid, 1000)) {
throw("connecting to data source failed.");
}
Thanks for your attention,
Maik
More information about the Plasma-devel
mailing list