-
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
a7ec856
016829d
2926090
f221096
f36a7e3
8cc87fa
4ff0e9e
826e1e2
82fbaf4
02160b0
d7cf10f
c4d297c
91a37ff
f1a32a7
d428a5c
5c45ebb
d814b48
73628cc
d3465fa
3a4f569
ce3ea89
13b3412
29de6d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,3 +93,4 @@ flake.lock | |
| .agents/ | ||
| .claude/ | ||
| .jj/ | ||
| .sisyphus/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,123 @@ | ||
| // Mock for react-native-mmkv (v3 uses NitroModules under the hood) | ||
| const strings = new Map(); | ||
| const numbers = new Map(); | ||
| const booleans = new Map(); | ||
| const buffers = new Map(); | ||
|
|
||
| class MMKV { | ||
| set(key, value) { | ||
| if (typeof value === 'string') { | ||
| strings.set(key, value); | ||
| numbers.delete(key); | ||
| booleans.delete(key); | ||
| buffers.delete(key); | ||
| return; | ||
| } | ||
|
|
||
| if (typeof value === 'number') { | ||
| numbers.set(key, value); | ||
| strings.delete(key); | ||
| booleans.delete(key); | ||
| buffers.delete(key); | ||
| return; | ||
| } | ||
|
|
||
| if (typeof value === 'boolean') { | ||
| booleans.set(key, value); | ||
| strings.delete(key); | ||
| numbers.delete(key); | ||
| buffers.delete(key); | ||
| return; | ||
| } | ||
|
|
||
| buffers.set(key, value); | ||
| strings.delete(key); | ||
| numbers.delete(key); | ||
| booleans.delete(key); | ||
| } | ||
|
|
||
| getString(key) { | ||
| return strings.get(key); | ||
| } | ||
|
|
||
| getNumber(key) { | ||
| return numbers.get(key); | ||
| } | ||
|
|
||
| getBoolean(key) { | ||
| return booleans.get(key); | ||
| } | ||
|
|
||
| getBuffer(key) { | ||
| return buffers.get(key); | ||
| } | ||
|
|
||
| contains(key) { | ||
| return ( | ||
| strings.has(key) || | ||
| numbers.has(key) || | ||
| booleans.has(key) || | ||
| buffers.has(key) | ||
| ); | ||
| } | ||
|
|
||
| delete(key) { | ||
| strings.delete(key); | ||
| numbers.delete(key); | ||
| booleans.delete(key); | ||
| buffers.delete(key); | ||
| } | ||
|
|
||
| clearAll() { | ||
| strings.clear(); | ||
| numbers.clear(); | ||
| booleans.clear(); | ||
| buffers.clear(); | ||
| } | ||
| } | ||
|
|
||
| const createTupleHook = getter => (key, fallback) => | ||
| [ | ||
| getter(key) ?? fallback, | ||
| jest.fn(value => { | ||
| if (value === undefined) { | ||
| strings.delete(key); | ||
| numbers.delete(key); | ||
| booleans.delete(key); | ||
| buffers.delete(key); | ||
| return; | ||
| } | ||
|
|
||
| if (typeof value === 'string') { | ||
| strings.set(key, value); | ||
| } else if (typeof value === 'number') { | ||
| numbers.set(key, value); | ||
| } else if (typeof value === 'boolean') { | ||
| booleans.set(key, value); | ||
| } else { | ||
| buffers.set(key, value); | ||
| } | ||
| }), | ||
| ]; | ||
|
|
||
| module.exports = { | ||
| MMKV, | ||
| useMMKVString: createTupleHook(key => strings.get(key)), | ||
| useMMKVNumber: createTupleHook(key => numbers.get(key)), | ||
| useMMKVBoolean: createTupleHook(key => booleans.get(key)), | ||
| useMMKVObject: createTupleHook(key => { | ||
| const json = strings.get(key); | ||
| if (!json) { | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| return JSON.parse(json); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| }), | ||
| NitroModules: { | ||
| createHybridObject: jest.fn(() => { | ||
| // Return a mock object that won't be used since MMKV has its own mock | ||
| return {}; | ||
| }), | ||
| createHybridObject: jest.fn(() => ({})), | ||
| }, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,7 +114,8 @@ | |
| "react-native-worklets": "^0.7.4", | ||
| "react-native-zip-archive": "^7.0.2", | ||
| "sanitize-html": "^2.17.1", | ||
| "urlencode": "^2.0.0" | ||
| "urlencode": "^2.0.0", | ||
| "zustand": "^5.0.12" | ||
|
Comment on lines
+117
to
+118
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🌐 Web query:
💡 Result: The latest version of the zustand npm package is 5.0.12. Citations:
🌐 Web query:
💡 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 🤖 Prompt for AI Agents |
||
| }, | ||
| "devDependencies": { | ||
| "@babel/core": "^7.29.0", | ||
|
|
||
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, | ||
| ); | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -82,15 +82,10 @@ export const getAllNovels = async (): Promise<NovelInfo[]> => { | |
| return dbManager.select().from(novelSchema).all(); | ||
| }; | ||
|
|
||
| export const getNovelById = async ( | ||
| novelId: number, | ||
| ): Promise<NovelInfo | undefined> => { | ||
| const res = dbManager | ||
| .select() | ||
| .from(novelSchema) | ||
| .where(eq(novelSchema.id, novelId)) | ||
| .get(); | ||
| return res; | ||
| export const getNovelById = (novelId: number): NovelInfo | undefined => { | ||
| return dbManager.getSync( | ||
| dbManager.select().from(novelSchema).where(eq(novelSchema.id, novelId)), | ||
| ); | ||
| }; | ||
|
Comment on lines
+85
to
89
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. Don't silently flip this shared query to sync.
🤖 Prompt for AI Agents |
||
|
|
||
| export const getNovelByPath = ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,3 +29,99 @@ jest.mock('@hooks/persisted/useTrackedNovel'); | |||||||||||||||||||
| jest.mock('@hooks/persisted/useUpdates'); | ||||||||||||||||||||
| jest.mock('@services/plugin/fetch'); | ||||||||||||||||||||
| jest.mock('@components/Context/LibraryContext'); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const createMockChapterTextCache = () => { | ||||||||||||||||||||
| const cache = new Map<number, string | Promise<string>>(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return { | ||||||||||||||||||||
| read: jest.fn((chapterId: number) => cache.get(chapterId)), | ||||||||||||||||||||
| write: jest.fn((chapterId: number, value: string | Promise<string>) => { | ||||||||||||||||||||
| cache.set(chapterId, value); | ||||||||||||||||||||
| }), | ||||||||||||||||||||
| remove: jest.fn((chapterId: number) => cache.delete(chapterId)), | ||||||||||||||||||||
| clear: jest.fn(() => cache.clear()), | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export const createMockNovelStoreState = ( | ||||||||||||||||||||
| overrides: Record<string, unknown> = {}, | ||||||||||||||||||||
| ) => ({ | ||||||||||||||||||||
| loading: false, | ||||||||||||||||||||
| fetching: false, | ||||||||||||||||||||
| pageIndex: 0, | ||||||||||||||||||||
| pages: ['1'], | ||||||||||||||||||||
| novel: undefined, | ||||||||||||||||||||
| chapters: [], | ||||||||||||||||||||
| firstUnreadChapter: undefined, | ||||||||||||||||||||
| batchInformation: { | ||||||||||||||||||||
| batch: 0, | ||||||||||||||||||||
| total: 0, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
|
Comment on lines
+56
to
+59
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. Include The new bootstrap/store flow populates Possible fix batchInformation: {
batch: 0,
total: 0,
+ totalChapters: 0,
},📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| novelSettings: { | ||||||||||||||||||||
| filter: [], | ||||||||||||||||||||
| showChapterTitles: true, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| chapterTextCache: createMockChapterTextCache(), | ||||||||||||||||||||
| lastRead: undefined, | ||||||||||||||||||||
|
|
||||||||||||||||||||
| bootstrapNovel: jest.fn().mockResolvedValue(true), | ||||||||||||||||||||
| getChapters: jest.fn().mockResolvedValue(undefined), | ||||||||||||||||||||
| getNextChapterBatch: jest.fn().mockResolvedValue(undefined), | ||||||||||||||||||||
| loadUpToBatch: jest.fn().mockResolvedValue(undefined), | ||||||||||||||||||||
| refreshNovel: jest.fn().mockResolvedValue(undefined), | ||||||||||||||||||||
|
|
||||||||||||||||||||
| setNovel: jest.fn(), | ||||||||||||||||||||
| setPages: jest.fn(), | ||||||||||||||||||||
| setPageIndex: jest.fn(), | ||||||||||||||||||||
| openPage: jest.fn().mockResolvedValue(undefined), | ||||||||||||||||||||
| setNovelSettings: jest.fn(), | ||||||||||||||||||||
| setLastRead: jest.fn(), | ||||||||||||||||||||
| followNovel: jest.fn(), | ||||||||||||||||||||
|
|
||||||||||||||||||||
| updateChapter: jest.fn(), | ||||||||||||||||||||
| setChapters: jest.fn(), | ||||||||||||||||||||
| extendChapters: jest.fn(), | ||||||||||||||||||||
|
|
||||||||||||||||||||
| bookmarkChapters: jest.fn(), | ||||||||||||||||||||
| markPreviouschaptersRead: jest.fn(), | ||||||||||||||||||||
| markChapterRead: jest.fn(), | ||||||||||||||||||||
| markChaptersRead: jest.fn(), | ||||||||||||||||||||
| markPreviousChaptersUnread: jest.fn(), | ||||||||||||||||||||
| markChaptersUnread: jest.fn(), | ||||||||||||||||||||
| updateChapterProgress: jest.fn(), | ||||||||||||||||||||
| deleteChapter: jest.fn(), | ||||||||||||||||||||
| deleteChapters: jest.fn(), | ||||||||||||||||||||
| refreshChapters: jest.fn(), | ||||||||||||||||||||
| ...overrides, | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export const createMockNovelStore = ( | ||||||||||||||||||||
| stateOverrides: Record<string, unknown> = {}, | ||||||||||||||||||||
| ) => { | ||||||||||||||||||||
| let state = createMockNovelStoreState(stateOverrides); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return { | ||||||||||||||||||||
| getState: jest.fn(() => state), | ||||||||||||||||||||
| setState: jest.fn(nextState => { | ||||||||||||||||||||
| const partial = | ||||||||||||||||||||
| typeof nextState === 'function' ? nextState(state) : nextState; | ||||||||||||||||||||
| state = { | ||||||||||||||||||||
| ...state, | ||||||||||||||||||||
| ...partial, | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| }), | ||||||||||||||||||||
| subscribe: jest.fn(() => () => {}), | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const defaultMockNovelContext = { | ||||||||||||||||||||
| novelStore: createMockNovelStore(), | ||||||||||||||||||||
| navigationBarHeight: 0, | ||||||||||||||||||||
| statusBarHeight: 0, | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export const mockUseNovelContext = jest.fn(() => defaultMockNovelContext); | ||||||||||||||||||||
|
Comment on lines
+98
to
+123
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. Make the mock store obey the external-store contract.
Possible fix export const createMockNovelStore = (
stateOverrides: Record<string, unknown> = {},
) => {
let state = createMockNovelStoreState(stateOverrides);
+ const listeners = new Set<() => void>();
return {
getState: jest.fn(() => state),
setState: jest.fn(nextState => {
const partial =
typeof nextState === 'function' ? nextState(state) : nextState;
state = {
...state,
...partial,
};
+ listeners.forEach(listener => listener());
}),
- subscribe: jest.fn(() => () => {}),
+ subscribe: jest.fn(listener => {
+ listeners.add(listener);
+ return () => listeners.delete(listener);
+ }),
};
};
-const defaultMockNovelContext = {
- novelStore: createMockNovelStore(),
- navigationBarHeight: 0,
- statusBarHeight: 0,
-};
-
-export const mockUseNovelContext = jest.fn(() => defaultMockNovelContext);
+const createDefaultMockNovelContext = () => ({
+ novelStore: createMockNovelStore(),
+ navigationBarHeight: 0,
+ statusBarHeight: 0,
+});
+
+export const mockUseNovelContext = jest.fn(() =>
+ createDefaultMockNovelContext(),
+);🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| jest.mock('@screens/novel/NovelContext', () => ({ | ||||||||||||||||||||
| useNovelContext: () => mockUseNovelContext(), | ||||||||||||||||||||
| })); | ||||||||||||||||||||
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.
Hook setters are recreated on each call, losing mock call history.
createTupleHookreturns a newjest.fn()on every invocation. This means:If tests need to assert on setter calls or rely on referential stability, consider caching the setter per key.
🛡️ Optional fix: Cache setters per key
Remember to clear
setterCachein abeforeEachif needed.📝 Committable suggestion
🤖 Prompt for AI Agents