feat: 优化

This commit is contained in:
范胜发
2022-03-11 17:22:33 +08:00
parent 9bae8badbd
commit acf83137e1
24 changed files with 453 additions and 193 deletions

29
store/index.js Normal file
View File

@ -0,0 +1,29 @@
const { ipcMain } = require('electron');
const { TodoStore } = require('./store');
const store = new TodoStore();
module.exports.install = function install() {
ipcMain.handle('get-todo', (e) => {
return store.getTodo();
});
ipcMain.handle('set-todo', (e, id, value) => {
store.setTodo(id, value);
return store.getTodo();
});
ipcMain.handle('add-todo', (e, value) => {
store.addTodo(value);
return store.getTodo();
});
ipcMain.handle('del-todo', (e, id) => {
store.delTodo(id);
return store.getTodo();
});
ipcMain.handle('clear-todo', (e, id, status) => {
store.setClear(id, status);
return store.getTodo();
});
};

102
store/store.js Normal file
View File

@ -0,0 +1,102 @@
const Store = require('electron-store');
const store = new Store();
const { isDef, isDate } = require('../utils/def');
class Todo {
constructor({ id, title, time, isClear, clearTime }) {
this.id = id;
this.title = title;
this.time = isDate(time) ? new Date(time).getTime() : Date.now();
this.isClear = isDef(isClear) ? isClear : false;
this.clearTime = isDate(clearTime) ? new Date(clearTime).getTime() : 0;
}
}
class TodoStore {
constructor() {
this.init();
}
init() {
if (!store.has('todo')) {
store.set('todo', []);
}
}
getTodo() {
return store.get('todo');
}
setTodo(id, value) {
const list = this.getTodo();
const i = list.findIndex((d) => d.id === id);
if (i > -1) {
list[i] = new Todo({
...value,
id,
});
store.set('todo', list);
return true;
} else {
return false;
}
}
addTodo(value) {
const list = this.getTodo();
const last = list[list.length - 1] || {};
const id = last.id ? `${last.id + 1}` : '1';
const newData = new Todo({
id,
...value,
});
list.push(newData);
store.set('todo', list);
return newData;
}
delTodo(id) {
const list = this.getTodo();
const i = list.findIndex((d) => d.id === id);
if (i > -1) {
list.splice(i, 1);
store.set('todo', list);
return true;
} else {
return false;
}
}
clearAll() {
store.set('todo', []);
return true;
}
setClear(id, status) {
const list = this.getTodo();
const i = list.findIndex((d) => d.id === id);
if (i > -1) {
list[i].isClear = isDef(status) ? status : !list[i].isClear;
if (list[i].isClear) {
list[i].clearTime = Date.now();
} else {
list[i].clearTime = 0;
}
store.set('todo', list);
return true;
} else {
return false;
}
}
}
module.exports.TodoStore = TodoStore;