Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 1c58763

Browse files
committed
porting over old server configs
1 parent 1aaf55a commit 1c58763

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

js/languages/en-US.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@
675675
"password": "Password",
676676
"saveChanges": "Save Changes",
677677
"defaultServerName": "default",
678+
"portedConnectionName": "ported connection",
678679
"connectionFailed": "Connection failed to %{serverName}",
679680
"connectingTo": "Connecting to %{serverName}",
680681
"connectingToDefault": "Connecting to the default server",

js/main.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,41 @@ user.on('change:language', function(md, lang) {
9393

9494
app.serverConfigs = new ServerConfigsCl();
9595
app.serverConfigs.fetch().done(() => {
96+
var oldConfig,
97+
defaultConfig;
98+
9699
if (!app.serverConfigs.getActive()) {
97-
app.serverConfigs.setActive(
98-
app.serverConfigs.serverConfigs.create({
99-
name: polyglot.t('serverConnectModal.defaultServerName'),
100-
default: true
101-
}).id
102-
);
100+
defaultConfig = app.serverConfigs.create({
101+
name: polyglot.t('serverConnectModal.defaultServerName'),
102+
default: true
103+
})
104+
105+
// migrate any existing connection from the
106+
// old single config set-up (_serverConfig-1)
107+
if (oldConfig = localStorage['_serverConfig-1']) {
108+
oldConfig = JSON.parse(oldConfig);
109+
110+
app.serverConfigs.setActive(
111+
app.serverConfigs.create(
112+
__.extend(
113+
{},
114+
__.omit(oldConfig, ['local_username', 'local_password', 'id']),
115+
{ name: polyglot.t('serverConnectModal.portedConnectionName') }
116+
)
117+
).id
118+
);
119+
120+
if (oldConfig.local_username && oldConfig.local_password) {
121+
defaultConfig.save({
122+
local_username: oldConfig.local_username,
123+
local_password: oldConfig.local_password
124+
});
125+
}
126+
127+
localStorage.removeItem('_serverConfig-1');
128+
} else {
129+
app.serverConfigs.setActive(defaultConfig.id);
130+
}
103131
}
104132
});
105133

@@ -397,7 +425,7 @@ launchOnboarding = function(guidCreating) {
397425

398426
pageConnectModal.on('cancel', () => {
399427
removeStartupRetry();
400-
app.getHeartbeatSocket().close();
428+
app.getHeartbeatSocket().cancel();
401429
pageConnectModal.remove();
402430
app.serverConnectModal.open();
403431
}).render().open();
@@ -430,7 +458,7 @@ app.getHeartbeatSocket().on('close', (startUpRetry = function(e) {
430458
Date.now() - startTime < startUpConnectMaxTime &&
431459
startUpConnectMaxRetries
432460
) {
433-
setTimeout(() => {
461+
startUpRetry.timeout = setTimeout(() => {
434462
startUpConnectMaxRetries--;
435463
app.connectHeartbeatSocket();
436464
}, startUpConnectRetryDelay);
@@ -441,8 +469,9 @@ app.getHeartbeatSocket().on('close', (startUpRetry = function(e) {
441469
}));
442470

443471
removeStartupRetry = function() {
472+
clearTimeout(startUpRetry.timeout);
444473
app.getHeartbeatSocket().off('close', startUpRetry);
445-
app.getHeartbeatSocket().on('close', () => {
474+
app.getHeartbeatSocket().on('close', (e) => {
446475
app.serverConnectModal.failConnection(null, app.serverConfigs.getActive())
447476
.open();
448477
});

js/utils/Socket.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ Socket.prototype.connect = function(url) {
1818
this.url = url || this.url;
1919

2020
if (this._socket) {
21-
this._socket.close();
2221
this._socket.onopen = null;
2322
this._socket.onclose = null;
2423
this._socket.onerror = null;
2524
this._socket.onmessage = null;
25+
this._socket.close();
2626
}
2727

2828
this._socket = new WebSocket(this.url);
@@ -57,6 +57,18 @@ Socket.prototype.send = function() {
5757
this._socket.send.apply(this._socket, arguments);
5858
};
5959

60+
Socket.prototype.cancel = function() {
61+
// this will not result in a 'close' event
62+
// todo: perhaps we should fire a 'cancel' event?
63+
if (this._socket) {
64+
this._socket.onopen = null;
65+
this._socket.onclose = null;
66+
this._socket.onerror = null;
67+
this._socket.onmessage = null;
68+
this._socket.close();
69+
}
70+
};
71+
6072
Socket.prototype.getReadyState = function() {
6173
return this._socket.readyState;
6274
};

0 commit comments

Comments
 (0)