I deployed a "function pet_change(pet, gain)" to swap between idle sets according if gain or lose a Luopan (pet).
I have something similar in my aftercast too, but that works only after an action. If I were to use only that, my idle set wouldn't change if I'm not doing anything and my pet gets oneshot, which is exactely the main reason why I'm using this function.
Still, I don't want to find myself in a situation where I'm casting a long spell (say, Impact), my luopan dies, and the pet_change function brings me back into my idle gear. To avoid such situations I wanted to make so the content of the pet_change function are executed only if I'm not currently busy with casting, i.e. midaction() returns "false".