open-dis / open-dis-javascript

Javascript implementation of the IEEE-1278.1 Distributed Interactive Simulation (DIS) application protocol v6 and v7
BSD 2-Clause "Simplified" License
11 stars 12 forks source link

Error dis is not defined -- when using in Angular framework #27

Closed beetbit closed 7 months ago

beetbit commented 8 months ago

Hello,

I am currently attempting to implement OpenDIS into a web application, however, the web app is built within and utilizes an Angular framework whereas the available documented projects for the javascript implementation all make use of Node.js. Does anyone have any pointers or direction on how to implement this within Angular?

Thanks!

leif81 commented 8 months ago

I'm not sure myself but maybe someone in the community has some experience and will comment here.

samindaw commented 8 months ago

For my angular app I installed the latest version of the module with no issues.

npm install open-dis --save

However I kept getting the error dis is not defined in the console when I tried to use the module. After quite some tinkering and experimenting I managed to get it to work for my app. However I had to modify the module (dist/dis6.min.js or dist/dis7.min.js) as follows.

Since the variable dis was not properly declared, I inserted the var keyword.

if(typeof dis==="undefined")dis={};

to

if(typeof dis==="undefined")var dis={};

And in my app component pages instead of using require I used import instead. eg:

// @ts-ignore
import {PduFactory} from 'open-dis'

Note: Had to add // @ts-ignore because the compiler seems to not able to locate the module although it's available during runtime.

If this looks like the way to go, I'd appreciate someone from the developer team updating the code and publishing a new version so that I can directly use the one in the npm registry without needing to modify it.

thanks in advance.

leif81 commented 8 months ago

Thank-you @samindaw

The proposed change makes sense to me. Would you be able to submit a pull request for the change?

samindaw commented 8 months ago

Sure @leif81. I'll do the needful changes and send you a PR.

samindaw commented 7 months ago

I created a PR for adding var.

However the PR does not contain a fix for the issue of compiler not recognising the module (the need to add // @ts-config). In order to fix that we are required to introduce a ts file declaring the exported functions and variables with their types. It would not interfere with existing behaviour for nodejs apps. If you want I can take a stab at it but it would take some time to create.

leif81 commented 7 months ago

Thank-you @samindaw

I've merged the var fix.

Yes, go ahead and create a second PR for the typescript header it sounds like a useful improvement to include here.