DRAWFOREGROUND and undo

Hi! Could you please help to figure out how to put everything happening within DRAWFOREGROUND as a single undo step?
I tried this:

def drawforeground( self, layer, event ):
layer.parent.beginUndo()
do stuff that should be part of one undo
layer.parent.endUndo()

But seems like I still have to undo as many times as the callback has been called.
Thank you!

You should not do changes to the layer and effect undo in any way during drawing operations.

Hm, then maybe there’s a god practice how to undo in one step everything happened during the script?

Normally, all actions that are done in one run of a script are grouped automatically.
If that is not enough, there is: Glyphs.app Python Scripting API Documentation — Glyphs.app Python Scripting API 3.0 documentation