retejs / module-plugin

4 stars 10 forks source link

How to load modules dynamically? #16

Open eyasman opened 3 years ago

eyasman commented 3 years ago

editor.use(ModulePlugin.default, { engine, modules });

I'm looking for a way to tell the engine that modules have been updated.

eyasman commented 3 years ago

After updating the modules:

    modules = JSON.parse(reader.result); //getting modules from JSON
    re_alight(); //redrawing the received modules in html modules menu

When trying to open the index editor view I'm getting 'IO not found for node XXX', only the Module Nodes appear without any ports, all other Nodes are displayed properly.

When I open the Modules in editor view, they have properly named input and output Ports.

Same JSON file for Modules displays fine if provided to the ModulePlugin here: editor.use(ModulePlugin.default, { engine, modules });

Photon-Engineer commented 3 years ago

I just ran into this exact issue as well and found a solution. I'll try my best to explain what's going on, but be aware I'm 100% self taught so I may not know the right words to describe everything.

Here's what I think is happening. When you assign the modules object to the editor in the line: editor.use(ModulePlugin.default, { engine, modules }); You are creating a reference to that object in your systems memory, that you and the rete program can use to make changes to modules as the software is used. The problem is, the line: modules = JSON.parse(reader.result); //getting modules from JSON This line is not updating the original object that you and rete originally were using, it instead creates a new object in the systems memory and assigns it to that variable. Now, you have access to the new object, but you've lost your reference to the original object still in use by the rete code.

The solution is to modify your object without completely re-creating it. See the answer by georg in the following stack exchange question https://stackoverflow.com/questions/26957719/replace-object-value-without-replacing-reference. Copy that "modify" function into your script, then change the above line in your code to instead be: modify(modules,JSON.parse(reader.result));

This is what worked for me. Hopefully that makes some sense.

Good luck!