script: Improve dirty propagation and fix script-layout synchronization.

This fixes race conditions whereby layout and script could be running
simultaneously.

r? @jdm 
cc @cgaebel 
I think this should be a match so that if other cases are added, there will be an appropriate compiler error to figure out what to do with the new case.