提交 48734dd4 authored 作者: vipcxj's avatar vipcxj

增加操作历史记录相关api

上级 d8819186
/* eslint-disable no-param-reassign */
/** /**
* Created by yaohx_169 on 2017/6/8. * Created by yaohx_169 on 2017/6/8.
*/ */
import { cookie } from './config'; import { cookie } from './config';
import { getCookie, setCookie, delCookie } from './helper'; import { getCookie, setCookie, delCookie } from './helper';
import db from './db';
export async function getToken() { export async function getToken() {
return getCookie(cookie.token); return getCookie(cookie.token);
...@@ -57,3 +59,93 @@ export async function isAuthed() { ...@@ -57,3 +59,93 @@ export async function isAuthed() {
export async function hasDomain() { export async function hasDomain() {
return getDomain().then(result => !!result); return getDomain().then(result => !!result);
} }
const normHistory = (history) => {
if (!history) {
history = {};
}
if (!history.size) {
history.size = 10;
}
if (history.size < 1) {
history.size = 1;
}
if (!history.data) {
history.data = [];
history.start = 0;
history.top = 0;
history.empty = true;
}
return history;
};
const next = (i, size) => {
if (i < 0 || i >= size) {
throw new Error(`out of range: ${i} in ${size}`);
}
if (i + 1 >= size) {
return 0;
} else {
return i + 1;
}
};
const prev = (i, size) => {
if (i < 0 || i >= size) {
throw new Error(`out of range: ${i} in ${size}`);
}
if (i - i < 0) {
return size - 1;
} else {
return i - 1;
}
};
export const histories = {
async getLatest(name) {
let history = await db.get(`history.${name}`).value();
history = normHistory(history);
if (history.empty) {
return null;
}
return history.data[prev(history.top, history.size)];
},
async getHistory(name) {
let history = db.get(`history.${name}`).value();
history = normHistory(history);
if (history.empty) {
return [];
} else if (history.top > history.start) {
return history.data.slice(history.start, history.top);
} else {
return [...history.data.slice(history.start, history.size), ...history.data.slice(0, history.top)];
}
},
async pushHistory(name, value) {
let history = await db.get(`history.${name}`).value();
history = normHistory(history);
history.data[history.top] = value;
const nextPos = next(history.top, history.size);
if (!history.empty && history.start === history.top) {
history.top = history.start = nextPos;
} else {
history.top = nextPos;
}
if (history.empty) {
history.empty = false;
}
return db.set(`history.${name}`, history).write();
},
async popHistory(name) {
let history = await db.get(`history.${name}`).value();
history = normHistory(history);
if (history.empty) {
return;
}
history.top = prev(history.top, history.size);
if (history.top === history.start) {
history.empty = true;
}
return db.set(`history.${name}`, history).write();
},
};
...@@ -46,6 +46,7 @@ const config = { ...@@ -46,6 +46,7 @@ const config = {
contextPath: _contextPath, contextPath: _contextPath,
apiContextPath: _apiContextPath, apiContextPath: _apiContextPath,
productId: 'big-machine-web-front', productId: 'big-machine-web-front',
fastNavigationPage: '',
defaultDateFormat, defaultDateFormat,
defaultTimeFormat, defaultTimeFormat,
defaultDateTimeFormat, defaultDateTimeFormat,
......
/** @module utils/db */
import low from 'lowdb'; import low from 'lowdb';
import LocalStorage from 'lowdb/adapters/LocalStorage'; import LocalStorage from 'lowdb/adapters/LocalStorage';
const adapter = new LocalStorage('db'); const adapter = new LocalStorage('db');
export default low(adapter);
/**
* @typedef {Object} DB
* @template {T}
*/
/**
* @member {Function} DB~get
* @param {Array.<string>|string} path
* @param {*} [defaultValue]
* @return {DB.<*>}
*/
/**
* @member {Function} DB~set
* @template {T}
* @param {Array.<string>|string} path
* @param {T} value
* @return {DB.<T>}
*/
/**
* @member {Function} DB~find
* @template {T}
* @param {Function} [predicate=_.identity]
* @param {number} [fromIndex=0]
* @return {DB.<T>}
*/
/**
* @member {Function} DB~unset
* @param {Array.<string>|string} path
* @return {DB.<boolean>}
*/
/**
* @member {Function} DB~read
* @return {Promise.<*>}
*/
/**
* @member {Function} DB~write
* @return {Promise.<*>}
*/
/**
* @member {Function} DB~value
* @template {T}
* @return {Promise.<T>}
*/
/**
* @type {DB.<*>}
*/
const db = low(adapter);
export default db;
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论