Commit e8fc6fda authored by timel's avatar timel

feat: async to sync

parent ca83bae4
...@@ -50,8 +50,8 @@ export interface CardText { ...@@ -50,8 +50,8 @@ export interface CardText {
* @returns 卡片数据 * @returns 卡片数据
* *
* */ * */
export async function fetchCard(id: number): Promise<CardMeta> { export function fetchCard(id: number): CardMeta {
const res = await sqliteMiddleWare({ const res = sqliteMiddleWare({
cmd: sqliteCmd.SELECT, cmd: sqliteCmd.SELECT,
payload: { id }, payload: { id },
}); });
...@@ -65,8 +65,8 @@ export async function fetchCard(id: number): Promise<CardMeta> { ...@@ -65,8 +65,8 @@ export async function fetchCard(id: number): Promise<CardMeta> {
* @returns 卡片数据 * @returns 卡片数据
* *
* */ * */
export async function searchCards(params: FtsParams): Promise<CardMeta[]> { export function searchCards(params: FtsParams): CardMeta[] {
const res = await sqliteMiddleWare({ const res = sqliteMiddleWare({
cmd: sqliteCmd.FTS, cmd: sqliteCmd.FTS,
payload: { ftsParams: params }, payload: { ftsParams: params },
}); });
......
...@@ -24,8 +24,8 @@ export enum sqliteCmd { ...@@ -24,8 +24,8 @@ export enum sqliteCmd {
FTS, FTS,
} }
export interface sqliteAction { export interface sqliteAction<T extends sqliteCmd> {
cmd: sqliteCmd; cmd: T;
// 初始化DB需要业务方传入的数据 // 初始化DB需要业务方传入的数据
initInfo?: { initInfo?: {
dbUrl: string; dbUrl: string;
...@@ -47,8 +47,14 @@ const sqlPromise = initSqlJs({ ...@@ -47,8 +47,14 @@ const sqlPromise = initSqlJs({
locateFile: (file) => `${NeosConfig.assetsPath}/${file}`, locateFile: (file) => `${NeosConfig.assetsPath}/${file}`,
}); });
export default function <T extends sqliteCmd>(
action: sqliteAction<T>,
): T extends sqliteCmd.INIT ? Promise<void> : sqliteResult {
return helper(action) as any;
}
// FIXME: 应该有个返回值,告诉业务方本次请求的结果,比如初始化DB失败 // FIXME: 应该有个返回值,告诉业务方本次请求的结果,比如初始化DB失败
export default async function (action: sqliteAction): Promise<sqliteResult> { function helper<T extends sqliteCmd>(action: sqliteAction<T>) {
switch (action.cmd) { switch (action.cmd) {
case sqliteCmd.INIT: { case sqliteCmd.INIT: {
const info = action.initInfo; const info = action.initInfo;
...@@ -56,16 +62,14 @@ export default async function (action: sqliteAction): Promise<sqliteResult> { ...@@ -56,16 +62,14 @@ export default async function (action: sqliteAction): Promise<sqliteResult> {
const dataPromise = pfetch(info.dbUrl, { const dataPromise = pfetch(info.dbUrl, {
progressCallback: action.initInfo?.progressCallback, progressCallback: action.initInfo?.progressCallback,
}).then((res) => res.arrayBuffer()); // TODO: i18n }).then((res) => res.arrayBuffer()); // TODO: i18n
return Promise.all([sqlPromise, dataPromise]).then(([SQL, buffer]) => {
const [SQL, buffer] = await Promise.all([sqlPromise, dataPromise]); YGODB = new SQL.Database(new Uint8Array(buffer));
YGODB = new SQL.Database(new Uint8Array(buffer)); console.log("YGODB inited!");
});
console.info("YGODB inited!");
} else { } else {
console.warn("init YGODB action without initInfo"); console.warn("init YGODB action without initInfo");
return {};
} }
return {};
} }
case sqliteCmd.SELECT: { case sqliteCmd.SELECT: {
if (YGODB && action.payload && action.payload.id) { if (YGODB && action.payload && action.payload.id) {
......
...@@ -23,7 +23,7 @@ export const CardDetail: React.FC<{ ...@@ -23,7 +23,7 @@ export const CardDetail: React.FC<{
}> = ({ code, open, onClose }) => { }> = ({ code, open, onClose }) => {
const [card, setCard] = useState<CardMeta>(); const [card, setCard] = useState<CardMeta>();
useEffect(() => { useEffect(() => {
fetchCard(code).then(setCard); setCard(fetchCard(code));
}, [code]); }, [code]);
const cardType = useMemo( const cardType = useMemo(
() => () =>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment