Skip to content

Commit

Permalink
🐛 fix(resv): 解决打开日记的时候无法正确插入今天预约的问题
Browse files Browse the repository at this point in the history
- 这种问题往往是由于多段同步造成的
- 将原本基于内存缓存的方案,重构成基于 SQL 查询获取今日的预约;修复这个问题

TODO:之后的版本中完全去掉 reservation 缓存,全部基于 SQL 查询
  • Loading branch information
frostime committed Sep 9, 2024
1 parent 0953cd9 commit 80c09af
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"rollup-plugin-livereload": "^2.0.5",
"sass": "^1.72.0",
"siyuan": "1.0.2",
"svelte": "^4.2.18",
"svelte": "^4.2.19",
"sy-plugin-changelog": "^0.0.7",
"ts-node": "^10.9.2",
"typescript": "^5.4.2",
Expand Down
58 changes: 35 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions src/func/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,17 @@ export class RoutineEventHandler {
const HighlightResv = settings.get('HighlightResv');
if (!HighlightResv) return;

if (reservation.isTodayReserved()) {
reservation.isTodayReserved().then(yes => {
if (!yes) {
updateStyleSheet('');
return;
}
updateStyleSheet(`
span[data-type="siyuan-dailynote-todaydock_tab"][data-title="${this.plugin.i18n.DockReserve.arial}"] {
background-color: var(--b3-theme-primary-lightest);
}
`);
} else {
updateStyleSheet('');
}
})
}

/********** Duplicate **********/
Expand Down Expand Up @@ -153,11 +155,12 @@ export class RoutineEventHandler {
* 尝试自动插入今天的预约
*/
public async tryAutoInsertResv() {
//如果已经插入过了,就不再插入
//TODO 允许用户配置是否每次打开都尝试更新预约
if (this.flag.hasAutoInsertResv === true) return;

//如果今天没有预约,就不插入
if (!reservation.isTodayReserved()) return;
let hasResv = await reservation.isTodayReserved();
if (!hasResv) return;

let succeed = updateTodayReservation(notebooks.default);

Expand Down
7 changes: 5 additions & 2 deletions src/func/reserve/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { confirm } from 'siyuan';
import { i18n } from "@/utils";
import { DebugKit, i18n } from "@/utils";
import * as serverApi from '@/serverApi';
import { reservation, settings } from '@/global-status';
import { Retrieve, RetvFactory } from './retrieve';
Expand Down Expand Up @@ -29,7 +29,10 @@ export async function updateTodayReservation(notebook: Notebook, refresh: boolea
* @returns
*/
export async function updateDocReservation(docId: string, refresh: boolean = false) {
let resvBlockIds = reservation.getTodayReservations();

DebugKit.info('调用 updateDocReservation', ...arguments);

let resvBlockIds = await reservation.getTodayReservations();
if (resvBlockIds.length == 0) {
return;
}
Expand Down
22 changes: 14 additions & 8 deletions src/global-status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/**
* Copyright (c) 2023 frostime. All rights reserved.
/*
* Copyright (c) 2024 by frostime. All Rights Reserved.
* @Author : frostime
* @Date : 2024-05-21 14:14:08
* @FilePath : /src/global-status.ts
* @LastEditTime : 2024-09-09 21:46:44
* @Description :
*/
import { eventBus } from './event-bus';
import { filterExistsBlocks } from './func';
Expand Down Expand Up @@ -128,6 +133,7 @@ const ReserveFile = 'Reservation.json';
class ReservationManger {
plugin: DailyNoteTodayPlugin;
reserved: Map<string, string>;
//@deprecated 后期把内置的缓存删掉,完全基于块属性来
reservations: { "OnDate": { [date: string]: string[] } } = { "OnDate": {} };

constructor() {
Expand Down Expand Up @@ -246,15 +252,15 @@ class ReservationManger {
this.reserved.set(blockId, date_str);
}

isTodayReserved(): boolean {
return this.getTodayReservations().length > 0;
async isTodayReserved(): Promise<boolean> {
let resv = await this.getTodayReservations();
return resv.length > 0;
}

//获取今天的预约
getTodayReservations(): string[] {
let date = new Date();
let date_str = this.dateTemplate(date);
return this.reservations.OnDate[date_str] || [];
async getTodayReservations(): Promise<string[]> {
let reservations: Reservation[] = await retrieveResvFromBlocks('today');
return reservations.map(r => r.id);
}

//清理已经过期的预约
Expand Down
27 changes: 10 additions & 17 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,15 @@ export const lute = window.Lute!.New();
export type I18N = typeof zh_Hans;
// export type I18N = any;


// export function debug(...msg: any[]): void {
// console.debug(`[DailyNoteToday][DEBUG] ${msg}`);
// }

// export function info(...msg: any[]): void {
// console.log(`[DailyNoteToday][INFO] ${msg}`);
// }

// export function error(...msg: any[]): void {
// console.error(`[DailyNoteToday][ERROR] ${msg}`);
// }

// export function warn(...msg: any[]): void {
// console.warn(`[DailyNoteToday][WARN] ${msg}`);
// }
const _fn = (fn: Function, ...args: any[]) => {
if (process.env.DEV_MODE === 'true') {
fn(...args);
}
}
export const DebugKit = {
debug: (...msg: any[]) => _fn(console.debug, ...['[DN::Debug]', ...msg]),
info: (...msg: any[]) => _fn(console.log, ...['[DN::Info]', ...msg]),
}

/**
* 重复执行一个函数,直到返回值不为 null 或达到最大次数
Expand Down Expand Up @@ -157,7 +150,7 @@ class Debouncer {
Timer: { [key: string]: any } = {};
DefaultTimer: any = null;

getTimer(key?: string) {
getTimer(key?: string) {
return key ? this.Timer[key] : this.DefaultTimer;
}

Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default defineConfig({
emptyOutDir: false,

// 构建后是否生成 source map 文件
sourcemap: false,
sourcemap: isWatch ? 'inline' : false,

// 设置为 false 可以禁用最小化混淆
// 或是用来指定是应用哪种混淆器
Expand Down

0 comments on commit 80c09af

Please sign in to comment.