123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- goog.provide('goog.result');
- goog.require('goog.array');
- goog.require('goog.result.DependentResult');
- goog.require('goog.result.Result');
- goog.require('goog.result.SimpleResult');
- goog.result.successfulResult = function(value) {
- var result = new goog.result.SimpleResult();
- result.setValue(value);
- return result;
- };
- goog.result.failedResult = function(opt_error) {
- var result = new goog.result.SimpleResult();
- result.setError(opt_error);
- return result;
- };
- goog.result.canceledResult = function() {
- var result = new goog.result.SimpleResult();
- result.cancel();
- return result;
- };
- goog.result.wait = function(result, handler, opt_scope) {
- result.wait(handler, opt_scope);
- };
- goog.result.waitOnSuccess = function(result, handler, opt_scope) {
- goog.result.wait(result, function(res) {
- if (res.getState() == goog.result.Result.State.SUCCESS) {
-
- handler.call(this, res.getValue(), res);
- }
- }, opt_scope);
- };
- goog.result.waitOnError = function(result, handler, opt_scope) {
- goog.result.wait(result, function(res) {
- if (res.getState() == goog.result.Result.State.ERROR) {
-
- handler.call(this, res.getError(), res);
- }
- }, opt_scope);
- };
- goog.result.transform = function(result, transformer) {
- var returnedResult = new goog.result.DependentResultImpl_([result]);
- goog.result.wait(result, function(res) {
- if (res.getState() == goog.result.Result.State.SUCCESS) {
- returnedResult.setValue(transformer(res.getValue()));
- } else {
- returnedResult.setError(res.getError());
- }
- });
- return returnedResult;
- };
- goog.result.chain = function(result, actionCallback, opt_scope) {
- var dependentResult = new goog.result.DependentResultImpl_([result]);
-
- goog.result.wait(result, function(result) {
- if (result.getState() == goog.result.Result.State.SUCCESS) {
-
- var contingentResult = actionCallback.call(opt_scope, result);
- dependentResult.addParentResult(contingentResult);
- goog.result.wait(contingentResult, function(contingentResult) {
-
-
- if (contingentResult.getState() == goog.result.Result.State.SUCCESS) {
- dependentResult.setValue(contingentResult.getValue());
- } else {
- dependentResult.setError(contingentResult.getError());
- }
- });
- } else {
-
- dependentResult.setError(result.getError());
- }
- });
- return dependentResult;
- };
- goog.result.combine = function(var_args) {
-
- var results = goog.array.clone(arguments);
- var combinedResult = new goog.result.DependentResultImpl_(results);
- var isResolved = function(res) {
- return res.getState() != goog.result.Result.State.PENDING;
- };
- var checkResults = function() {
- if (combinedResult.getState() == goog.result.Result.State.PENDING &&
- goog.array.every(results, isResolved)) {
- combinedResult.setValue(results);
- }
- };
- goog.array.forEach(
- results, function(result) { goog.result.wait(result, checkResults); });
- return combinedResult;
- };
- goog.result.combineOnSuccess = function(var_args) {
- var results = goog.array.clone(arguments);
- var combinedResult = new goog.result.DependentResultImpl_(results);
- var resolvedSuccessfully = function(res) {
- return res.getState() == goog.result.Result.State.SUCCESS;
- };
- goog.result.wait(
- goog.result.combine.apply(goog.result.combine, results),
-
- function(res) {
- var results =
- (res.getValue());
- if (goog.array.every(results, resolvedSuccessfully)) {
- combinedResult.setValue(results);
- } else {
- combinedResult.setError(results);
- }
- });
- return combinedResult;
- };
- goog.result.cancelParentResults = function(dependentResult) {
- var anyCanceled = false;
- var results = dependentResult.getParentResults();
- for (var n = 0; n < results.length; n++) {
- anyCanceled |= results[n].cancel();
- }
- return !!anyCanceled;
- };
- goog.result.DependentResultImpl_ = function(parentResults) {
- goog.result.DependentResultImpl_.base(this, 'constructor');
-
- this.parentResults_ = parentResults;
- };
- goog.inherits(goog.result.DependentResultImpl_, goog.result.SimpleResult);
- goog.result.DependentResultImpl_.prototype.addParentResult = function(
- parentResult) {
- this.parentResults_.push(parentResult);
- };
- goog.result.DependentResultImpl_.prototype.getParentResults = function() {
- return this.parentResults_;
- };
|