As in comments under the question, I think it's a duplicate of:
Automatically run a script when an object is transformed or a condition is met
Which points to a Space3DView draw callback solution:
Use a driver to programmatically set the positions of many vertices
Since we had a conversation in the comments about it, I decided to write an answer here. The principle is the same as under the link (the 2nd part, "If you don't want it for an animation") - on every draw, try to figure out if you don't have to do anything as soon as possible, and in such case just quit the function. That's the purpose of 'last_shading' variable - if you keep track of the last value, you now know if the value changed - if it didn't, you don't have to do anything and you can just quit. The sooner you realize the function has no work to do and can quit, the less CPU cycles you will waste. But, as I show in the benchmark you don't have to worry about it too much...
import bpy
from bpy import context as C, data as D
from bpy.types import SpaceView3D
visibility_data = {
'Cube': {'SOLID', 'MATERIAL', 'RENDERED'},
'Sphere': {'MATERIAL'},
'Suzanne': {'SOLID', 'MATERIAL'}
}
last_shading = None
def test():
global last_shading
if C.area.type != 'VIEW_3D':
return
shading_type = C.space_data.shading.type
if shading_type == last_shading:
return
print(last_shading) # The lesson from edits... Use a print to inspect if the function isn't executed too often
for ob_name, shadings in visibility_data.items():
ob = D.objects[ob_name]
ob.hide_viewport = shading_type not in shadings
last_shading = shading_type
try:
SpaceView3D.draw_handler_remove(SpaceView3D.my_handler, 'WINDOW')
except (AttributeError, ValueError):
pass
SpaceView3D.my_handler = SpaceView3D.draw_handler_add(test, (), 'WINDOW', 'PRE_VIEW')
If you run the script with a project that has a cube, sphere and 🐵, all with default names, you should be able to see that they hide automatically based on the visibility_data
dictionary:

Not sure if changing the rendering mode updates depsgraph, but for almost anything, consider replacing the view 3D callback with the application handler, as used here by Martynas Žiemys:
Operator that runs when a value input is changed