require('es6-set/implement');

var findIndex = require('find-index');

function getSelectedValue(options) {
  return options.values[
    findIndex(options.radioNodes, function(node) {
      return node.checked;
    })
  ];
}

module.exports = function (args) {
  var view = args.view;
  var model = args.model;
  // TODO: Support `args.logger`.

  var checkedOptionSnapshot;
  function updateCheckedOption(newValue) {
    if (newValue !== checkedOptionSnapshot) {
      model.patches.emit('apply', {value: newValue});
    }

    checkedOptionSnapshot = newValue;
  }

  function updateFromOptions(options) {
    return updateCheckedOption(getSelectedValue(options));
  }

  // Rescan options and emit a patch if necessary:
  // * when options are added, removed or changed,
  var optionsSnapshot = {};
  view.options.when('update', function(options) {
    updateFromOptions(options);
    optionsSnapshot = options;
  });

  // * upon a `change` event on the container – when something has been
  // selected.
  view.containerElement.on('change', function() {
    updateFromOptions(optionsSnapshot);
  });

  // Update the selected option when the `value` attribute has been updated.
  model.updates.when('value', function(data) {
    // TODO: Can we assume that event messages are safe?

    // Fail silently:
    // * if there are no options loaded yet.
    if (!optionsSnapshot.radioNodes || !optionsSnapshot.values) return;

    // * if there is no option provided for the value.
    var option = optionsSnapshot.radioNodes[
      optionsSnapshot.values.indexOf(data.attributes.value)
    ];
    if (!option) return;
      // TODO: Should we really fail silently?

    // Else check the respective option.
    option.checked = true;
  });
};

@@ -1215,20 +1215,21 @@
 unction(
-data
+state
 ) %7B%0A    
@@ -1285,455 +1285,99 @@
 fe?%0A
-%0A
     
-// Fail silently:%0A    // * if there are no options loaded yet.%0A    if (!optionsSnapshot.radioNodes %7C%7C !optionsSnapshot.values) return;%0A%0A    // * if there is no option provided for the value.%0A    var option = optionsSnapshot.radioNodes%5B%0A      optionsSnapshot.values.indexOf(data.attributes.value)%0A    %5D;%0A    if (!option) return;%0A      // TODO: Should we really fail silently?%0A%0A    // Else check the respective option.%0A    option.checked = true
+view.selection.emit('update', %7B%0A      newValue: (state.attributes.value %7C%7C null)%0A    %7D)
 ;%0A  

Implement the new behavior of the “value” plugin [#6798]