Skip to content

Commit c9c9a04

Browse files
refactor(StateQueueManager): convert to ES6 class
1 parent 9a3d015 commit c9c9a04

File tree

3 files changed

+69
-60
lines changed

3 files changed

+69
-60
lines changed

src/state/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export interface StateDeclaration extends ViewDeclaration {
386386
/**
387387
* @deprecated define individual parameters as [[ParamDeclaration.dynamic]]
388388
*/
389-
reloadOnSearch: boolean;
389+
reloadOnSearch?: boolean;
390390
}
391391

392392
export interface StateParams {

src/state/state.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,16 +390,14 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactoryProvider) {
390390

391391
let $transitions: ITransitionService = <any> _$transition;
392392
// Implicit root state that is always active
393-
let rootStateDef = {
393+
let rootStateDef: StateDeclaration = {
394394
name: '',
395395
url: '^',
396396
views: null,
397397
params: {
398398
'#': { value: null, type: 'hash' }
399399
},
400-
// params: [Param.fromPath('#', <Type> paramTypes.types.hash, { value: null })],
401-
path: [],
402-
'abstract': true
400+
abstract: true
403401
};
404402
root = stateQueue.register(rootStateDef, true);
405403

src/state/stateQueueManager.ts

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,84 @@
22
import {extend, inherit, isString, pluck, equalForKeys, abstractKey} from "../common/common";
33
import {StateDeclaration} from "./interface";
44
import {State} from "./module";
5+
import {StateBuilder} from "./stateBuilder";
6+
import {StateService} from "./interface";
57

6-
export function StateQueueManager(states, builder, $urlRouterProvider, $state) {
7-
let queue = [];
8+
export class StateQueueManager {
9+
queue: State[];
810

9-
let queueManager = extend(this, {
10-
register: function(config: StateDeclaration, pre?: boolean) {
11-
// Wrap a new object around the state so we can store our private details easily.
12-
// @TODO: state = new State(extend({}, config, { ... }))
13-
let state = inherit(new State(), extend({}, config, {
14-
self: config,
15-
resolve: config.resolve || {},
16-
toString: () => config.name
17-
}));
11+
constructor(
12+
public states: { [key: string]: State; },
13+
public builder: StateBuilder,
14+
public $urlRouterProvider,
15+
public $state: StateService) {
16+
this.queue = [];
17+
}
1818

19-
if (!isString(state.name)) throw new Error("State must have a valid name");
20-
if (states.hasOwnProperty(state.name) || pluck(queue, 'name').indexOf(state.name) !== -1)
21-
throw new Error(`State '${state.name}' is already defined`);
19+
register(config: StateDeclaration, pre?: boolean) {
20+
let {states, queue, $state} = this;
21+
// Wrap a new object around the state so we can store our private details easily.
22+
// @TODO: state = new State(extend({}, config, { ... }))
23+
let state = inherit(new State(), extend({}, config, {
24+
self: config,
25+
resolve: config.resolve || {},
26+
toString: () => config.name
27+
}));
2228

23-
queue[pre ? "unshift" : "push"](state);
29+
if (!isString(state.name)) throw new Error("State must have a valid name");
30+
if (states.hasOwnProperty(state.name) || pluck(queue, 'name').indexOf(state.name) !== -1)
31+
throw new Error(`State '${state.name}' is already defined`);
2432

25-
if (queueManager.autoFlush) {
26-
queueManager.flush($state);
27-
}
28-
return state;
29-
},
33+
queue[pre ? "unshift" : "push"](state);
3034

31-
flush: function($state) {
32-
let result, state, orphans = [], orphanIdx, previousQueueLength = {};
35+
if (this.autoFlush) {
36+
this.flush($state);
37+
}
38+
return state;
39+
}
3340

34-
while (queue.length > 0) {
35-
state = queue.shift();
36-
result = builder.build(state);
37-
orphanIdx = orphans.indexOf(state);
41+
flush($state) {
42+
let {queue, states, builder} = this;
43+
let result, state, orphans = [], orphanIdx, previousQueueLength = {};
3844

39-
if (result) {
40-
if (states.hasOwnProperty(state.name))
41-
throw new Error(`State '${name}' is already defined`);
42-
states[state.name] = state;
43-
this.attachRoute($state, state);
44-
if (orphanIdx >= 0) orphans.splice(orphanIdx, 1);
45-
continue;
46-
}
45+
while (queue.length > 0) {
46+
state = queue.shift();
47+
result = builder.build(state);
48+
orphanIdx = orphans.indexOf(state);
4749

48-
let prev = previousQueueLength[state.name];
49-
previousQueueLength[state.name] = queue.length;
50-
if (orphanIdx >= 0 && prev === queue.length) {
51-
// Wait until two consecutive iterations where no additional states were dequeued successfully.
52-
throw new Error(`Cannot register orphaned state '${state.name}'`);
53-
} else if (orphanIdx < 0) {
54-
orphans.push(state);
55-
}
50+
if (result) {
51+
if (states.hasOwnProperty(state.name))
52+
throw new Error(`State '${name}' is already defined`);
53+
states[state.name] = state;
54+
this.attachRoute($state, state);
55+
if (orphanIdx >= 0) orphans.splice(orphanIdx, 1);
56+
continue;
57+
}
5658

57-
queue.push(state);
59+
let prev = previousQueueLength[state.name];
60+
previousQueueLength[state.name] = queue.length;
61+
if (orphanIdx >= 0 && prev === queue.length) {
62+
// Wait until two consecutive iterations where no additional states were dequeued successfully.
63+
throw new Error(`Cannot register orphaned state '${state.name}'`);
64+
} else if (orphanIdx < 0) {
65+
orphans.push(state);
5866
}
59-
return states;
60-
},
6167

62-
autoFlush: false,
68+
queue.push(state);
69+
}
70+
return states;
71+
}
6372

64-
attachRoute: function($state, state) {
65-
if (state[abstractKey] || !state.url) return;
73+
autoFlush: boolean = false;
6674

67-
$urlRouterProvider.when(state.url, ['$match', '$stateParams', function ($match, $stateParams) {
68-
if ($state.$current.navigable !== state || !equalForKeys($match, $stateParams)) {
69-
$state.transitionTo(state, $match, { inherit: true, location: false });
70-
}
71-
}]);
72-
}
73-
});
75+
attachRoute($state, state) {
76+
let {$urlRouterProvider} = this;
77+
if (state[abstractKey] || !state.url) return;
78+
79+
$urlRouterProvider.when(state.url, ['$match', '$stateParams', function ($match, $stateParams) {
80+
if ($state.$current.navigable !== state || !equalForKeys($match, $stateParams)) {
81+
$state.transitionTo(state, $match, { inherit: true, location: false });
82+
}
83+
}]);
84+
}
7485
}

0 commit comments

Comments
 (0)