Document Save not always fired correctly

Hey Adobe community!

I recently wrote myself a Photoshop UXP plugin, which basically saves the document/canvas to an incrementally named file every X seconds.
The document save method is called correctly by my code from what i can tell, but it occurs more and more often that it simply doesn’t save anything. Because of that, i’m left with gaps in the incremental file naming and it generally is causing missing files due to that. I unfortunately couldn’t come up with any method to reproduce this bug, but it always leaves out exactly one save before working again. I’m pretty sure this has something to do with the way i handled (or didn’t really handle) the async calls, but i’m not sure about that.

If required, here’s the entire sourcecode of the plugin:

Below’s the part that’s causing issues.
Thanks in advance for every answer!

async function SaveSnapshot() {
	TimeLeft = NextSaveTime - GetTime();

	UpdateTimerText();

	if (!isEnabled || !isDirty || TimeLeft > 0) {return;}
	
	// Prevent new save from being started to avoid missing single files when the saving process takes longer than the timer itself
	clearInterval(IntervalTimer);
	
	CurrentIndex = CurrentIndex + 1;
	const file = await userFolder.createFile(`${DocumentName}_${CurrentIndex}.${FileFormat_Selected.toLowerCase()}`);
	console.log("Writing . . .", `${DocumentName}_${CurrentIndex}.${FileFormat_Selected.toLowerCase()}`, parseInt(SaveQuality));
	
	const Snapshot = await app.activeDocument.save(file, { embedColorProfile: true, quality: parseInt(SaveQuality)});
	
	isDirty = false;
	UpdateIntervalTimer();
}

function UpdateIntervalTimer() {
	if (!isEnabled) {return;}
	
	if (IntervalTimer) {
		clearInterval(IntervalTimer);
		IntervalTimer = null;
	}

	console.log("Scheduled next update for:", (SaveInterval) + GetTime())
	console.log("Save Interval:", SaveInterval);
	NextSaveTime = SaveInterval + GetTime();

	// Start Timer to call snapshot func
	IntervalTimer = setInterval(saveSnapshot, 500);
}

Have you identified any patterns to the break?

One thought:

I don’t know if save would work if Ps is in a modal state – such as modifying an image’s crop. I also don’t know if this would raise an exception or not, but it’s worth looking at the developer console logs to see if any errors are getting raised – that could help pinpoint the problem.

That apparently was the right guess - it simply didn’t do anything when any form of modal is open.
Thank you so much!

For anyone curious, this is how i fixed it. This essentially gives you a variable to determine the current modal state of photoshop.

In terms of the save method, i noticed that it only works with api version 1. Was pulling my hair out when i upgraded to 2 while debugging this and it suddenly didn’t work anymore

var isModalOpen = false;

function UpdateModalState(state) {
	if (state == "enter") {
		isModalOpen = true;
	} else {
		isModalOpen = false;
	}
}

listener = (e, d) => {
		if (e == "modalStateChanged") {
			UpdateModalState(d.state._value);
		}
	}
	require('photoshop').action.addNotificationListener([
		{ event: "modalStateChanged" },
	], listener);