Skip to content

Commit 20eb27a

Browse files
committed
Merge pull request #369 from CodeNow/Instance-list-fetch-speed-improvements
Improving the instance list fetching and so much more!
2 parents 3e0950c + bb66ad3 commit 20eb27a

File tree

21 files changed

+606
-325
lines changed

21 files changed

+606
-325
lines changed

client/config/routes.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ module.exports = [
1212
},
1313
}
1414
},
15-
15+
{
16+
state: 'base',
17+
abstract: true,
18+
url: '^/:userName',
19+
templateUrl: 'viewInstanceLayout',
20+
controller: 'ControllerApp'
21+
},
1622
{
1723
state: 'instance',
1824
abstract: true,

client/controllers/abstractLayouts/controllerInstanceLayout.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,70 @@ require('app')
55
*/
66
function ControllerInstanceLayout(
77
configLogoutURL,
8+
fetchUser,
9+
fetchOrgs,
10+
$stateParams,
11+
QueryAssist,
12+
$state,
13+
$rootScope,
14+
keypather,
15+
async,
816
$scope
917
) {
18+
var thisUser;
19+
fetchUser(function(err, user) {
20+
thisUser = user;
21+
});
1022

23+
function fetchInstances(account) {
24+
async.series([
25+
function (cb) {
26+
$scope.dataInstanceLayout.state.loadingInstances = true;
27+
$rootScope.safeApply(cb);
28+
},
29+
function (cb) {
30+
new QueryAssist(thisUser, cb)
31+
.wrapFunc('fetchInstances', cb)
32+
.query({
33+
githubUsername: account
34+
})
35+
.cacheFetch(function (instances, cached, cb) {
36+
if (account === keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()')) {
37+
if ($rootScope.dataApp.data.instances !== instances) {
38+
$rootScope.dataApp.data.instances = instances;
39+
}
40+
$scope.dataInstanceLayout.state.loadingInstances = false;
41+
$rootScope.safeApply(cb);
42+
} else {
43+
cb();
44+
}
45+
})
46+
.resolve(function (err, projects, cb) {
47+
cb(err);
48+
})
49+
.go();
50+
}
51+
], function (err) {
52+
if (err) { throw err; }
53+
});
54+
}
1155
var dataInstanceLayout = $scope.dataInstanceLayout = {
1256
data: {},
57+
state: {},
1358
actions: {}
1459
};
1560
dataInstanceLayout.data.logoutURL = configLogoutURL();
1661

62+
var instanceListUnwatcher = $scope.$on('INSTANCE_LIST_FETCH', function(event, username) {
63+
fetchInstances(username);
64+
});
65+
66+
$scope.$on('$destroy', function () {
67+
instanceListUnwatcher();
68+
});
69+
70+
if (keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()')) {
71+
fetchInstances(keypather.get($rootScope, 'dataApp.data.activeAccount.oauthName()'));
72+
}
73+
1774
}

client/controllers/controllerApp.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,23 @@ require('app')
77
* @ngInject
88
*/
99
function ControllerApp(
10-
$log,
1110
$scope,
12-
$state,
1311
$rootScope,
1412
$window,
15-
async,
1613
configAPIHost,
1714
configEnvironment,
1815
configLoginURL,
1916
configLogoutURL,
2017
errs,
2118
fetchUser,
22-
keypather,
23-
QueryAssist,
24-
user
19+
fetchOrgs,
20+
keypather
2521
) {
2622

2723
var dataApp = $rootScope.dataApp = $scope.dataApp = {
2824
data: {},
29-
actions: {}
25+
actions: {},
26+
state: {}
3027
};
3128

3229
// used in dev-info box
@@ -41,13 +38,35 @@ function ControllerApp(
4138
data: {},
4239
actions: {}
4340
};
44-
41+
function setActiveAccount(accountName) {
42+
$scope.$watch('dataApp.data.orgs', function(n) {
43+
if (n) {
44+
dataApp.data.instances = null;
45+
var accounts = [thisUser].concat(n.models);
46+
dataApp.data.activeAccount = accounts.find(function (org) {
47+
return (keypather.get(org, 'oauthName().toLowerCase()') === accountName.toLowerCase());
48+
});
49+
if (!dataApp.data.activeAccount) {
50+
dataApp.data.activeAccount = thisUser;
51+
}
52+
$rootScope.$broadcast('INSTANCE_LIST_FETCH', dataApp.data.activeAccount.oauthName());
53+
$rootScope.safeApply();
54+
}
55+
});
56+
}
4557
// shows spinner overlay
4658
dataApp.data.loading = false;
47-
$scope.$on('$stateChangeStart', function () {
59+
$scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, error) {
60+
if (!keypather.get(dataApp, 'data.activeAccount.oauthName()') ||
61+
toParams.userName !== dataApp.data.activeAccount.oauthName()) {
62+
setActiveAccount(toParams.userName);
63+
}
4864
dataApp.data.loading = false;
4965
});
5066

67+
var thisUser;
68+
69+
5170
$scope.$watch(function () {
5271
return errs.errors.length;
5372
}, function(n) {
@@ -66,28 +85,22 @@ function ControllerApp(
6685
$scope.$broadcast('app-document-click');
6786
};
6887

69-
var thisUser,
70-
thisUserOrgs;
7188

72-
function fetchOrgs(user, cb) {
73-
thisUser = user;
74-
thisUserOrgs = thisUser.fetchGithubOrgs(function (err) {
75-
cb(err, thisUserOrgs);
76-
});
77-
}
78-
79-
async.waterfall([
80-
fetchUser,
81-
fetchOrgs
82-
], function(err, results) {
89+
fetchUser(function(err, results) {
90+
thisUser = results;
91+
dataApp.data.user = results;
92+
});
93+
fetchOrgs(function(err, results) {
94+
dataApp.data.orgs = results;
95+
$rootScope.safeApply();
8396
if (err) {
8497
return errs.handler(err);
8598
}
8699
if ($window.heap) {
87100
$window.heap.identify({
88101
name: thisUser.oauthName(),
89102
email: thisUser.attrs.email,
90-
orgs: $window.JSON.stringify(thisUserOrgs)
103+
orgs: $window.JSON.stringify(results)
91104
});
92105
}
93106
if ($window.initIntercom) {

client/controllers/instance/controllerInstance.js

Lines changed: 57 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ require('app')
55
*/
66
function ControllerInstance(
77
async,
8-
determineActiveAccount,
8+
$filter,
99
errs,
1010
keypather,
1111
OpenItems,
1212
QueryAssist,
13-
fetchUser,
13+
$rootScope,
1414
$scope,
1515
$state,
16-
$log,
1716
$stateParams,
18-
exists,
19-
user
17+
fetchUser
2018
) {
2119
var dataInstance = $scope.dataInstance = {
2220
data: {},
@@ -38,7 +36,37 @@ function ControllerInstance(
3836
// in shows/hides file-menu
3937
in: false
4038
};
41-
39+
if (!$stateParams.instanceName) {
40+
var unwatch = $rootScope.$watch('dataApp.data.instances', function (n, p) {
41+
if (n !== p && n) {
42+
unwatch();
43+
if (n.models.length) {
44+
var models = $filter('orderBy')(n.models, 'attrs.name');
45+
$state.go('instance.instance', {
46+
instanceName: models[0].attrs.name,
47+
userName: $stateParams.userName
48+
}, {location: 'replace'});
49+
} else {
50+
$state.go('instance.new', {
51+
userName: $stateParams.userName
52+
}, {location: 'replace'});
53+
}
54+
}
55+
});
56+
} else if ($stateParams.instanceName && $stateParams.userName) {
57+
async.waterfall([
58+
fetchUser,
59+
fetchInstance
60+
], function (err) {
61+
if (err) {
62+
$state.go('instance.instance', {
63+
instanceName: '',
64+
userName: $stateParams.userName
65+
}, {reload: true});
66+
}
67+
errs.handler(err);
68+
});
69+
}
4270
// watch showExplorer (toggle when user clicks file menu)
4371
// if no running container, return early (user shouldn't be able to even click
4472
// button in this situation)
@@ -88,68 +116,29 @@ function ControllerInstance(
88116
}
89117
}
90118

91-
async.waterfall([
92-
determineActiveAccount,
93-
function(activeAccount, cb) {
94-
data.activeAccount = activeAccount;
95-
$scope.safeApply();
96-
cb();
97-
},
98-
function (cb) {
99-
fetchUser(function(err, user) {
100-
if (err) { return cb(err); }
101-
data.user = user;
102-
$scope.safeApply();
103-
cb();
104-
});
105-
},
106-
fetchInstance,
107-
fetchInstances
108-
], errs.handler);
109-
110-
// This is to fetch the list of instances. This is separate so the page can load quickly
111-
// since it will have its instance. Only the modals use this list
112-
function fetchInstances(cb) {
113-
new QueryAssist(data.user, cb)
114-
.wrapFunc('fetchInstances', cb)
115-
.query({
116-
githubUsername: $stateParams.userName
117-
})
118-
.cacheFetch(function (instances, cached, cb) {
119-
if (!cached && instances.models.length === 0) {
120-
return cb(new Error('instance not found'));
121-
}
122-
data.instances = instances;
123-
$scope.safeApply();
124-
cb();
125-
})
126-
.resolve(function (err) {
127-
cb(err);
128-
})
129-
.go();
130-
}
131-
132-
function fetchInstance(cb) {
133-
new QueryAssist(data.user, cb)
134-
.wrapFunc('fetchInstances')
135-
.query({
136-
githubUsername: $stateParams.userName,
137-
name: $stateParams.instanceName
138-
})
139-
.cacheFetch(function (instances, cached, cb) {
140-
if (!cached && instances.models.length === 0) {
141-
return cb(new Error('Instance not found'));
142-
}
143-
var instance = instances.models[0];
144-
data.instance = instance;
145-
data.instance.state = {};
146-
$scope.safeApply();
147-
cb();
148-
})
149-
.resolve(function (err) {
150-
cb(err);
151-
})
152-
.go();
119+
function fetchInstance(user, cb) {
120+
if ($stateParams.instanceName && $stateParams.userName) {
121+
new QueryAssist(user, cb)
122+
.wrapFunc('fetchInstances')
123+
.query({
124+
githubUsername: $stateParams.userName,
125+
name: $stateParams.instanceName
126+
})
127+
.cacheFetch(function (instances, cached, cb) {
128+
data.instance = keypather.get(instances, 'models[0]');
129+
if (!data.instance) {
130+
cb(new Error('Could not find instance on server'));
131+
} else {
132+
data.instance.state = {};
133+
$scope.safeApply();
134+
cb();
135+
}
136+
})
137+
.resolve(function (err) {
138+
cb(err);
139+
})
140+
.go();
141+
}
153142
}
154143

155144
function watchForContainerBeforeDisplayTabs (container) {

client/controllers/setup/controllerNew.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ require('app')
66
function ControllerNew(
77
async,
88
hasKeypaths,
9-
QueryAssist,
109
errs,
1110
fetchUser,
1211
$scope,
1312
$state,
14-
uuid,
15-
user
13+
uuid
1614
) {
1715

1816
var thisUser;

0 commit comments

Comments
 (0)