I'm splitting one file code into multiple ones and I run into a few circular dependencies.
Some of them, I am able to avoid them by refactoring a bit, others it seem more complicated or it just wouldn't make sense to me as the refactoring will probably mean having to put a lot of code together in the same file instead of splitting it by features/functionalities.
I've seen other components using events to communicate between modules and I thought I could use this same technique to avoid the circular dependency, but.... I'm not sure if this is just an ugly hack rather than the proper way of doing it.
So, for example, having the following:
import { sayHi } from 'introductions.js';
export function demo(){
// stuff here
sayHi(stuff); // circular dependency here
}
I was thinking I could replace it for the following:
export function demo(){
// do stuff here
emit('sayHi', stuff); // event here, avoiding the import
}
//introductions.js
import { whatever } from './whatever.js';
on('sayHi', function(stuff){
sayHi(stuff);
});
function sayHi(stuff){
console.log("Hi " + stuff);
}
...
Would this be a proper solution to it? Or just an ugly hack to "remove" the circular dependency?