Skip to content

Commit

Permalink
feat(stateService): add transition option 'supercede' so transition c…
Browse files Browse the repository at this point in the history
…an be ignored if one is pending
  • Loading branch information
Rafael Fernandez Serra authored and christopherthielen committed Dec 19, 2019
1 parent 4a771bc commit 6e5a56f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/state/stateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ export class StateService {

if (!ref.valid()) return <TransitionPromise>silentRejection(ref.error());

if (options.supercede === false && getCurrent()) {
return <TransitionPromise>Rejection.ignored('Another transition is in progress and supercede has been set to false in TransitionOptions for the transition. So the transition was ignored in favour of the existing one in progress.').toPromise();
}

/**
* Special handling for Ignored, Aborted, and Redirected transitions
*
Expand Down
12 changes: 12 additions & 0 deletions src/transition/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ export interface TransitionOptions {
* You can define your own Transition Options inside this property and use them, e.g., from a Transition Hook
*/
custom?: any;
/**
* This option may be used to cancel the active transition (if one is active) in favour of the this one.
* This is the default behaviour or ui-router.
*
*
* - When `true`, the active transition will be canceled and new transition will begin.
* - when `false`, the transition will be canceled if a transition is already running. This can be useful in cases where
* you only want to navigate to a different state if you are not already navigating somewhere.
*
* @default `true`
*/
supercede?: boolean;
/** @internalapi */
reloadState?: StateObject;
/** @internalapi
Expand Down
1 change: 1 addition & 0 deletions src/transition/transitionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export let defaultTransOpts: TransitionOptions = {
inherit: false,
notify: true,
reload: false,
supercede: true,
custom: {},
current: () => null,
source: 'unknown',
Expand Down
19 changes: 19 additions & 0 deletions test/stateServiceSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,25 @@ describe('stateService', function() {
done();
});

describe('when supercede TransitionOption is false', () => {
it('ignores transition if another transition is running', async done => {
$state.defaultErrorHandler(() => null);
await initStateTo(A);

const activeTransition = $state.transitionTo(B, {});
const superseded = await $state.transitionTo(C, {}, { supercede: false }).catch(err => err);

const result: Rejection = await superseded;
expect(result.type).toEqual(RejectType.IGNORED);

await activeTransition;

expect($state.current).toBe(B);

done();
});
});

it('aborts pending transitions (last call wins)', async done => {
$state.defaultErrorHandler(() => null);
await initStateTo(A);
Expand Down

0 comments on commit 6e5a56f

Please sign in to comment.