-
Notifications
You must be signed in to change notification settings - Fork 0
Use novel refactor #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c8f5bcd
a3c7b57
6da8ca0
9b41dc3
4ce7f22
0dde694
b2b1a26
4820a2a
9538c06
05f4ae3
a3c4d1b
e1315de
55fff01
a6bf396
1e0c847
3830b61
6a34fe9
626cd54
0548be9
8eb37ec
354c638
db25722
e9015ad
1ecd3f3
2a6e43b
5af4698
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,9 +71,9 @@ class DbManager implements IDbManager { | |
| >; | ||
| } | ||
|
|
||
| public async allSync<T extends ExecutableSelect>( | ||
| public allSync<T extends ExecutableSelect>( | ||
| query: T, | ||
| ): Promise<Awaited<ReturnType<T['all']>>> { | ||
| ): Awaited<ReturnType<T['all']>> { | ||
| const { sql: sqlString, params } = query.toSQL(); | ||
| return db.executeSync(sqlString, params as any[]).rows as Awaited< | ||
| ReturnType<T['all']> | ||
|
|
@@ -135,6 +135,40 @@ type FireOn = Array<{ table: TableNames; ids?: number[] }>; | |
| export function useLiveQuery<T extends ExecutableSelect>( | ||
| query: T, | ||
| fireOn: FireOn, | ||
| callback?: (data: Awaited<ReturnType<T['all']>>) => void, | ||
| ) { | ||
| type ReturnValue = Awaited<ReturnType<T['all']>>; | ||
|
|
||
| const { sql: sqlString, params } = query.toSQL(); | ||
| const paramsKey = JSON.stringify(params); | ||
| const fireOnKey = JSON.stringify(fireOn); | ||
|
|
||
| const [data, setData] = useState<ReturnValue>(() => { | ||
| const r = db.executeSync(sqlString, params as any[]).rows as ReturnValue; | ||
| if (callback) callback(r); | ||
| return r; | ||
|
Comment on lines
+146
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, find and read the file
wc -l src/database/manager/manager.tsRepository: CD-Z/lnreader Length of output: 92 🏁 Script executed: # Read the file around the mentioned line ranges
cat -n src/database/manager/manager.ts | sed -n '140,200p'Repository: CD-Z/lnreader Length of output: 2251 🏁 Script executed: cat -n src/database/manager/manager.ts | sed -n '135,170p'Repository: CD-Z/lnreader Length of output: 1346 🏁 Script executed: cat -n src/database/manager/manager.ts | sed -n '168,202p'Repository: CD-Z/lnreader Length of output: 1293 Make callback delivery lifecycle-safe. The callback paths have two React-specific correctness issues: the initial calls are triggered from render (useState initializers on lines 146-149 and 179-185), and later reactive updates keep stale callback closures because Minimal pattern-import { useEffect, useState } from 'react';
+import { useEffect, useRef, useState } from 'react';
export function useLiveQuery<T extends ExecutableSelect>(
query: T,
fireOn: FireOn,
callback?: (data: Awaited<ReturnType<T['all']>>) => void,
) {
type ReturnValue = Awaited<ReturnType<T['all']>>;
+ const callbackRef = useRef(callback);
+
+ useEffect(() => {
+ callbackRef.current = callback;
+ }, [callback]);
- const [data, setData] = useState<ReturnValue>(() => {
- const r = db.executeSync(sqlString, params as any[]).rows as ReturnValue;
- if (callback) callback(r);
- return r;
- });
+ const [data, setData] = useState<ReturnValue>(
+ () => db.executeSync(sqlString, params as any[]).rows as ReturnValue,
+ );
+
+ useEffect(() => {
+ callbackRef.current?.(data);
+ }, []);
useEffect(() => {
const unsub = db.reactiveExecute({
query: sqlString,
arguments: params as any[],
fireOn,
callback: (result: { rows: ReturnValue }) => {
setData(result.rows);
- if (callback) callback(result.rows);
+ callbackRef.current?.(result.rows);
},
});Apply the same 🤖 Prompt for AI Agents |
||
| }); | ||
|
|
||
| useEffect(() => { | ||
| const unsub = db.reactiveExecute({ | ||
| query: sqlString, | ||
| arguments: params as any[], | ||
| fireOn, | ||
| callback: (result: { rows: ReturnValue }) => { | ||
| setData(result.rows); | ||
| if (callback) callback(result.rows); | ||
| }, | ||
| }); | ||
| return unsub; | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [sqlString, paramsKey, fireOnKey]); | ||
|
|
||
| return data; | ||
| } | ||
| export function useLiveQueryAsync<T extends ExecutableSelect>( | ||
| query: T, | ||
| fireOn: FireOn, | ||
| callback?: (data: Awaited<ReturnType<T['all']>>) => void, | ||
| ) { | ||
| type ReturnValue = Awaited<ReturnType<T['all']>>; | ||
|
|
||
|
|
@@ -143,7 +177,11 @@ export function useLiveQuery<T extends ExecutableSelect>( | |
| const fireOnKey = JSON.stringify(fireOn); | ||
|
|
||
| const [data, setData] = useState<ReturnValue>( | ||
| () => db.executeSync(sqlString, params as any[]).rows as ReturnValue, | ||
| () => | ||
| db.execute(sqlString, params as any[]).then(result => { | ||
| callback?.(result.rows as ReturnValue); | ||
| return result.rows; | ||
| }) as ReturnValue, | ||
| ); | ||
|
Comment on lines
179
to
185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: The 🐛 Suggested fix: Use null/undefined initial state and load asynchronously- const [data, setData] = useState<ReturnValue>(
- () =>
- db.execute(sqlString, params as any[]).then(result => {
- callback?.(result.rows as ReturnValue);
- return result.rows;
- }) as ReturnValue,
- );
+ const [data, setData] = useState<ReturnValue | null>(null);
+
+ useEffect(() => {
+ let cancelled = false;
+ db.execute(sqlString, params as any[]).then(result => {
+ if (!cancelled) {
+ setData(result.rows as ReturnValue);
+ callback?.(result.rows as ReturnValue);
+ }
+ });
+ return () => { cancelled = true; };
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [sqlString, paramsKey]);🤖 Prompt for AI Agents |
||
|
|
||
| useEffect(() => { | ||
|
|
@@ -153,6 +191,7 @@ export function useLiveQuery<T extends ExecutableSelect>( | |
| fireOn, | ||
| callback: (result: { rows: ReturnValue }) => { | ||
| setData(result.rows); | ||
| if (callback) callback(result.rows); | ||
| }, | ||
| }); | ||
| return unsub; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -300,23 +300,61 @@ export const clearUpdates = async (): Promise<void> => { | |
| // #endregion | ||
| // #region Selectors | ||
|
|
||
| export const getCustomPages = async (novelId: number) => { | ||
| return await dbManager | ||
| .selectDistinct({ page: chapterSchema.page }) | ||
| .from(chapterSchema) | ||
| .where(eq(chapterSchema.novelId, novelId)) | ||
| .orderBy(asc(castInt(chapterSchema.page))) | ||
| .all(); | ||
| export const getCustomPages = (novelId: number) => { | ||
| return dbManager.allSync( | ||
| dbManager | ||
| .selectDistinct({ page: chapterSchema.page }) | ||
| .from(chapterSchema) | ||
| .where(eq(chapterSchema.novelId, novelId)) | ||
| .orderBy(asc(castInt(chapterSchema.page))), | ||
| ); | ||
| }; | ||
|
Comment on lines
+303
to
311
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sync change may cause test inconsistency. The function now returns synchronously, but the test file (per context snippet at 🤖 Prompt for AI Agents |
||
|
|
||
| export const getNovelChapters = async ( | ||
| novelId: number, | ||
| sort?: ChapterOrderKey, | ||
| filter?: ChapterFilterKey[], | ||
| page?: string, | ||
| limit: number = 1000, | ||
| ): Promise<ChapterInfo[]> => | ||
| dbManager | ||
| .select() | ||
| .from(chapterSchema) | ||
| .where(eq(chapterSchema.novelId, novelId)); | ||
| .where( | ||
| and( | ||
| eq(chapterSchema.novelId, novelId), | ||
| !page ? sql.raw('true') : eq(chapterSchema.page, page), | ||
| chapterFilterToSQL(filter), | ||
| ), | ||
| ) | ||
| .orderBy(chapterOrderToSQL(sort)) | ||
| .limit(limit) | ||
| .all(); | ||
|
|
||
| export const getNovelChaptersSync = ( | ||
| novelId: number, | ||
| sort?: ChapterOrderKey, | ||
| filter?: ChapterFilterKey[], | ||
| page?: string, | ||
| limit: number = 1000, | ||
| ): ChapterInfo[] => | ||
| dbManager.allSync( | ||
| dbManager | ||
| .select() | ||
| .from(chapterSchema) | ||
| .where( | ||
| and( | ||
| eq(chapterSchema.novelId, novelId), | ||
| !page ? sql.raw('true') : eq(chapterSchema.page, page), | ||
| chapterFilterToSQL(filter), | ||
| ), | ||
| ) | ||
| .orderBy(chapterOrderToSQL(sort)) | ||
| .limit(limit), // Adding a limit to prevent potential performance issues with large datasets | ||
| ); | ||
| /** | ||
| * @deprecated, use getNovelChapters with whereConditions instead | ||
| */ | ||
| export const getUnreadNovelChapters = async ( | ||
| novelId: number, | ||
| ): Promise<ChapterInfo[]> => | ||
|
|
@@ -326,7 +364,9 @@ export const getUnreadNovelChapters = async ( | |
| .where( | ||
| and(eq(chapterSchema.novelId, novelId), eq(chapterSchema.unread, true)), | ||
| ); | ||
|
|
||
| /** | ||
| * @deprecated, use getNovelChapters with whereConditions instead | ||
| */ | ||
| export const getAllUndownloadedChapters = async ( | ||
| novelId: number, | ||
| ): Promise<ChapterInfo[]> => | ||
|
|
@@ -339,7 +379,9 @@ export const getAllUndownloadedChapters = async ( | |
| eq(chapterSchema.isDownloaded, false), | ||
| ), | ||
| ); | ||
|
|
||
| /** | ||
| * @deprecated, use getNovelChapters with whereConditions instead | ||
| */ | ||
| export const getAllUndownloadedAndUnreadChapters = async ( | ||
| novelId: number, | ||
| ): Promise<ChapterInfo[]> => | ||
|
|
@@ -408,8 +450,8 @@ export const getPageChaptersBatched = async ( | |
| page?: string, | ||
| batch: number = 0, | ||
| ) => { | ||
| const limit = 300; | ||
| const offset = 300 * batch; | ||
| const limit = 1000; | ||
| const offset = 1000 * batch; | ||
| const query = dbManager | ||
| .select() | ||
| .from(chapterSchema) | ||
|
|
@@ -451,20 +493,21 @@ export const getFirstUnreadChapter = ( | |
| filter?: ChapterFilterKey[], | ||
| page?: string, | ||
| ) => | ||
| dbManager | ||
| .select() | ||
| .from(chapterSchema) | ||
| .where( | ||
| and( | ||
| eq(chapterSchema.novelId, novelId), | ||
| eq(chapterSchema.page, page || '1'), | ||
| eq(chapterSchema.unread, true), | ||
| chapterFilterToSQL(filter), | ||
| ), | ||
| ) | ||
| .orderBy(asc(chapterSchema.position)) | ||
| .limit(1) | ||
| .get(); | ||
| dbManager.getSync( | ||
| dbManager | ||
| .select() | ||
| .from(chapterSchema) | ||
| .where( | ||
| and( | ||
| eq(chapterSchema.novelId, novelId), | ||
| eq(chapterSchema.page, page || '1'), | ||
| eq(chapterSchema.unread, true), | ||
| chapterFilterToSQL(filter), | ||
| ), | ||
| ) | ||
| .orderBy(asc(chapterSchema.position)) | ||
| .limit(1), | ||
| ); | ||
|
|
||
| export const getNovelChaptersByName = async ( | ||
| novelId: number, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,9 +147,9 @@ describe('NovelQueries', () => { | |
| 'test-plugin', | ||
| ); | ||
|
|
||
| expect(result?.inLibrary).toBe(true); | ||
| const novel = await getNovelById(novelId); | ||
| expect(novel?.inLibrary).toBe(true); | ||
| expect(Boolean(result?.inLibrary)).toBe(true); | ||
| const novel = getNovelById(novelId); | ||
| expect(Boolean(novel?.inLibrary)).toBe(true); | ||
|
Comment on lines
+150
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid These assertions no longer prove that Suggested tightening- expect(Boolean(result?.inLibrary)).toBe(true);
+ expect(result).toBeDefined();
+ expect(result?.inLibrary).toBe(true);
const novel = getNovelById(novelId);
- expect(Boolean(novel?.inLibrary)).toBe(true);
+ expect(novel).toBeDefined();
+ expect(novel?.inLibrary).toBe(true);
- expect(Boolean(result?.inLibrary)).toBe(false);
+ expect(result).toBeDefined();
+ expect(result?.inLibrary).toBe(false);
const novel = getNovelById(novelId);
- expect(Boolean(novel?.inLibrary)).toBe(false);
+ expect(novel).toBeDefined();
+ expect(novel?.inLibrary).toBe(false);
- expect(Boolean(novel1?.inLibrary)).toBe(false);
- expect(Boolean(novel2?.inLibrary)).toBe(false);
+ expect(novel1).toBeDefined();
+ expect(novel2).toBeDefined();
+ expect(novel1?.inLibrary).toBe(false);
+ expect(novel2?.inLibrary).toBe(false);Also applies to: 168-170, 214-215 🤖 Prompt for AI Agents |
||
| }); | ||
|
|
||
| it('should remove novel from library', async () => { | ||
|
|
@@ -165,9 +165,9 @@ describe('NovelQueries', () => { | |
| 'test-plugin', | ||
| ); | ||
|
|
||
| expect(result?.inLibrary).toBe(false); | ||
| const novel = await getNovelById(novelId); | ||
| expect(novel?.inLibrary).toBe(false); | ||
| expect(Boolean(result?.inLibrary)).toBe(false); | ||
| const novel = getNovelById(novelId); | ||
| expect(Boolean(novel?.inLibrary)).toBe(false); | ||
| }); | ||
|
|
||
| it('should assign default category when adding to library', async () => { | ||
|
|
@@ -211,8 +211,8 @@ describe('NovelQueries', () => { | |
|
|
||
| const novel1 = await getNovelById(novelId1); | ||
| const novel2 = await getNovelById(novelId2); | ||
| expect(novel1?.inLibrary).toBe(false); | ||
| expect(novel2?.inLibrary).toBe(false); | ||
| expect(Boolean(novel1?.inLibrary)).toBe(false); | ||
| expect(Boolean(novel2?.inLibrary)).toBe(false); | ||
| }); | ||
|
|
||
| it('should handle empty array', async () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
What is the latest version of zustand npm package?💡 Result:
The latest version of the zustand npm package is 5.0.12.
Citations:
🌐 Web query:
Zustand 5.0.12 React 19 compatibility💡 Result:
Zustand 5.0.12 is not officially compatible with React 19. The peer dependency for React is specified as ">=18.0.0", which does not include React 19. Official documentation and changelogs for v5.0.12 do not mention React 19 support or updates to peer dependencies. Early discussions (e.g., Nov 2024) show peer dependency conflicts with React 19 RCs when using npm strict mode, requiring --force or --legacy-peer-deps. While some tools like DepFixer claim compatibility for earlier 5.x versions and Zustand works in practice due to minimal React API usage, strict compatibility requires peer deps alignment, which is absent. For React 19, follow best practices like using selectors with 'use' prefix and useShallow for destructuring to work with React Compiler.
Citations:
Zustand 5.0.12 is not officially compatible with React 19.
Zustand 5.0.12 is the latest stable version, but its peer dependency specifies
>=18.0.0, explicitly excluding React 19. Using it with React 19 will require--forceor--legacy-peer-depsflags during installation. If this project targets React 19, either upgrade Zustand once React 19 support is released or retain React 18 compatibility.🤖 Prompt for AI Agents