I am writing an application which has physical simulations on it. Each simulation differs in the physics and what gets drawn to a canvas, but not in generalized things such as:
- Dealing with parameters in the address bar which set the simulation up
- How to generate a URL for the simulation to send to others
- How to send data from the simulation tot he data service
- How to create a Reactive Form components to control the simulation.
etc... i.e. there are many parts which are done exactly the same in every simulation, but I current have that same code in each simulation. This adds up to 300-400 repeated lines of code in every simulation, obviously bulking my app up really well.
I want to take all of the general functions which deal with these non-simulation-specific issues and put them in another file, which is then loaded alongside each simulation. Changes in the common file affect all of the simulations which use it.
I have made a demo stackblitz of the problem. This is not the actual app, that's big, but a demo of what my code currently looks like and why I want to change it. The foods have specific things for them, but also general functions which are the same in each component.
For the record I have tried:
- exporting a common.ts file, importing as a service in my .ts file for each simulation.
- exporting only the functions (export function generalFunction()) and reimporting.
- creating a common .ts file which is extended by the simulation .ts file (export class SimulationComponent extends CommonComponent).
I have not been able to make any of them work, as the variables and function names are not happy working together as they would be if they were all under the same class. I recognize that my not making them work is reflective of my current skill level, not that they shouldn't work.
I did not try making the functions static as I read on another stackexchange answer that can cause problems down the line. I want the best solution really.
