refactor(core): allow restart logic to be triggered using an event#3380
refactor(core): allow restart logic to be triggered using an event#3380
Conversation
MarshallOfSound
left a comment
There was a problem hiding this comment.
This still feels side-effecty/hacky. Like, we're still emitting a random string on a random emitter. This should just be a method exposed via forge core, or plugin base that can be called normally
What about: // at the top-level of the module
const restartAppEventEmitter = new EventEmitter()
// ...
if (interactive) {
restartAppEventEmitter.on('restart', async () => {
if (lastSpawned) {
console.info(chalk.cyan('\nRestarting App\n'));
lastSpawned.restarted = true;
lastSpawned.kill('SIGTERM');
lastSpawned.emit('restarted', await forgeSpawnWrapper());
}
});
process.stdin.on('data', async (data) => {
if (data.toString().trim() === 'rs') {
restartApp();
}
});
process.stdin.resume();
}
// ...
export function restartApp() {
restartAppEventEmitter.emit('restart');
}This would work without consumers directly emitting anything and still allows us to keep the logic simple. |
72410fd to
c670573
Compare
|
Here's the alternative I mentioned in my previous comment: 3a3cf97. I believe using some form of event-based approach is the best option here — the restart logic depends on variables that are inside the scope of the enclosing |
6acb75a to
3a3cf97
Compare
3a3cf97 to
ef1e465
Compare
|
Implemented in #3468 |
Summarize your changes:
This allows the restart event to be triggered from anywhere in Forge and should make it possible for @caoxiemeihao to address the
process.stdin.emitissue mentioned in #3203 (comment):At first, I was thinking of creating a dedicated
EventEmitterinstance for this, butprocessis an event emitter and is available everywhere, so that makes things simpler if a bit hacky.