How to run modal() code when navigating the viewport?

Hi, trying to make a tool to align the light to the camera. However the code inside modal() doesn’t run while i’m in the middle of a navigation.

Is there any fix to this? Thanks

class LUCERO_OT_CAMERA_ALIGN(bpy.types.Operator):
    bl_idname = "lucero.camera_align"
    bl_label = "Camera Align"
    bl_options = {"REGISTER", "UNDO"}
    bl_description = """Aligns the active light to camera"""

    def invoke(self, context, _):
        context.window_manager.modal_handler_add(self)
        return {"RUNNING_MODAL"}

    def modal(self, context, event):
        print("modal")
        if event.type == "ESC":
            return {"FINISHED"}
        return {"PASS_THROUGH"}

Seems like I can use a Timer, however I need to create a class flag to properly unregister it, as is_registered() doesn’t seem to properly detect the operator method.

And if I create a function outside the operator, I would need to use a lambda or partial to pass the object argument, which also doesn’t properly get detected by is_registered(), so i cannot call `unregister().

Any ideas for this?

    def snap_light(self):
        self.light.matrix_world = get_view_matrix_inverted()
        if __class__.CANCEL_FLAG:
            return None
        return 0.01

    def invoke(self, context, _):
        __class__.CANCEL_FLAG = False
        self.light = LUCERO_UL_ENTRIES.get_active_item().obj

        self.draw_handle = add_2d_draw_handler(self.draw)
        bpy.app.timers.register(self.snap_light)
        context.window_manager.modal_handler_add(self)
        return {"RUNNING_MODAL"}

    def cancel(self, _):
        __class__.CANCEL_FLAG = Tru

Why do you need to call timers.unregister? When your timer function return None it will get unregistered automatically.

I’m returning None because timers.unregister was not working properly, ideally I would like to use timers.unregister, that way I don’t have to use a flag variable on the class