From 80c09af9eb6c7cd0109a478b0380335f55f35f9c Mon Sep 17 00:00:00 2001 From: frostime Date: Mon, 9 Sep 2024 21:50:39 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(resv):=20=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=89=93=E5=BC=80=E6=97=A5=E8=AE=B0=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE=E6=8F=92=E5=85=A5=E4=BB=8A?= =?UTF-8?q?=E5=A4=A9=E9=A2=84=E7=BA=A6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 这种问题往往是由于多段同步造成的 - 将原本基于内存缓存的方案,重构成基于 SQL 查询获取今日的预约;修复这个问题 TODO:之后的版本中完全去掉 reservation 缓存,全部基于 SQL 查询 --- package.json | 2 +- pnpm-lock.yaml | 58 +++++++++++++++++++++++---------------- src/func/index.ts | 15 ++++++---- src/func/reserve/index.ts | 7 +++-- src/global-status.ts | 22 +++++++++------ src/utils.ts | 27 +++++++----------- vite.config.ts | 2 +- 7 files changed, 75 insertions(+), 58 deletions(-) diff --git a/package.json b/package.json index e417758..e401920 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c20d0f0..ae9e32e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,7 +14,7 @@ importers: devDependencies: '@sveltejs/vite-plugin-svelte': specifier: ^3.1.1 - version: 3.1.1(svelte@4.2.18)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)) + version: 3.1.1(svelte@4.2.19)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)) '@tsconfig/svelte': specifier: ^5.0.4 version: 5.0.4 @@ -46,8 +46,8 @@ importers: specifier: 1.0.2 version: 1.0.2 svelte: - specifier: ^4.2.18 - version: 4.2.18 + specifier: ^4.2.19 + version: 4.2.19 sy-plugin-changelog: specifier: ^0.0.7 version: 0.0.7 @@ -407,8 +407,9 @@ packages: aria-query@5.3.0: resolution: {integrity: sha512-b0P0sZPKtyu8HkeRAfCq0IfURZK+SuwMjY1UXGBU27wpAiTwQAIlq56IbIO+ytk/JjS1fMR14ee5WBBfKi5J6A==} - axobject-query@4.0.0: - resolution: {integrity: sha512-+60uv1hiVFhHZeO+Lz0RYzsVHy5Wr1ayX0mwda9KPDVLNJgZ1T9Ny7VmFbLDzxsH0D87I86vgj3gFrjTJUYznw==} + axobject-query@4.1.0: + resolution: {integrity: sha512-qIj0G9wZbMGNLjLmg1PT6v2mE9AH2zlnADJD/2tC6E00hgmhUOfEB6greHPAfLRSufHqROIUTkw6E+M3lH0PTQ==} + engines: {node: '>= 0.4'} balanced-match@1.0.2: resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==} @@ -590,6 +591,9 @@ packages: magic-string@0.30.10: resolution: {integrity: sha512-iIRwTIf0QKV3UAnYK4PU8uiEc4SRh5jX0mwpIwETPpHdhVM4f53RSwS/vXvN1JhGX+Cs7B8qIq3d6AH49O5fAQ==} + magic-string@0.30.11: + resolution: {integrity: sha512-+Wri9p0QHMy+545hKww7YAu5NyzF8iomPL/RQazugQ9+Ez4Ic3mERMd8ZTX5rfK944j+560ZJi8iAwgak1Ac7A==} + make-error@1.3.6: resolution: {integrity: sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==} @@ -704,6 +708,10 @@ packages: resolution: {integrity: sha512-itJW8lvSA0TXEphiRoawsCksnlf8SyvmFzIhltqAHluXd88pkCd+cXJVHTDwdCr0IzwptSm035IHQktUu1QUMg==} engines: {node: '>=0.10.0'} + source-map-js@1.2.1: + resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==} + engines: {node: '>=0.10.0'} + string_decoder@1.1.1: resolution: {integrity: sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==} @@ -713,8 +721,8 @@ packages: peerDependencies: svelte: ^3.19.0 || ^4.0.0 - svelte@4.2.18: - resolution: {integrity: sha512-d0FdzYIiAePqRJEb90WlJDkjUEx42xhivxN8muUBmfZnP+tzUgz12DJ2hRJi8sIHCME7jeK1PTMgKPSfTd8JrA==} + svelte@4.2.19: + resolution: {integrity: sha512-IY1rnGr6izd10B0A8LqsBfmlT5OILVuZ7XsI0vdGPEvuonFV7NYEUK4dAkm9Zg2q0Um92kYjTpS1CAP3Nh/KWw==} engines: {node: '>=16'} sy-plugin-changelog@0.0.7: @@ -992,24 +1000,24 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.18.0': optional: true - '@sveltejs/vite-plugin-svelte-inspector@2.1.0(@sveltejs/vite-plugin-svelte@3.1.1(svelte@4.2.18)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)))(svelte@4.2.18)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4))': + '@sveltejs/vite-plugin-svelte-inspector@2.1.0(@sveltejs/vite-plugin-svelte@3.1.1(svelte@4.2.19)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)))(svelte@4.2.19)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4))': dependencies: - '@sveltejs/vite-plugin-svelte': 3.1.1(svelte@4.2.18)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)) + '@sveltejs/vite-plugin-svelte': 3.1.1(svelte@4.2.19)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)) debug: 4.3.5 - svelte: 4.2.18 + svelte: 4.2.19 vite: 5.2.12(@types/node@20.13.0)(sass@1.77.4) transitivePeerDependencies: - supports-color - '@sveltejs/vite-plugin-svelte@3.1.1(svelte@4.2.18)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4))': + '@sveltejs/vite-plugin-svelte@3.1.1(svelte@4.2.19)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4))': dependencies: - '@sveltejs/vite-plugin-svelte-inspector': 2.1.0(@sveltejs/vite-plugin-svelte@3.1.1(svelte@4.2.18)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)))(svelte@4.2.18)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)) + '@sveltejs/vite-plugin-svelte-inspector': 2.1.0(@sveltejs/vite-plugin-svelte@3.1.1(svelte@4.2.19)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)))(svelte@4.2.19)(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)) debug: 4.3.5 deepmerge: 4.3.1 kleur: 4.1.5 magic-string: 0.30.10 - svelte: 4.2.18 - svelte-hmr: 0.16.0(svelte@4.2.18) + svelte: 4.2.19 + svelte-hmr: 0.16.0(svelte@4.2.19) vite: 5.2.12(@types/node@20.13.0)(sass@1.77.4) vitefu: 0.2.5(vite@5.2.12(@types/node@20.13.0)(sass@1.77.4)) transitivePeerDependencies: @@ -1050,9 +1058,7 @@ snapshots: dependencies: dequal: 2.0.3 - axobject-query@4.0.0: - dependencies: - dequal: 2.0.3 + axobject-query@4.1.0: {} balanced-match@1.0.2: {} @@ -1116,7 +1122,7 @@ snapshots: css-tree@2.3.1: dependencies: mdn-data: 2.0.30 - source-map-js: 1.2.0 + source-map-js: 1.2.1 dayjs@1.11.8: {} @@ -1276,6 +1282,10 @@ snapshots: dependencies: '@jridgewell/sourcemap-codec': 1.4.15 + magic-string@0.30.11: + dependencies: + '@jridgewell/sourcemap-codec': 1.5.0 + make-error@1.3.6: {} mdn-data@2.0.30: {} @@ -1398,15 +1408,17 @@ snapshots: source-map-js@1.2.0: {} + source-map-js@1.2.1: {} + string_decoder@1.1.1: dependencies: safe-buffer: 5.1.2 - svelte-hmr@0.16.0(svelte@4.2.18): + svelte-hmr@0.16.0(svelte@4.2.19): dependencies: - svelte: 4.2.18 + svelte: 4.2.19 - svelte@4.2.18: + svelte@4.2.19: dependencies: '@ampproject/remapping': 2.3.0 '@jridgewell/sourcemap-codec': 1.5.0 @@ -1414,13 +1426,13 @@ snapshots: '@types/estree': 1.0.5 acorn: 8.12.1 aria-query: 5.3.0 - axobject-query: 4.0.0 + axobject-query: 4.1.0 code-red: 1.0.4 css-tree: 2.3.1 estree-walker: 3.0.3 is-reference: 3.0.2 locate-character: 3.0.0 - magic-string: 0.30.10 + magic-string: 0.30.11 periscopic: 3.1.0 sy-plugin-changelog@0.0.7: {} diff --git a/src/func/index.ts b/src/func/index.ts index 1cf53b9..ea0edf9 100644 --- a/src/func/index.ts +++ b/src/func/index.ts @@ -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 **********/ @@ -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); diff --git a/src/func/reserve/index.ts b/src/func/reserve/index.ts index 79ffabe..c895e3c 100644 --- a/src/func/reserve/index.ts +++ b/src/func/reserve/index.ts @@ -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'; @@ -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; } diff --git a/src/global-status.ts b/src/global-status.ts index 907d6fb..f97c5d4 100644 --- a/src/global-status.ts +++ b/src/global-status.ts @@ -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'; @@ -128,6 +133,7 @@ const ReserveFile = 'Reservation.json'; class ReservationManger { plugin: DailyNoteTodayPlugin; reserved: Map; + //@deprecated 后期把内置的缓存删掉,完全基于块属性来 reservations: { "OnDate": { [date: string]: string[] } } = { "OnDate": {} }; constructor() { @@ -246,15 +252,15 @@ class ReservationManger { this.reserved.set(blockId, date_str); } - isTodayReserved(): boolean { - return this.getTodayReservations().length > 0; + async isTodayReserved(): Promise { + 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 { + let reservations: Reservation[] = await retrieveResvFromBlocks('today'); + return reservations.map(r => r.id); } //清理已经过期的预约 diff --git a/src/utils.ts b/src/utils.ts index 53a89f2..1cd81f5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 或达到最大次数 @@ -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; } diff --git a/vite.config.ts b/vite.config.ts index 81bee7d..74d2c3c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -110,7 +110,7 @@ export default defineConfig({ emptyOutDir: false, // 构建后是否生成 source map 文件 - sourcemap: false, + sourcemap: isWatch ? 'inline' : false, // 设置为 false 可以禁用最小化混淆 // 或是用来指定是应用哪种混淆器