Hot reload fix
This commit is contained in:
1 parent
8b8a7f4f9a
commit
0fa9db4691
6 files changed
+677
-178
No files matched your search
+101
-5
@@ -47,20 +47,116 @@ void CoreLoader::onCoreFileChanged() {
|
||||
return;
|
||||
}
|
||||
|
||||
ECHOION_LOG_INFO("Core file changed, triggering hot reload...");
|
||||
ECHOION_LOG_INFO("Core file changed, scheduling hot reload after current track...");
|
||||
|
||||
// Small delay to ensure file write is complete
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
|
||||
auto result = hotReload(currentLibraryPath_);
|
||||
if (!result) {
|
||||
ECHOION_LOG_ERROR(std::format("Auto hot-reload failed: {}", result.error().message));
|
||||
// Check if playback is active - if so, defer reload
|
||||
if (isPlaybackActive()) {
|
||||
std::string currentTrackId;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(pendingReloadMutex_);
|
||||
hasPendingReload_ = true;
|
||||
pendingReloadPath_ = currentLibraryPath_;
|
||||
|
||||
// Capture current track ID
|
||||
auto state = core_->getPlaybackState();
|
||||
if (state && state->currentTrack.has_value()) {
|
||||
pendingTrackId_ = state->currentTrack->track.id;
|
||||
currentTrackId = pendingTrackId_;
|
||||
}
|
||||
}
|
||||
|
||||
ECHOION_LOG_INFO(std::format("Playback active (track: {}), deferring reload until track finishes", currentTrackId));
|
||||
|
||||
// Start background thread to wait for track end
|
||||
if (deferredReloadThread_.joinable()) {
|
||||
deferredReloadThread_.join();
|
||||
}
|
||||
deferredReloadThread_ = std::thread([this, currentTrackId]() {
|
||||
waitForTrackAndReload(pendingReloadPath_, currentTrackId);
|
||||
});
|
||||
} else {
|
||||
ECHOION_LOG_INFO("Auto hot-reload completed successfully");
|
||||
// No playback, reload immediately
|
||||
auto result = hotReload(currentLibraryPath_);
|
||||
if (!result) {
|
||||
ECHOION_LOG_ERROR(std::format("Auto hot-reload failed: {}", result.error().message));
|
||||
} else {
|
||||
ECHOION_LOG_INFO("Auto hot-reload completed successfully");
|
||||
// Notify Plugin to re-register callbacks
|
||||
if (hotReloadCallback_) {
|
||||
hotReloadCallback_();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CoreLoader::isPlaybackActive() const {
|
||||
if (!core_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto state = core_->getPlaybackState();
|
||||
if (!state) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if currently playing a track
|
||||
return state->isPlaying && state->currentTrack.has_value();
|
||||
}
|
||||
|
||||
bool CoreLoader::isSameTrackPlaying(const std::string& trackId) const {
|
||||
if (!core_ || trackId.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto state = core_->getPlaybackState();
|
||||
if (!state || !state->currentTrack.has_value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return state->currentTrack->track.id == trackId;
|
||||
}
|
||||
|
||||
void CoreLoader::waitForTrackAndReload(const std::filesystem::path& targetPath, const std::string& trackId) {
|
||||
ECHOION_LOG_INFO(std::format("Waiting for track {} to finish before reload...", trackId));
|
||||
|
||||
// Poll every 500ms to check if the specific track finished
|
||||
// (playback may continue with next track, but we only care about THIS track)
|
||||
while (isSameTrackPlaying(trackId)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
}
|
||||
|
||||
ECHOION_LOG_INFO("Track finished, starting hot reload");
|
||||
|
||||
// Clear pending flag
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(pendingReloadMutex_);
|
||||
hasPendingReload_ = false;
|
||||
pendingTrackId_.clear();
|
||||
}
|
||||
|
||||
// Now do the reload
|
||||
auto result = hotReload(targetPath);
|
||||
if (!result) {
|
||||
ECHOION_LOG_ERROR(std::format("Deferred hot-reload failed: {}", result.error().message));
|
||||
} else {
|
||||
ECHOION_LOG_INFO("Deferred hot-reload completed successfully");
|
||||
// Notify Plugin to re-register callbacks
|
||||
if (hotReloadCallback_) {
|
||||
hotReloadCallback_();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CoreLoader::~CoreLoader() {
|
||||
// Stop any deferred reload thread
|
||||
if (deferredReloadThread_.joinable()) {
|
||||
// We can't really stop it gracefully, but we can detach it
|
||||
// to avoid hanging on shutdown
|
||||
deferredReloadThread_.detach();
|
||||
}
|
||||
unload();
|
||||
cleanupTempFiles();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user