Commit 6431e477 authored by antoor's avatar antoor

Reconstruction of optimized code

重构优化代码
parent c03f6d26
//
// 缓存管理模块
//
/**
* 缓存管理模块
* 更新:2016/04/28
* 作者:蚁逅 <https://github.com/antoor>
*/
'use strict';
const fs = require('fs');
const path = require('path');
const log4js = require('log4js');
const Datastore = require('nedb');
const logger = log4js.getLogger('Cache');
const fs = require('fs'),
path = require('path'),
CONF = require('./config'),
logger = require('log4js').getLogger('Cache'),
Datastore = require('nedb');
class Cache {
/**
* 初始化监听事件
* @param {Object} electron electron对象
* @return {[type]} [description]
*/
constructor(electron) {
// 创建数据库
// 获取用户保存目录(mac&&*unix=/home/path/,win=c:/path/appdata
let dbPath = '';
if (process.env.HOME) {
dbPath = path.join(process.env.HOME, '.antSword');
}else if (process.env.LOCALAPPPATH) {
dbPath = path.join(process.env.LOCALAPPPATH, '.antSword');
}else{
dbPath = 'database';
};
// 创建数据目录
if (!fs.existsSync(dbPath)) {
fs.mkdirSync(dbPath);
};
// 创建缓存目录
const cachePath = path.join(dbPath, 'cache');
if (!fs.existsSync(cachePath)) {
fs.mkdirSync(cachePath);
};
this.dbPath = dbPath;
this.cachePath = cachePath;
// 监听数据请求
this.listenHandle(electron.ipcMain);
}
listenHandle(ipcMain) {
logger.info('listenHandle');
ipcMain
// 添加缓存
// arg={id="shellID",tag="存储标识",cache="存储内容"}
.on('cache-add', (event, arg) => {
logger.debug('cache-add', arg);
this.createDB(arg['id']).insert({
tag: arg['tag'],
cache: arg['cache']
}, (err, ret) => {
event.returnValue = err || ret;
});
})
// 更新缓存
// arg = {id, tag, cache}
.on('cache-set', (event, arg) => {
logger.debug('cache-set', arg);
this.createDB(arg['id']).update({
tag: arg['tag']
}, {
$set: {
cache: arg['cache']
}
}, (err, ret) => {
event.returnValue = err || ret;
});
})
// 查询缓存
// arg={id="shellID", tag="存储标识"}
.on('cache-get', (event, arg) => {
logger.debug('cache-get', arg);
this.createDB(arg['id']).findOne({
tag: arg['tag']
}, (err, ret) => {
event.returnValue = err || ret;
})
})
// 删除缓存
// arg = {id: 'SHELL-ID', tag: 'SAVE-TAG'}
.on('cache-del', (event, arg) => {
logger.warn('cache-del', arg);
this.createDB(arg['id']).remove({
tag: arg['tag']
}, (err, ret) => {
event.returnValue = err || ret;
})
})
// 清空缓存
// arg = {id: 'SHELL-ID'}
.on('cache-clear', (event, arg) => {
logger.fatal('cache-clear', arg);
try{
fs.unlinkSync(path.join(this.cachePath, arg['id']));
event.returnValue = true;
}catch(e) {
event.returnValue = e;
}
})
// 清空所有缓存
.on('cache-clearAll', (event, arg) => {
logger.fatal('cache-clearAll', arg);
try{
fs.readdirSync(this.cachePath).map((_) => {
fs.unlinkSync(path.join(this.cachePath, _));
});
event.returnValue = true;
}catch(e) {
event.returnValue = e;
}
})
electron.ipcMain
.on('cache-add', this.addCache.bind(this))
.on('cache-set', this.setCache.bind(this))
.on('cache-get', this.getCache.bind(this))
.on('cache-del', this.delCache.bind(this))
.on('cache-clear', this.clearCache.bind(this))
.on('cache-clearAll', this.clearAllCache.bind(this));
}
createDB(id) {
// 创建数据库
/**
* 创建nedb数据库文件
* @param {String} id 数据存储文件名
* @return {[type]} [description]
*/
createDB(id = String(+new Date)) {
return new Datastore({
filename: path.join(this.cachePath, id),
filename: path.join(CONF.cachePath, id),
autoload: true
});
}
/**
* 添加缓存数据
* @param {Object} event ipcMain对象
* @param {Object} opts 缓存配置(id,tag,cache
*/
addCache(event, opts) {
logger.debug('addCache', opts);
this.createDB(opts['id']).insert({
tag: opts['tag'],
cache: opts['cache']
}, (err, ret) => {
event.returnValue = err || ret;
});
}
/**
* 设置缓存数据
* @param {Object} event ipcMain对象
* @param {Object} opts 缓存配置(id,tag,cache
*/
setCache(event, opts) {
logger.debug('setCache', opts);
this.createDB(opts['id']).update({
tag: opts['tag']
}, {
$set: {
cache: opts['cache']
}
}, (err, ret) => {
event.returnValue = err || ret;
});
}
/**
* 获取缓存数据
* @param {Object} event ipcMain对象
* @param {Object} opts 缓存配置(id,tag)
* @return {[type]} [description]
*/
getCache(event, opts) {
logger.debug('getCache', opts);
this.createDB(opts['id']).findOne({
tag: opts['tag']
}, (err, ret) => {
event.returnValue = err || ret;
})
}
/**
* 删除缓存
* @param {Object} event ipcMain对象
* @param {Object} opts 缓存配置(id,tag)
* @return {[type]} [description]
*/
delCache(event, opts) {
logger.warn('delCache', opts);
this.createDB(opts['id']).remove({
tag: opts['tag']
}, (err, ret) => {
event.returnValue = err || ret;
});
}
/**
* 清空缓存数据
* @param {Object} event ipcMain对象
* @param {Object} opts 缓存配置(id)
* @return {[type]} [description]
*/
clearCache(event, opts) {
logger.fatal('clearCache', opts);
try{
fs.unlinkSync(path.join(CONF.cachePath, opts['id']));
event.returnValue = true;
}catch(e) {
event.returnValue = e;
}
}
/**
* 清空所有缓存数据
* @param {Object} event ipcMain对象
* @param {Object} opts 缓存配置(null)
* @return {[type]} [description]
*/
clearAllCache(event, opts) {
logger.fatal('clearAllCache', opts);
try{
fs.readdirSync(CONF.cachePath).map((_) => {
fs.unlinkSync(path.join(CONF.cachePath, _));
});
event.returnValue = true;
}catch(e) {
event.returnValue = e;
}
}
}
module.exports = Cache;
\ No newline at end of file
module.exports = Cache;
/**
* 中国蚁剑::后端配置模块
* ? 用于进行一些通用的变量如初始化目录等设置
* 开写:2016/04/26
* 更新:2016/04/28
* 作者:蚁逅 <https://github.com/antoor>
*/
'use strict';
const fs = require('fs'),
path = require('path');
class Conf {
constructor() {
// 获取数据存储目录
this.basePath = path.join(
process.env.HOME || process.env.LOCALAPPPATH || process.cwd() || '.',
'.antSword'
);
// 创建.antSword目录
!fs.existsSync(this.basePath) ? fs.mkdirSync(this.basePath) : null;
}
/**
* 获取数据存储路径
* @return {String} file-path
*/
get dataPath() {
return path.join(this.basePath, 'shell.db');
}
/**
* 获取缓存目录
* @return {String} dir-path
*/
get cachePath() {
let _ = path.join(this.basePath, '/cache/');
// 创建缓存目录
!fs.existsSync(_) ? fs.mkdirSync(_) : null;
return _;
}
}
module.exports = new Conf();
This diff is collapsed.
......@@ -4,20 +4,18 @@
'use strict';
// 读取package.json信息
const info = require('../package');
class Menubar {
constructor(electron, app, mainWindow) {
const Menu = electron.Menu;
const ipcMain = electron.ipcMain;
// 清空菜单栏
Menu.setApplicationMenu(Menu.buildFromTemplate([]));
// 监听重载菜单事件
ipcMain.on('menubar', this.reload.bind(this));
ipcMain.on('quit', app.quit.bind(app));
electron.ipcMain
.on('quit', app.quit.bind(app))
.on('menubar', this.reload.bind(this));
this.electron = electron;
this.app = app;
......@@ -25,7 +23,12 @@ class Menubar {
this.mainWindow = mainWindow;
}
// 刷新菜单
/**
* 重新载入菜单
* @param {Object} event ipcMain对象
* @param {Object} LANG 语言模板
* @return {[type]} [description]
*/
reload(event, LANG) {
// 菜单模板
const template = [
......@@ -36,9 +39,7 @@ class Menubar {
{
label: LANG['shell']['add'],
accelerator: 'Shift+A',
click: () => {
event.sender.send('menubar', 'shell-add');
}
click: event.sender.send.bind(event.sender, 'menubar', 'shell-add')
}, {
label: LANG['shell']['search'],
accelerator: 'Shift+S',
......@@ -99,7 +100,7 @@ class Menubar {
}
];
// 调试菜单
if (info['debug']) {
if (process.env['npm_package_debug']) {
template.push({
label: LANG['debug']['title'],
submenu: [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment