How to use Object.deliverChangeRecords method in javascript

298 views Asked by At

I understand how to use Object.observe(), and Object.getNotifier(obj).notify or Object.getNotifier(obj).performChange, but how do I use Object.deliverChangeRecords()

1

There are 1 answers

2
Jeremy Danyow On BEST ANSWER

The point of Object.deliverChangeRecords is to get synchronous delivery to the function that is listening to the mutations.

http://www.danyow.net/object-deliverchangerecords/

Here's a running example that demonstrates the sequence of events with and without deliverChangeRecords:

var output = document.getElementById('output');

function runTest(useDeliver) {
  var obj = {};

  output.textContent = '';
 
  function handleChange(records) {
    output.textContent += 'CHANGE DELIVERED\n';
  }
  
  Object.observe(obj, handleChange);
  
  output.textContent += '1\n';
  
  obj.a = 'b';
  
  output.textContent += '2\n';
  
  if (useDeliver) {
    Object.deliverChangeRecords(handleChange);
  }
  
  output.textContent += '3\n';
  
}
<button onclick="runTest(true)">With deliverChangeRecords</button>
<button onclick="runTest(false)">Without deliverChangeRecords</button>
<pre id="output"></pre>