Skip to content

Commit

Permalink
cache nativeImage instance to reduce ram use trazyn#36
Browse files Browse the repository at this point in the history
  • Loading branch information
trazyn committed Sep 19, 2017
1 parent b271239 commit 73a3244
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
54 changes: 36 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,38 +258,48 @@ let mainMenu = [
}
];
let avatarPath = tmp.dirSync();
let avatarMap = {};
let avatarCache = {};
let avatarPlaceholder = `${__dirname}/src/assets/images/user-fallback.png`;

async function getIcon(cookies, userid, src) {
var icon = src || avatarPlaceholder;
var cached = avatarCache[userid];
var icon;

icon = avatarMap[userid];
if (cached) {
return cached;
}

if (!icon && cookies && src) {
if (cookies && src) {
try {
let response = await axios({
url: src,
method: 'get',
responseType: 'stream',
responseType: 'arraybuffer',
headers: {
Cookie: cookies,
Host: 'wx.qq.com',
Referer: 'https://wx.qq.com/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8',
},
});
// eslint-disable-next-line
let base64 = new Buffer(response.data, 'binary').toString('base64');

icon = `${avatarPath.name}/${userid}.jpg`;
response.data.pipe(fs.createWriteStream(icon));
fs.writeFileSync(icon, base64.replace(/^data:image\/png;base64,/, ''), 'base64');
} catch (ex) {
console.error(ex);
icon = avatarPlaceholder;
}
}

avatarMap[userid] = icon;
return nativeImage.createFromPath(icon).resize({ width: 24, height: 24 });
var image = nativeImage.createFromPath(icon);

image = image.resize({ width: 24, height: 24 });

avatarCache[userid] = image;

return image;
}

function checkForUpdates() {
Expand Down Expand Up @@ -526,22 +536,26 @@ const createMainWindow = () => {
var contactsMenu = mainMenu.find(e => e.label === 'Contacts');
var shouldUpdate = false;

conversations = JSON.parse(conversations);
contacts = JSON.parse(contacts);
if (!isOsx) {
return;
}

if (conversations.length) {
if (conversations.length
&& conversations.map(e => e.name).join() !== conversationsMenu.submenu.map(e => e.label).join()) {
shouldUpdate = true;

conversations = await Promise.all(
conversations.map(async(e, index) => {
let icon = await getIcon(cookies, e.id, e.avatar);

return {
label: e.RemarkName || e.NickName,
label: e.name,
accelerator: `Cmd+${index}`,
icon: await getIcon(cookies, e.UserName, e.HeadImgUrl),
icon,
click() {
mainWindow.show();
mainWindow.webContents.send('message-chatto', {
id: e.UserName,
id: e.id,
});
}
};
Expand All @@ -555,13 +569,15 @@ const createMainWindow = () => {

contacts = await Promise.all(
contacts.map(async e => {
let icon = await getIcon(cookies, e.id, e.avatar);

return {
label: e.RemarkName || e.NickName,
icon: await getIcon(cookies, e.UserName, e.HeadImgUrl),
label: e.name,
icon,
click() {
mainWindow.show();
mainWindow.webContents.send('show-userinfo', {
id: e.UserName,
id: e.id,
});
}
};
Expand All @@ -570,7 +586,9 @@ const createMainWindow = () => {
contactsMenu.submenu = contacts;
}

shouldUpdate && createMenu();
if (shouldUpdate) {
createMenu();
}
});

ipcMain.on('message-unread', (event, args) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wewechat",
"version": "1.1.2",
"version": "1.1.3",
"description": "Unofficial WeChat client built with React, MobX and Electron.",
"main": "main.js",
"scripts": {
Expand Down
14 changes: 11 additions & 3 deletions src/js/stores/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,16 @@ function hasUnreadMessage(messages) {

async function updateMenus({ conversations = [], contacts = [] }) {
ipcRenderer.send('menu-update', {
conversations: JSON.stringify(conversations),
contacts: JSON.stringify(contacts),
conversations: conversations.map(e => ({
id: e.UserName,
name: e.RemarkName || e.NickName,
avatar: e.HeadImgUrl,
})),
contacts: contacts.map(e => ({
id: e.UserName,
name: e.RemarkName || e.NickName,
avatar: e.HeadImgUrl,
})),
cookies: await helper.getCookie(),
});
}
Expand Down Expand Up @@ -382,6 +390,7 @@ class Chat {
data: [message],
unread: 0,
};
self.messages.set(from, list);
}

if (self.user.UserName === from) {
Expand All @@ -402,7 +411,6 @@ class Chat {
});

self.sessions.replace([...stickyed, ...normaled]);
self.messages.set(from, list);

hasUnreadMessage(self.messages);
updateMenus({
Expand Down
1 change: 0 additions & 1 deletion src/js/stores/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class Contacts {
self.memberList = response.data.MemberList.filter(e => helper.isContact(e) && !helper.isOfficial(e) && !helper.isBrand(e)).concat(me);
self.memberList.map(e => {
e.MemberList = [];

return self.resolveUser(auth, e);
});

Expand Down

0 comments on commit 73a3244

Please sign in to comment.