<!DOCTYPE html>
<!--
Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/mre/failure.html">

<script>
'use strict';

tr.exportTo('tr.mre', function() {
  function MreResult(failures, pairs) {
    if (failures === undefined)
      failures = [];
    if (pairs === undefined)
      pairs = {};
    this.failures = failures;
    this.pairs = pairs;
  }

  MreResult.prototype = {
    addFailure: function(failure) {
      this.failures.push(failure);
    },

    addPair: function(key, value) {
      if (key in this.pairs)
        throw new Error('Key ' + key + ' already exists in result.');
      this.pairs[key] = value;
    },

    asDict: function() {
      var d = {
        pairs: this.pairs
      };

      if (this.failures)
        d.failures = this.failures.map(function(f) {return f.asDict();});

      return d;
    },

    hadFailures: function() {
      return this.failures.length > 0;
    }
  };

  MreResult.fromDict = function(resultDict) {
    if (resultDict.failures !== undefined)
      var failures = resultDict.failures.map(tr.mre.Failure.fromDict);
    var pairs = resultDict.pairs;
    return new MreResult(failures, pairs);
  };

  return {
    MreResult: MreResult
  };
});

</script>

<!DOCTYPE html>
<!--
Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/mre/failure.html">

<script>
'use strict';

tr.exportTo('tr.mre', function() {
  class MreResult {
    constructor(failures, pairs) {
      if (failures === undefined)
        failures = [];
      if (pairs === undefined)
        pairs = {};
      this.failures = failures;
      this.pairs = pairs;
    }

    addFailure(failure) {
      this.failures.push(failure);
    }

    addPair(key, value) {
      if (key in this.pairs)
        throw new Error('Key ' + key + ' already exists in result.');
      this.pairs[key] = value;
    }

    asDict() {
      var d = {
        pairs: this.pairs
      };

      if (this.failures)
        d.failures = this.failures.map(function(f) {return f.asDict();});

      return d;
    }

    hadFailures() {
      return this.failures.length > 0;
    }

    static fromDict(resultDict) {
      if (resultDict.failures !== undefined)
        var failures = resultDict.failures.map(tr.mre.Failure.fromDict);
      var pairs = resultDict.pairs;
      return new MreResult(failures, pairs);
    }
  };

  return {
    MreResult: MreResult
  };
});

</script>

Make the JS impl of MreResult a class
[TBMv2] Make the JS impl of MreResult a class

More preparation for making ValueSet a subclass of MreResult.

BUG=catapult:#2717

Review-Url: https://codereview.chromium.org/2274193005
