代码拉取完成,页面将自动刷新
const grunt = require("grunt");
class File {
constructor(name) {
grunt.file.defaultEncoding = "utf8";
this.name = name;
return this;
}
read() {
return grunt.file.exists(this.name) ? grunt.file.readJSON(this.name) : null;
}
write(content, ops) {
grunt.file.write(this.name, content, ops || {});
return this;
}
delete() {
if (grunt.file.exists(this.name)) grunt.file.delete(this.name);
return this;
}
}
const file = new File("./src/data/store.json");
let storeData = {};
class Store {
constructor() {
this.state = {};
let _state = file.read();
if (_state) {
this.state = { ...this.state, ..._state };
file.write(JSON.stringify(this.state));
}
return this;
}
createStore(ops) {
this.state = { ...this.state, ...(ops.state || {}) };
file.write(JSON.stringify(this.state));
storeData._actions = ops.actions;
storeData._mutations = ops.mutations;
storeData._getters = ops.getters;
return this;
}
setState(name, value) {
this.state[name] = value;
file.write(JSON.stringify(this.state));
return this.state[name];
}
query(filter, target) {
target = target || this.state;
if (!filter) return;
let filterResult = [];
for (let n in target) {
if (typeof filter === "function") {
let res = filter(target[n]);
if (res) {
filterResult.push(target[n]);
}
} else if (target[n] === filter) {
filterResult.push(target[n]);
}
}
return filterResult;
}
destroy() {
this.state = {};
file.delete();
}
computed() {
let args = arguments,
that = this;
return storeData._getters[args[0]](that.state, that);
}
dispatch() {
let args = arguments,
that = this;
return new Promise((resolve, reject) => {
if (args[0]) {
storeData._actions[args[0]](that, args[1])
.then((res) => resolve(res))
.catch((err) => reject(err));
} else {
reject();
}
});
}
commit() {
let args = arguments,
that = this;
return new Promise((resolve, reject) => {
if (args[0]) {
storeData._mutations[args[0]](that, args[1]);
resolve();
} else {
reject();
}
});
}
}
module.exports = Store;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。