Skip to content

Commit

Permalink
Fixed #59 so pipeline is correctly aborted when another transition be…
Browse files Browse the repository at this point in the history
…gins before the last one completes.
  • Loading branch information
jeme committed Aug 23, 2014
1 parent cba327f commit cec6e56
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 41 deletions.
28 changes: 19 additions & 9 deletions build/angular-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,23 +927,25 @@ function $PipelineProvider() {
var promise = $q.when(locals);
forEach(all, function (stage) {
promise = promise.then(function () {
if (locals.$error) {
return;
//throw Error(locals.$error);
}
return stage(locals);
});
});
promise.then(defered.resolve, onReject);
promise.then(defered.resolve, defered.reject);

function onReject(error) {
defered.promise.catch(function onReject(error) {
locals.$error = error;
forEach(errorHandlers, function (handler) {
handler = $inject.create(handler);
handler(locals);
});
});

defered.reject(error);
}
return defered;
};

return sv;
}];
}
Expand Down Expand Up @@ -1056,6 +1058,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider).config

pipeline.append('step7', [
'$changes', '$context', '$rootScope', '$state', '$q', function ($changes, $context, $rootScope, $state, $q) {
//TODO: Pull event broadcasting back into state service...
if ($rootScope.$broadcast(EVENTS.STATE_CHANGE_START, $context.toState, $state.current).defaultPrevented) {
return $q.reject('Rejected state transition as canceled by user in ' + EVENTS.STATE_CHANGE_START + '.');
}
Expand Down Expand Up @@ -1094,6 +1097,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider).config

pipeline.append('step10', [
'$changes', '$context', '$state', '$rootScope', '$args', function ($changes, $context, $state, $rootScope, $args) {
//TODO: Pull event broadcasting back into state service...
var fromState = $state.current;
$state.params = $args.params;
$state.current = $context.toState;
Expand All @@ -1116,6 +1120,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider).config

pipeline.error('error01', [
'$changes', '$context', '$rootScope', '$state', '$error', function ($changes, $context, $rootScope, $state, $error) {
//TODO: Pull event broadcasting back into state service...
$rootScope.$broadcast(EVENTS.STATE_CHANGE_ERROR, $context.toState, $state.current, $error);
if ($context.transaction && !$context.transaction.completed) {
$context.transaction.cancel();
Expand Down Expand Up @@ -2203,7 +2208,7 @@ var $StateProvider = [
},
goto: function (state, params) {
return initPromise.then(function () {
goto({
return goto({
state: state,
params: buildParams(params),
updateroute: true
Expand All @@ -2215,7 +2220,7 @@ var $StateProvider = [
},
reload: function (state) {
return initPromise.then(function () {
reload(state);
return reload(state);
});
},
url: function (arg1, arg2, arg3) {
Expand Down Expand Up @@ -2308,14 +2313,17 @@ var $StateProvider = [
forceReload = current.fullname;
}

var defered = $q.defer();
$rootScope.$evalAsync(function () {
goto({ state: current, params: $state.params, force: forceReload });
goto({ state: current, params: $state.params, force: forceReload }).then(defered.resolve, defered.reject);
});
return defered.promise;
}

var comparer = new StateComparer();
var running, inProgress = false;
function goto(args) {
var defered = $q.defer();
if (inProgress) {
running.reject("Transition defered by another call to goto");
}
Expand All @@ -2328,9 +2336,11 @@ var $StateProvider = [
running = $pipeline.run({ $changes: changes, $context: context, $args: args });
running.promise.then(function () {
current = changes.to;
}).finally(function () {
defered.resolve(current);
}).catch(defered.reject).finally(function () {
inProgress = false;
});
return defered.promise;
}
return $state;
}];
Expand Down
2 changes: 1 addition & 1 deletion build/angular-routing.min.js

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions build/src/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,25 @@ function $PipelineProvider() {
var promise = $q.when(locals);
forEach(all, function (stage) {
promise = promise.then(function () {
if (locals.$error) {
return;
//throw Error(locals.$error);
}
return stage(locals);
});
});
promise.then(defered.resolve, onReject);
promise.then(defered.resolve, defered.reject);

function onReject(error) {
defered.promise.catch(function onReject(error) {
locals.$error = error;
forEach(errorHandlers, function (handler) {
handler = $inject.create(handler);
handler(locals);
});
});

defered.reject(error);
}
return defered;
};

return sv;
}];
}
Expand Down Expand Up @@ -224,6 +226,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider).config

pipeline.append('step7', [
'$changes', '$context', '$rootScope', '$state', '$q', function ($changes, $context, $rootScope, $state, $q) {
//TODO: Pull event broadcasting back into state service...
if ($rootScope.$broadcast(EVENTS.STATE_CHANGE_START, $context.toState, $state.current).defaultPrevented) {
return $q.reject('Rejected state transition as canceled by user in ' + EVENTS.STATE_CHANGE_START + '.');
}
Expand Down Expand Up @@ -262,6 +265,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider).config

pipeline.append('step10', [
'$changes', '$context', '$state', '$rootScope', '$args', function ($changes, $context, $state, $rootScope, $args) {
//TODO: Pull event broadcasting back into state service...
var fromState = $state.current;
$state.params = $args.params;
$state.current = $context.toState;
Expand All @@ -284,6 +288,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider).config

pipeline.error('error01', [
'$changes', '$context', '$rootScope', '$state', '$error', function ($changes, $context, $rootScope, $state, $error) {
//TODO: Pull event broadcasting back into state service...
$rootScope.$broadcast(EVENTS.STATE_CHANGE_ERROR, $context.toState, $state.current, $error);
if ($context.transaction && !$context.transaction.completed) {
$context.transaction.cancel();
Expand Down
13 changes: 9 additions & 4 deletions build/src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ var $StateProvider = [
},
goto: function (state, params) {
return initPromise.then(function () {
goto({
return goto({
state: state,
params: buildParams(params),
updateroute: true
Expand All @@ -528,7 +528,7 @@ var $StateProvider = [
},
reload: function (state) {
return initPromise.then(function () {
reload(state);
return reload(state);
});
},
url: function (arg1, arg2, arg3) {
Expand Down Expand Up @@ -621,14 +621,17 @@ var $StateProvider = [
forceReload = current.fullname;
}

var defered = $q.defer();
$rootScope.$evalAsync(function () {
goto({ state: current, params: $state.params, force: forceReload });
goto({ state: current, params: $state.params, force: forceReload }).then(defered.resolve, defered.reject);
});
return defered.promise;
}

var comparer = new StateComparer();
var running, inProgress = false;
function goto(args) {
var defered = $q.defer();
if (inProgress) {
running.reject("Transition defered by another call to goto");
}
Expand All @@ -641,9 +644,11 @@ var $StateProvider = [
running = $pipeline.run({ $changes: changes, $context: context, $args: args });
running.promise.then(function () {
current = changes.to;
}).finally(function () {
defered.resolve(current);
}).catch(defered.reject).finally(function () {
inProgress = false;
});
return defered.promise;
}
return $state;
}];
Expand Down
28 changes: 19 additions & 9 deletions gh-pages/assets/scripts/angular-routing/impl/angular-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,23 +927,25 @@ function $PipelineProvider() {
var promise = $q.when(locals);
forEach(all, function (stage) {
promise = promise.then(function () {
if (locals.$error) {
return;
//throw Error(locals.$error);
}
return stage(locals);
});
});
promise.then(defered.resolve, onReject);
promise.then(defered.resolve, defered.reject);

function onReject(error) {
defered.promise.catch(function onReject(error) {
locals.$error = error;
forEach(errorHandlers, function (handler) {
handler = $inject.create(handler);
handler(locals);
});
});

defered.reject(error);
}
return defered;
};

return sv;
}];
}
Expand Down Expand Up @@ -1056,6 +1058,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider).config

pipeline.append('step7', [
'$changes', '$context', '$rootScope', '$state', '$q', function ($changes, $context, $rootScope, $state, $q) {
//TODO: Pull event broadcasting back into state service...
if ($rootScope.$broadcast(EVENTS.STATE_CHANGE_START, $context.toState, $state.current).defaultPrevented) {
return $q.reject('Rejected state transition as canceled by user in ' + EVENTS.STATE_CHANGE_START + '.');
}
Expand Down Expand Up @@ -1094,6 +1097,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider).config

pipeline.append('step10', [
'$changes', '$context', '$state', '$rootScope', '$args', function ($changes, $context, $state, $rootScope, $args) {
//TODO: Pull event broadcasting back into state service...
var fromState = $state.current;
$state.params = $args.params;
$state.current = $context.toState;
Expand All @@ -1116,6 +1120,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider).config

pipeline.error('error01', [
'$changes', '$context', '$rootScope', '$state', '$error', function ($changes, $context, $rootScope, $state, $error) {
//TODO: Pull event broadcasting back into state service...
$rootScope.$broadcast(EVENTS.STATE_CHANGE_ERROR, $context.toState, $state.current, $error);
if ($context.transaction && !$context.transaction.completed) {
$context.transaction.cancel();
Expand Down Expand Up @@ -2203,7 +2208,7 @@ var $StateProvider = [
},
goto: function (state, params) {
return initPromise.then(function () {
goto({
return goto({
state: state,
params: buildParams(params),
updateroute: true
Expand All @@ -2215,7 +2220,7 @@ var $StateProvider = [
},
reload: function (state) {
return initPromise.then(function () {
reload(state);
return reload(state);
});
},
url: function (arg1, arg2, arg3) {
Expand Down Expand Up @@ -2308,14 +2313,17 @@ var $StateProvider = [
forceReload = current.fullname;
}

var defered = $q.defer();
$rootScope.$evalAsync(function () {
goto({ state: current, params: $state.params, force: forceReload });
goto({ state: current, params: $state.params, force: forceReload }).then(defered.resolve, defered.reject);
});
return defered.promise;
}

var comparer = new StateComparer();
var running, inProgress = false;
function goto(args) {
var defered = $q.defer();
if (inProgress) {
running.reject("Transition defered by another call to goto");
}
Expand All @@ -2328,9 +2336,11 @@ var $StateProvider = [
running = $pipeline.run({ $changes: changes, $context: context, $args: args });
running.promise.then(function () {
current = changes.to;
}).finally(function () {
defered.resolve(current);
}).catch(defered.reject).finally(function () {
inProgress = false;
});
return defered.promise;
}
return $state;
}];
Expand Down

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions src/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,26 @@ function $PipelineProvider() {
var defered = $q.defer();
var promise = $q.when(locals);
forEach(all, stage => {
promise = promise.then(() => stage(locals));
promise = promise.then(function () {
if (locals.$error) {
return;
//throw Error(locals.$error);
}
return stage(locals);
});
});
promise.then(defered.resolve, onReject);
promise.then(defered.resolve, defered.reject);

function onReject(error) {
defered.promise.catch(function onReject(error) {
locals.$error = error;
forEach(errorHandlers, handler => {
handler = $inject.create(handler);
handler(locals);
});

defered.reject(error);
}
});

return defered;
};

return sv;
}];
}
Expand Down Expand Up @@ -215,6 +219,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider)
}]);

pipeline.append('step7', ['$changes', '$context', '$rootScope', '$state', '$q', function ($changes, $context, $rootScope, $state, $q) {
//TODO: Pull event broadcasting back into state service...
if ($rootScope.$broadcast(EVENTS.STATE_CHANGE_START, $context.toState, $state.current).defaultPrevented) {
return $q.reject('Rejected state transition as canceled by user in ' + EVENTS.STATE_CHANGE_START+'.');
}
Expand Down Expand Up @@ -254,6 +259,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider)
}]);

pipeline.append('step10', ['$changes', '$context', '$state', '$rootScope', '$args', function ($changes, $context, $state, $rootScope, $args) {
//TODO: Pull event broadcasting back into state service...
var fromState = $state.current;
$state.params = $args.params;
$state.current = $context.toState;
Expand All @@ -275,6 +281,7 @@ angular.module('dotjem.routing').provider('$pipeline', $PipelineProvider)
}]);

pipeline.error('error01', ['$changes', '$context', '$rootScope', '$state', '$error', function ($changes, $context, $rootScope, $state, $error) {
//TODO: Pull event broadcasting back into state service...
$rootScope.$broadcast(EVENTS.STATE_CHANGE_ERROR, $context.toState, $state.current, $error);
if ($context.transaction && !$context.transaction.completed) {
$context.transaction.cancel();
Expand Down
Loading

0 comments on commit cec6e56

Please sign in to comment.