Skip to content

Commit 78cd693

Browse files
committed
refactor: ♻️ 重构 core 模块
1 parent 1404351 commit 78cd693

52 files changed

Lines changed: 157 additions & 142 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.oxlintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
"../**/*",
157157
"!.*/**/*"
158158
],
159-
"message": "只能通过 main 导入"
159+
"message": "只能通过 core 导入"
160160
}
161161
]
162162
}

eslint.config.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export default antfu(
8383
'prefer-promise-reject-errors': 'off',
8484

8585
'no-debugger': 'warn',
86+
87+
'prefer-const': 'off',
8688
},
8789
},
8890
i18next.configs['flat/recommended'],
@@ -109,11 +111,6 @@ export default antfu(
109111
},
110112
prettierConflicts,
111113
...oxlint.buildFromOxlintConfigFile('./.oxlintrc.json'),
112-
)
113-
.disableRulesFix(['prefer-const'], {
114-
builtinRules: () =>
115-
import('eslint/use-at-your-own-risk').then((r) => r.builtinRules),
116-
})
117114
// 一些规则即使在上面 false 了,实际也还是会启用,需要在这里禁用
118115
// oxc 有的全部禁用交给 oxc
119-
.removePlugins('node', 'jsdoc', 'vue', 'test', 'import', 'ts', 'unicorn');
116+
).removePlugins('node', 'jsdoc', 'vue', 'test', 'import', 'ts', 'unicorn');

rollup.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const packlist = [
7979
'components/Toast',
8080
'userscript/copyApi',
8181
'userscript/detectAd',
82-
'userscript/main',
82+
'userscript/core',
8383
'worker/detectAd',
8484
'worker/ImageRecognition',
8585
'worker/ImageUpscale',
@@ -102,7 +102,7 @@ const baseOptions = {
102102
external: [
103103
...Object.keys(meta.resource ?? {}),
104104
...packlist,
105-
'main',
105+
'core',
106106
/^solid/,
107107
],
108108
input: '',
@@ -248,8 +248,8 @@ const optionList: RollupOptions[] = [
248248
/\s+\/\/ import list/,
249249
packlist
250250
.map((path) => {
251-
if (path === 'userscript/main')
252-
return `\ncase 'main':\ncode = \`inject('${path}')\`;\nbreak;`;
251+
if (path === 'userscript/core')
252+
return `\ncase 'core':\ncode = \`inject('${path}')\`;\nbreak;`;
253253
return `\ncase '${path}':\ncode = \`inject('${path}')\`;\nbreak;`;
254254
})
255255
.join(''),

src/components/Manga/store/prop.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Promisable } from 'type-fest';
2+
13
import type { ToolbarButtonList } from '../defaultButtonList';
24
import type { SettingList } from '../defaultSettingList';
35
import type { ComicImg } from './image';
@@ -13,25 +15,25 @@ type PropState = {
1315
/** 点击结束页按钮时触发的回调 */
1416
onExit?: (isEnd?: boolean) => void;
1517
/** 点击上一话按钮时触发的回调 */
16-
onPrev?: () => void | Promise<void>;
18+
onPrev?: () => Promisable<void>;
1719
/** 点击下一话按钮时触发的回调 */
18-
onNext?: () => void | Promise<void>;
20+
onNext?: () => Promisable<void>;
1921

2022
/** 图片加载状态发生变化时触发的回调 */
21-
onLoading?: (imgList: ComicImg[], img?: ComicImg) => void | Promise<void>;
23+
onLoading?: (imgList: ComicImg[], img?: ComicImg) => Promisable<void>;
2224
/** 图片加载失败时触发的回调 */
23-
onImgError?: (url: string) => void | Promise<void>;
25+
onImgError?: (url: string) => Promisable<void>;
2426
/** 配置发生变化时触发的回调 */
25-
onOptionChange?: (option: Partial<Option>) => void | Promise<void>;
27+
onOptionChange?: (option: Partial<Option>) => Promisable<void>;
2628
/** 快捷键配置发生变化时触发的回调 */
2729
onHotkeysChange?: (
2830
hotkeys: Record<string, string[]>,
29-
) => void | Promise<void>;
31+
) => Promisable<void>;
3032
/** 显示图片发生变化时触发的回调 */
3133
onShowImgsChange?: (
3234
showImgs: Set<number>,
3335
imgList: ComicImg[],
34-
) => void | Promise<void>;
36+
) => Promisable<void>;
3537
/** 每次加载范围改变后触发的回调,返回加载范围中等待 url 的图片的 index */
3638
onWaitUrlImgs?: (indexs: Set<number>, imgList: ComicImg[]) => void;
3739

src/helper/other.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ScheduleCallback } from '@solid-primitives/scheduled';
2+
import type { Promisable } from 'type-fest';
23

34
import {
45
debounce as _debounce,
@@ -189,7 +190,7 @@ export const singleThreaded = <T extends any[]>(
189190
callback: (
190191
state: SingleThreadedState<T>,
191192
...args: T
192-
) => void | undefined | Promise<void | undefined>,
193+
) => Promisable<void | undefined>,
193194
initState?: Partial<SingleThreadedState<T>>,
194195
) => {
195196
const state: SingleThreadedState<T> = {
@@ -231,7 +232,7 @@ export const singleThreaded = <T extends any[]>(
231232
* @returns 所有 Promise 的返回值
232233
*/
233234
export const plimit = async <T>(
234-
fnList: (() => Promise<T> | T)[],
235+
fnList: (() => Promisable<T>)[],
235236
callBack = undefined as
236237
| ((doneNum: number, totalNum: number, resList: T[], i: number) => void)
237238
| undefined,
@@ -337,16 +338,16 @@ export const needDarkMode = (hexColor: string) => {
337338
* @param waitTime - 轮询间隔时间(毫秒),默认为 100
338339
*/
339340
export async function wait<T>(
340-
fn: () => T | undefined | Promise<T | undefined>,
341+
fn: () => Promisable<T | undefined>,
341342
): Promise<TrueValue<T>>;
342343
export async function wait<T>(
343-
fn: () => T | undefined | Promise<T | undefined>,
344+
fn: () => Promisable<T>,
344345
timeout?: number,
345346
waitTime?: number,
346347
): Promise<T>;
347348
// oxlint-disable-next-line func-style
348349
export async function wait<T>(
349-
fn: () => T | undefined | Promise<T | undefined>,
350+
fn: () => Promisable<T>,
350351
timeout = Number.POSITIVE_INFINITY,
351352
waitTime = 100,
352353
) {
@@ -583,7 +584,7 @@ export const hijackFn = <T extends unknown[] = unknown[], R = unknown>(
583584

584585
export const getGmValue = async <T extends string | number | object = string>(
585586
name: string,
586-
setValueFn: () => unknown | Promise<unknown>,
587+
setValueFn: () => Promisable<unknown>,
587588
) => {
588589
const value = await GM.getValue<T>(name);
589590
if (value !== undefined) return value;

src/helper/solidJs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import {
1414

1515
import { createScheduled, isEqual, throttle } from './other';
1616

17-
export { ReactiveSet } from '@solid-primitives/set';
1817
export { ReactiveMap } from '@solid-primitives/map';
18+
export { ReactiveSet } from '@solid-primitives/set';
1919

2020
let publicOwner: Owner;
2121
createRoot(() => {

src/helper/useCache.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Promisable } from 'type-fest';
2+
13
export type UseStore = <T>(
24
txMode: IDBTransactionMode,
35
callback: (store: IDBObjectStore) => T | PromiseLike<T>,
@@ -75,7 +77,7 @@ export const useCache = async <Schema extends Record<string, unknown>>(
7577
callback: (
7678
value: Schema[K],
7779
cursor: IDBCursorWithValue,
78-
) => void | Promise<void>,
80+
) => Promisable<void>,
7981
) {
8082
const request = db
8183
.transaction(storeName, 'readwrite')

src/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { listenHotkey, type MangaProps } from 'components/Manga';
2+
import { request, setup, type SetupOptions, toast } from 'core';
23
import {
34
fileType,
45
isUrl,
56
log,
6-
plimit,
77
querySelector,
88
querySelectorAll,
99
querySelectorClick,
@@ -14,10 +14,8 @@ import {
1414
t,
1515
useStyle,
1616
wait,
17-
waitDom,
1817
} from 'helper';
1918
import { getInitLang } from 'helper/languages';
20-
import { request, toast, setup, type SetupOptions } from 'main';
2119
import { getImglistByHtml } from 'userscript/copyApi';
2220
import { otherSite } from 'userscript/otherSite';
2321

@@ -173,7 +171,7 @@ try {
173171
: comicData.chapters.find((chapter) =>
174172
chapter.data.find((data) => data.chapter_id === chapterId),
175173
)!
176-
).data.sort((a, b) => a.chapter_order - b.chapter_order);
174+
).data.toSorted((a, b) => a.chapter_order - b.chapter_order);
177175
const chapterIndex = chapter.findIndex(
178176
({ chapter_id }) => chapter_id === chapterId,
179177
);
@@ -465,7 +463,8 @@ try {
465463

466464
// by: https://sleazyfork.org/zh-CN/scripts/374903-comicread/discussions/241035
467465
const getImgList = () =>
468-
[...(unsafeWindow.xx as string).matchAll(/(?<= s=").+?(?=")/g)].map(
466+
Array.from(
467+
(unsafeWindow.xx as string).matchAll(/(?<= s=").+?(?=")/g),
469468
([text]) => decodeURIComponent(text),
470469
);
471470

@@ -845,7 +844,7 @@ try {
845844
setup({
846845
name: 'mangadex',
847846
isMangaPage: () => {
848-
const match = /^\/chapter\/([^\/]+)/.exec(location.pathname);
847+
const match = /^\/chapter\/([^/]+)/.exec(location.pathname);
849848
if (match) return { id: match[1] };
850849
},
851850
async getImgList() {

src/pwa/src/DownloadButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createSignal } from 'solid-js';
55

66
import { toast } from 'components/Toast';
77
import { boolDataVal, isUrl, t, wait } from 'helper';
8-
import { request } from 'userscript/main';
8+
import { request } from 'userscript/core';
99

1010
import { loadNewImglist } from './store';
1111

src/pwa/src/fileParser/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Promisable } from 'type-fest';
2+
13
import type { ImgFile } from '../store';
24

35
import { isSupportFile } from './helper';
@@ -6,7 +8,7 @@ import { unzip } from './unzip';
68

79
export * from './helper';
810

9-
export const getImgData = (file: File): Promise<ImgFile[]> | ImgFile[] => {
11+
export const getImgData = (file: File): Promisable<ImgFile[]> => {
1012
const fileType = isSupportFile(file.name);
1113
switch (fileType) {
1214
case null:

0 commit comments

Comments
 (0)