feat: 更新版本至 0.0.61,重构登录缓存逻辑,添加浏览器缓存支持
This commit is contained in:
@@ -3,6 +3,7 @@ import type { Result, DataOpts } from '@kevisual/query/query';
|
||||
import { LoginCacheStore, CacheStore, User } from './login-cache.ts';
|
||||
import { Cache } from './login-cache.ts';
|
||||
import { BaseLoad } from '@kevisual/load';
|
||||
import { EventEmitter } from 'eventemitter3'
|
||||
export type QueryLoginOpts<T extends Cache = Cache> = {
|
||||
query?: Query;
|
||||
isBrowser?: boolean;
|
||||
@@ -26,9 +27,11 @@ export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
|
||||
*/
|
||||
cacheStore: CacheStore<T>;
|
||||
isBrowser: boolean;
|
||||
load?: boolean;
|
||||
storage: Storage;
|
||||
load: boolean = false;
|
||||
status: 'init' | 'logining' | 'loginSuccess' | 'loginError' = 'init';
|
||||
onLoad?: () => void;
|
||||
emitter = new EventEmitter();
|
||||
|
||||
constructor(opts?: QueryLoginOpts<T>) {
|
||||
super({
|
||||
@@ -42,14 +45,29 @@ export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
|
||||
if (!this.storage) {
|
||||
throw new Error('storage is required');
|
||||
}
|
||||
this.cacheStore.init().then(() => {
|
||||
this.onLoad?.();
|
||||
this.load = true;
|
||||
this.emitter.emit('load');
|
||||
});
|
||||
}
|
||||
setQuery(query: Query) {
|
||||
this.query = query;
|
||||
}
|
||||
private async init() {
|
||||
await this.cacheStore.init();
|
||||
this.load = true;
|
||||
this.onLoad?.();
|
||||
async init() {
|
||||
if (this.load) {
|
||||
return this.cacheStore.cacheData;
|
||||
}
|
||||
return new Promise(async (resolve) => {
|
||||
const timer = setTimeout(() => {
|
||||
resolve(this.cacheStore.cacheData);
|
||||
}, 1000 * 20); // 20秒超时,避免一直等待
|
||||
const listener = () => {
|
||||
clearTimeout(timer);
|
||||
resolve(this.cacheStore.cacheData);
|
||||
}
|
||||
this.emitter.once('load', listener);
|
||||
});
|
||||
}
|
||||
async post<T = any>(data: any, opts?: DataOpts) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user