Skip to content

Commit 0a3cddb

Browse files
committed
Merge pull request element-hq#13 from matrix-org/markjh/vector-cleanup-client-peg
Markjh/vector cleanup client peg
2 parents e9b2cd1 + 2802253 commit 0a3cddb

4 files changed

Lines changed: 40 additions & 44 deletions

File tree

‎src/MatrixClientPeg.js‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,25 @@ var Matrix = require("matrix-js-sdk");
2222
var matrixClient = null;
2323

2424
var localStorage = window.localStorage;
25+
26+
function createClient(hs_url, is_url, user_id, access_token) {
27+
var opts = {
28+
baseUrl: hs_url,
29+
idBaseUrl: is_url,
30+
accessToken: access_token,
31+
userId: user_id
32+
};
33+
34+
matrixClient = Matrix.createClient(opts);
35+
}
36+
2537
if (localStorage) {
2638
var hs_url = localStorage.getItem("mx_hs_url");
2739
var is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org';
2840
var access_token = localStorage.getItem("mx_access_token");
2941
var user_id = localStorage.getItem("mx_user_id");
3042
if (access_token && user_id && hs_url) {
31-
matrixClient = Matrix.createClient({
32-
baseUrl: hs_url,
33-
idBaseUrl: is_url,
34-
accessToken: access_token,
35-
userId: user_id
36-
});
43+
createClient(hs_url, is_url, user_id, access_token);
3744
}
3845
}
3946

@@ -42,15 +49,32 @@ module.exports = {
4249
return matrixClient;
4350
},
4451

45-
replace: function(cli) {
46-
matrixClient = cli;
52+
unset: function() {
53+
matrixClient = null;
4754
},
4855

4956
replaceUsingUrls: function(hs_url, is_url) {
5057
matrixClient = Matrix.createClient({
5158
baseUrl: hs_url,
5259
idBaseUrl: is_url
5360
});
61+
},
62+
63+
replaceUsingAccessToken: function(hs_url, is_url, user_id, access_token) {
64+
createClient(hs_url, is_url, user_id, access_token);
65+
if (localStorage) {
66+
try {
67+
localStorage.clear();
68+
localStorage.setItem("mx_hs_url", hs_url);
69+
localStorage.setItem("mx_is_url", is_url);
70+
localStorage.setItem("mx_user_id", user_id);
71+
localStorage.setItem("mx_access_token", access_token);
72+
} catch (e) {
73+
console.warn("Error using local storage: can't persist session!");
74+
}
75+
} else {
76+
console.warn("No local storage available: can't persist session!");
77+
}
5478
}
5579
};
5680

‎src/controllers/pages/MatrixChat.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ module.exports = {
9191
Notifier.stop();
9292
Presence.stop();
9393
MatrixClientPeg.get().removeAllListeners();
94-
MatrixClientPeg.replace(null);
94+
MatrixClientPeg.unset();
9595
break;
9696
case 'start_registration':
9797
if (this.state.logged_in) return;

‎src/controllers/templates/Login.js‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,10 @@ module.exports = {
7575
'user': formVals.username,
7676
'password': formVals.password
7777
}).done(function(data) {
78-
// XXX: we assume this means we're logged in, but there could be a next stage
79-
MatrixClientPeg.replace(Matrix.createClient({
80-
baseUrl: self.state.hs_url,
81-
idBaseUrl: self.state.is_url,
82-
userId: data.user_id,
83-
accessToken: data.access_token
84-
}));
85-
var localStorage = window.localStorage;
86-
if (localStorage) {
87-
try {
88-
localStorage.clear();
89-
localStorage.setItem("mx_hs_url", self.state.hs_url);
90-
localStorage.setItem("mx_is_url", self.state.is_url);
91-
localStorage.setItem("mx_user_id", data.user_id);
92-
localStorage.setItem("mx_access_token", data.access_token);
93-
} catch (e) {
94-
console.warn("Error using local storage: can't persist session!");
95-
}
96-
} else {
97-
console.warn("No local storage available: can't persist session!");
98-
}
78+
MatrixClientPeg.replaceUsingAccessToken(
79+
self.state.hs_url, self.state.is_url,
80+
data.user_id, data.access_token
81+
);
9982
if (self.props.onLoggedIn) {
10083
self.props.onLoggedIn();
10184
}

‎src/controllers/templates/Register.js‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,9 @@ module.exports = {
259259
},
260260

261261
onRegistered: function(user_id, access_token) {
262-
MatrixClientPeg.replace(Matrix.createClient({
263-
baseUrl: this.state.hs_url,
264-
idBaseUrl: this.state.is_url,
265-
userId: user_id,
266-
accessToken: access_token
267-
}));
268-
var localStorage = window.localStorage;
269-
if (localStorage) {
270-
localStorage.setItem("mx_hs_url", this.state.hs_url);
271-
localStorage.setItem("mx_user_id", user_id);
272-
localStorage.setItem("mx_access_token", access_token);
273-
} else {
274-
console.warn("No local storage available: can't persist session!");
275-
}
262+
MatrixClientPeg.replaceUsingAccessToken(
263+
this.state.hs_url, this.state.is_url, user_id, access_token
264+
);
276265
if (this.props.onLoggedIn) {
277266
this.props.onLoggedIn();
278267
}

0 commit comments

Comments
 (0)