Skip to content
This repository has been archived by the owner on Sep 20, 2020. It is now read-only.

Commit

Permalink
feat(future): Allow future states to be retried after a failed lazy l…
Browse files Browse the repository at this point in the history
…oad attempt

Closes #196
  • Loading branch information
christopherthielen committed Jun 9, 2015
1 parent 9c4be9f commit 6e6f3ec
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/future.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,31 @@
return deferred.promise;
}

var promise = $q.when([]), parentFuture = futureState.parentFutureState;
var parentPromises = $q.when([]), parentFuture = futureState.parentFutureState;
if (parentFuture && futureStates[parentFuture.name]) {
promise = lazyLoadState($injector, futureStates[parentFuture.name]);
parentPromises = lazyLoadState($injector, futureStates[parentFuture.name]);
}

var type = futureState.type;
var factory = stateFactories[type];
if (!factory) throw Error("No state factory for futureState.type: " + (futureState && futureState.type));
return promise
.then(function(array) {
var injectorPromise = $injector.invoke(factory, factory, { futureState: futureState });
return injectorPromise.then(function(fullState) {
if (fullState) { array.push(fullState); } // Pass a chain of realized states back
return array;
});
})
["finally"](function() { // IE8 hack
delete(futureStates[futureState.name]);
});

var failedLoadPolicy = factory.$options && factory.$options.failedLazyLoadPolicy || "remove";
function deregisterFutureState() { delete(futureStates[futureState.name]); }
function errorHandler(err) {
if (failedLoadPolicy === "remove") deregisterFutureState();
return $q.reject(err);
}

return parentPromises.then(function(array) {
var factoryPromise = $injector.invoke(factory, factory, { futureState: futureState });

return factoryPromise.then(function(fullState) {
deregisterFutureState(); // Success; remove future state
if (fullState) { array.push(fullState); } // Pass a chain of realized states back
return array;
});
}).catch(errorHandler)
}

var otherwiseFunc = [ '$log', '$location',
Expand Down Expand Up @@ -228,7 +234,7 @@
lazyloadInProgress = false;
}, function (error) {
console.log("failed to lazy load state ", error);
$state.go(fromState, fromParams);
if (fromState.name) $state.go(fromState, fromParams);
lazyloadInProgress = false;
});
});
Expand Down
43 changes: 43 additions & 0 deletions test/futureSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,48 @@ describe('futureState', function () {
_futureStateProvider.futureState(futureState("nourl.issue167", undefined, undefined, "iframe")); // no url
testGo("nourl.issue167");
});

// Test 1 for enhancement issue #196
it("should remove future states that properly load", function() {
var name = "top.foo";

expect($futureState.get()[name]).toBeDefined();

$state.go(name);
$q.flush();

expect($state.current.name).toBe(name);
expect($futureState.get()[name]).toBeUndefined();
});

// Test 2 for enhancement issue #196
it("should remove future states that failed to load, if no policy is set", function() {
var name = "top.bar";

expect($futureState.get()[name]).toBeDefined();

$state.go(name);
$q.flush();

expect($state.current.name).toBe("");
expect($futureState.get()[name]).toBeUndefined();
});

// Test 3 for enhancement issue #196
it("should remove future states that failed to load, if no policy is set", function() {
var name = "top.bar";

var factory = function (futureState) { return $q.reject("doesntwork"); };
factory.$options = { failedLazyLoadPolicy: "retain" };
_futureStateProvider.stateFactory('doesntwork', factory);

expect($futureState.get()[name]).toBeDefined();

$state.go(name);
$q.flush();

expect($state.current.name).toBe("");
expect($futureState.get()[name]).toBeDefined();
})
});
});

0 comments on commit 6e6f3ec

Please sign in to comment.