Part of #4196. (cherry picked from commit 23cf6a85bd)
23cf6a85bd
also added a demo showing how this works