cache auth token to prevent race condition on set

This commit is contained in:
Jeremy Kauffman 2017-08-03 12:15:52 -04:00
parent cae8e3701f
commit da8ed48fa7
2 changed files with 13 additions and 5 deletions

View file

@ -22,4 +22,4 @@
"lbrySettings": {
"lbrynetDaemonVersion": "0.14.2"
}
}
}

View file

@ -113,16 +113,24 @@ lbryio.call = function(resource, action, params = {}, method = "get") {
});
};
lbryio._authToken = null;
lbryio.getAuthToken = () => {
return new Promise((resolve, reject) => {
ipcRenderer.once("auth-token-response", (event, token) => {
return resolve(token);
});
ipcRenderer.send("get-auth-token");
if (lbryio._authToken) {
resolve(lbryio._authToken);
} else {
ipcRenderer.once("auth-token-response", (event, token) => {
lbryio._authToken = token;
return resolve(token);
});
ipcRenderer.send("get-auth-token");
}
});
};
lbryio.setAuthToken = token => {
lbryio._authToken = token ? token.toString().trim() : null;
ipcRenderer.send("set-auth-token", token);
};