Skip to content

Commit

Permalink
#55 Rerequest avatar when it is no longer on the file system.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutchie committed Apr 23, 2019
1 parent 2917ad6 commit 8607dc0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/avatarManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ export class AvatarManager {
}
if (this.avatars[email].image !== null) {
// Avatar image is available
this.sendAvatarToWebView(email);
this.sendAvatarToWebView(email, () => {
// Avatar couldn't be found, request it again
this.removeAvatarFromCache(email);
this.queue.add(email, repo, commits, true);
});
}
} else {
// Avatar not in the cache, request it
Expand All @@ -62,6 +66,11 @@ export class AvatarManager {
this.view = null;
}

public removeAvatarFromCache(email: string) {
delete this.avatars[email];
this.extensionState.removeAvatarFromCache(email);
}

public clearCache() {
this.avatars = {};
this.extensionState.clearAvatarCache();
Expand Down Expand Up @@ -261,14 +270,16 @@ export class AvatarManager {
this.avatars[email] = { image: image, timestamp: (new Date()).getTime(), identicon: identicon };
}
this.extensionState.saveAvatar(email, this.avatars[email]);
this.sendAvatarToWebView(email);
this.sendAvatarToWebView(email, () => { });
}

private sendAvatarToWebView(email: string) {
private sendAvatarToWebView(email: string, onError: () => void) {
if (this.view !== null) {
fs.readFile(this.avatarStorageFolder + '/' + this.avatars[email].image, (err, data) => {
// Read the avatar, and send it as a base64 encoded data uri
if (!err && this.view !== null) {
if (err) {
onError();
} else if (this.view !== null) {
// Send avatar to the webview as a base64 encoded data uri
this.view.sendMessage({
command: 'fetchAvatar',
email: email,
Expand Down
5 changes: 5 additions & 0 deletions src/extensionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export class ExtensionState {
avatars[email] = avatar;
this.globalState.update(AVATAR_CACHE, avatars);
}
public removeAvatarFromCache(email: string) {
let avatars = this.getAvatarCache();
delete avatars[email];
this.globalState.update(AVATAR_CACHE, avatars);
}
public clearAvatarCache() {
this.globalState.update(AVATAR_CACHE, {});
fs.readdir(this.globalStoragePath + AVATAR_STORAGE_FOLDER, (err, files) => {
Expand Down

0 comments on commit 8607dc0

Please sign in to comment.