title: State management in overlapping requests in a UI Framework in PHP
body: <p>I am developing a UI framework in PHP. The idea is very similar to .NET platform. Different php class are developed to render DOJO controls. In short the framework generates Javascript code to render DOJO UI controls on client side. The process is as follows:</p>
  
  <p>First Request:</p>
  
  <ul>
  <li>URL GET request come to server.</li>
  <li>Server executes the relevant scripts which load the FORM.</li>
  <li>Call render function of each child control (this generates the
  javascript code)</li>
  <li>serialize the whole FORM object and save it to a SESSION file.</li>
  <li>Output the JS Code to client.</li>
  </ul>
  
  <p>Subsequent Requests:</p>
  
  <ul>
  <li>FORM postback request comes to server.</li>
  <li>Server loads previous state from SESSION file (using unserialize).</li>
  <li>Update the state object with user submitted values.</li>
  <li>Call event handlers.</li>
  <li>Save the current state in SESSION file.</li>
  <li>Render response to client.</li>
  </ul>
  
  <p>The above process works perfect for a single request at a time from same session. <strong>I need to handle multiple requests from the same session at a time.</strong></p>
  
  <blockquote>
    <p><strong>Issue:</strong></p>
    
    <p>Let say there are two requests from the same session R1 and R2
    respectively. R1 takes 10 secs to complete while R2 takes 3 secs. While
    R1 was at 5 secs R2 comes and update the state file when R1 is at 8
    Sec. After that R1 updates the state file again after 2 secs.</p>
    
    <p><strong>The problem is that the changes made by request R2 to state file is lost.</strong></p>
  </blockquote>
  
  <p>How the above scenario could be handled in PHP? Is there any design pattern I can follow for state management in UI framework?</p>