registerCustomActionTags exception regarding function name
Hello,
My friend and I were trying to figure out how to add custom actions (which is also discussed in https://gitter.im/SCION-SCXML/Lobby). First of all, thank you very much for the guidance given in gitter! After following the examples, we have encountered a "SyntaxError: Unexpected token" exception. After some research, I was able to fix it with a hack. I'll provide code samples and the way I temporarily fixed it below. I'm using node 10.24.0, packages installed with npm 7.19.0:
"@scion-scxml/core": "^2.6.24",
"@scion-scxml/scxml": "^4.3.27"
Example code (Index.js):
const scxml = require("@scion-scxml/scxml");
const core = require("@scion-scxml/core");
let doc1 = `
<scxml initial="s0" version="1.0" xmlns="http://www.w3.org/2005/07/scxml" xmlns:test="http://example.com">
<state id="s0">
<onentry>
<log expr="'Entry'" label="info"/>
<test:me />
<send event="e0"/>
</onentry>
<transition target="final" event="e0">
</transition>
</state>
<final id="final">
<onentry>
<log expr="'Reached final'" label="info"/>
</onentry>
</final>
</scxml>
`;
let action = {
"http://example.com": {
me: function (action, builder) {
return `this.log("It works!!")`;
},
},
};
scxml.registerCustomActionTags(action);
scxml.documentStringToModel("", doc1, function (err, model) {
if (err) throw err;
model.prepare(function (err, fnModel) {
if (err) throw err;
//instantiate the interpreter
var sc = new core.Statechart(fnModel);
//start the interpreter
sc.start();
});
});
The exception we were getting is:
I figured, I should somehow fixed the function name to make it valid again. Therefore my fix was modifying scjson-to-module.js line 200
if(action.$type.startsWith("{"))
action.$type = action.$type.replace(/[{\+\-\[\]\.: }/]/g, '_')
With this, I was able to get it to work. There might be a better or more elegant solutions (I have also not polished the regex).
I'm sorry if I have not followed any correct convention during the issue. Thank you!