aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/result/deferredadaptor.js
blob: 3c6a9abf271cd01641b9d196d6e0039925eb2f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview An adaptor from a Result to a Deferred.
 *
 * TODO (vbhasin): cancel() support.
 * TODO (vbhasin): See if we can make this a static.
 * TODO (gboyer, vbhasin): Rename to "Adapter" once this graduates; this is the
 * proper programmer spelling.
 */


goog.provide('goog.result.DeferredAdaptor');

goog.require('goog.async.Deferred');
goog.require('goog.result');
goog.require('goog.result.Result');



/**
 * An adaptor from Result to a Deferred, for use with existing Deferred chains.
 *
 * @param {!goog.result.Result} result A result.
 * @constructor
 * @extends {goog.async.Deferred}
 */
goog.result.DeferredAdaptor = function(result) {
  goog.base(this);
  goog.result.wait(result, function(result) {
    if (this.hasFired()) {
      return;
    }
    if (result.getState() == goog.result.Result.State.SUCCESS) {
      this.callback(result.getValue());
    } else if (result.getState() == goog.result.Result.State.ERROR) {
      if (result.getError() instanceof goog.result.Result.CancelError) {
        this.cancel();
      } else {
        this.errback(result.getError());
      }
    }
  }, this);
};
goog.inherits(goog.result.DeferredAdaptor, goog.async.Deferred);