Skip to content

Commit

Permalink
perf: ⚡️ transform优化图片加载时机
Browse files Browse the repository at this point in the history
日志加上transform-eagle前缀
  • Loading branch information
meetqy committed Apr 18, 2023
1 parent 9e80750 commit ec26dfe
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/transform-eagle/lib/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const WAIT_TIME = 3000;
8 changes: 3 additions & 5 deletions packages/transform-eagle/lib/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import TagPrisma from "../tag";
import { trigger } from "../trigger";
import getPrismaParams from "./getPrismaParams";
import { Metadata, Transform } from "../types";
import { WAIT_TIME } from "../constant";

interface FileItem {
file: string;
type: "update" | "delete";
}

// 防抖 需要延迟的毫秒数
const _wait = 3000;

let bar;
const supportExt = ["jpg", "png", "webp", "jpeg", "bmp", "gif", "mp4", "pdf"];

Expand Down Expand Up @@ -66,7 +64,7 @@ const PendingFiles: {
TagPrisma.clearImageZero();
}

logger.info("Image Complete 🚀");
logger.info("[transform-eagle] Image Complete 🚀");
}
}
},
Expand Down Expand Up @@ -162,7 +160,7 @@ const handleImage = async () => {
}
};

const _debounce = _.debounce(handleImage, _wait);
const _debounce = _.debounce(handleImage, WAIT_TIME);

const watchImage = (library: string, transform?: Transform) => {
const _path = join(library, "./images");
Expand Down
12 changes: 5 additions & 7 deletions packages/transform-eagle/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ const TransformEagle = async (args: Args) => {

process.env.LIBRARY = library;

logger.info("Start transform 🛫");
logger.info("[transform-eagle] Start transform 🛫");

getPrisma(library);

await watchMetadata(library);
await watchStarredTags(library);

// 延迟一个 wait 时间
setTimeout(() => {
watchMetadata(library, () => {
// 先初始化标签和文件夹
watchImage(library, transform);
}, 3000);
});
watchStarredTags(library);
};

export default TransformEagle;
9 changes: 4 additions & 5 deletions packages/transform-eagle/lib/metadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import _ from "lodash";
import { readJsonSync } from "fs-extra";
import { handleFloder } from "./folder";
import { handleTagsGroups } from "./tags-groups";

const _wait = 5000;
import { WAIT_TIME } from "../constant";

const handleMetadata = (file: string) => {
const json = readJsonSync(file);
handleFloder(json["folders"]);
handleTagsGroups(json["tagsGroups"]);
};

const _debounce = _.debounce(handleMetadata, _wait);
const _debounce = _.debounce(handleMetadata, WAIT_TIME);

const watchMetaData = (LIBRARY: string) => {
const watchMetaData = (LIBRARY: string, ready: () => void) => {
const file = join(LIBRARY, "./metadata.json");

chokidar.watch(file).on("add", _debounce).on("change", _debounce);
chokidar.watch(file).on("add", _debounce).on("change", _debounce).on("ready", ready);
};

export default watchMetaData;
2 changes: 1 addition & 1 deletion packages/transform-eagle/lib/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TagPrisma = {
},
})
.then((e) => {
logger.debug(e, "Clear tag with image count less than 1: ");
logger.debug(e, "[transform-eagle] Clear tag with image count less than 1: ");
});
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/transform-eagle/lib/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export const trigger = _.debounce(() => {
const file = process.env.LIBRARY + "/raopics.db";
const content = readFileSync(file);
writeFileSync(file, content);
logger.info("Trigger file update");
logger.info("[transform-eagle] Trigger file update");
}, 10000);

0 comments on commit ec26dfe

Please sign in to comment.