JS script to get next state
Refactor distribution patch to use Javascript for processing