Skip to content

Commit

Permalink
feat(1.4.1): opt codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kyuch4n committed Sep 19, 2020
1 parent 20a9027 commit d4ef3fe
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 110 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "neural",
"version": "1.4.0",
"version": "1.4.1",
"private": true,
"main": "public/electron.js",
"homepage": "./",
Expand Down
128 changes: 49 additions & 79 deletions src/components/tag-detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
FrownOutlined,
} from "@ant-design/icons";
import neuralDB, { Tag, TagStatus } from "../utils/database";
import { shake } from "../utils/window";
import { CatchWrapper } from "../decorators/catch";
import "./tag-detail.scss";

const { Countdown } = Statistic;
Expand Down Expand Up @@ -56,37 +56,27 @@ const TagDetail: FC<ITagDetailProps> = (tagDetailProps: ITagDetailProps) => {
setWikiUrl(e.target.value);
};

const handleAddWiki = () => {
try {
let _wikiUrl = wikiUrl.trim();
if (!_wikiUrl) return;
wikis.push(_wikiUrl);
const handleAddWiki = CatchWrapper(() => {
let _wikiUrl = wikiUrl.trim();
if (!_wikiUrl) return;
wikis.push(_wikiUrl);

let tag: Tag = Object.assign({}, selectedTag, {
wikis,
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
setWikiUrl("");
} catch (e) {
shake();
console.log(e);
}
};
let tag: Tag = Object.assign({}, selectedTag, {
wikis,
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
setWikiUrl("");
});

const handleDeleteWiki = (wiki: string, idx: number) => {
try {
wikis.splice(idx, 1);
let tag: Tag = Object.assign({}, selectedTag, {
wikis,
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
} catch (e) {
shake();
console.log(e);
}
};
const handleDeleteWiki = CatchWrapper((wiki: string, idx: number) => {
wikis.splice(idx, 1);
let tag: Tag = Object.assign({}, selectedTag, {
wikis,
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
});

const handleCopyWiki = (wiki: string) => {
window.clipboard.writeText(wiki);
Expand Down Expand Up @@ -132,51 +122,36 @@ const TagDetail: FC<ITagDetailProps> = (tagDetailProps: ITagDetailProps) => {
setDescriptions(e.target.value);
};

const handleConfirmEdit = () => {
try {
let tag: Tag = Object.assign({}, selectedTag, {
descriptions: descriptions.trim(),
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
setIsEditing(false);
} catch (e) {
shake();
console.log(e);
}
};
const handleConfirmEdit = CatchWrapper(() => {
let tag: Tag = Object.assign({}, selectedTag, {
descriptions: descriptions.trim(),
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
setIsEditing(false);
});

const handleCancelEdit = () => {
setDescriptions(selectedTag.descriptions || "");
setIsEditing(false);
};

const handleDone = () => {
try {
let tag: Tag = Object.assign({}, selectedTag, {
status: TagStatus.DONE,
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
} catch (e) {
shake();
console.log(e);
}
};

const handleDelay = (val: number) => {
try {
let tag: Tag = Object.assign({}, selectedTag, {
expires: Date.now() + val * 24 * 3600000,
status: TagStatus.PENDING,
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
} catch (e) {
shake();
console.log(e);
}
};
const handleDone = CatchWrapper(() => {
let tag: Tag = Object.assign({}, selectedTag, {
status: TagStatus.DONE,
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
});

const handleDelay = CatchWrapper((val: number) => {
let tag: Tag = Object.assign({}, selectedTag, {
expires: Date.now() + val * 24 * 3600000,
status: TagStatus.PENDING,
});
neuralDB.upsert_tag(tag);
onUpdate(tag);
});

const descriptionsRow = (
<div className="row-descriptions-expires">
Expand Down Expand Up @@ -240,16 +215,11 @@ const TagDetail: FC<ITagDetailProps> = (tagDetailProps: ITagDetailProps) => {
* Tag Detail
************************************************************************************************/

const handleDeleteTag = async () => {
try {
let id = selectedTag.id;
await neuralDB.delete_tag_by_id(id!);
onDelete();
} catch (e) {
shake();
console.log(e);
}
};
const handleDeleteTag = CatchWrapper(async () => {
let id = selectedTag.id;
await neuralDB.delete_tag_by_id(id!);
onDelete();
});

return (
<div className="tag-detail">
Expand Down
25 changes: 24 additions & 1 deletion src/decorators/catch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { shake } from "../utils/window";

type HandlerFunction = (error: any, ctx: any) => void;

const defaultHandler: HandlerFunction = (e) => {
shake();
console.log(e);
};

// ================================================================================================================
// 装饰器
// ================================================================================================================
function handleError(ctx: any, errorClass: any, handler: HandlerFunction, error: any) {
if (typeof handler === "function" && error instanceof errorClass) {
handler.call(null, error, ctx);
Expand All @@ -8,7 +18,7 @@ function handleError(ctx: any, errorClass: any, handler: HandlerFunction, error:
}
}

export default (errorClass: any, handler: HandlerFunction): any => {
export const Catch = (errorClass: any, handler: HandlerFunction): any => {
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
const originalMethod = descriptor.value;

Expand All @@ -31,3 +41,16 @@ export default (errorClass: any, handler: HandlerFunction): any => {
return descriptor;
};
};

// ================================================================================================================
// 非装饰器
// ================================================================================================================
export const CatchWrapper = (callback: (...args: any[]) => any, errorHandler: HandlerFunction = defaultHandler) => {
return async (...args: Parameters<typeof callback>) => {
try {
await callback(...args);
} catch (e) {
errorHandler(e, null);
}
};
};
45 changes: 16 additions & 29 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import SmartInput from "./components/smart-input";
import TagList from "./components/tag-list";
import TagDetail from "./components/tag-detail";
import neuralDB, { Tag } from "./utils/database";
import { SizeType, resizeTo, shake } from "./utils/window";
import { SizeType, resizeTo } from "./utils/window";
import Event from "./utils/event";
import Timer from "./utils/timer";
import useThrottle from "./hooks/use-throttle";
import { CatchWrapper } from "./decorators/catch";
import { Symb } from "./const/base";

import "./index.scss";

const Pattern = {
Expand All @@ -24,41 +26,31 @@ const App: FC = () => {
let [selectedTag, setSelectedTag] = useState<Tag | null>(null);
let [tagList, setTaglist] = useState<Array<Tag>>([]);

const onConfirmInput = async (input: string) => {
const onConfirmInput = CatchWrapper(async (input: string) => {
input = input.trim();
switch (true) {
case Pattern.isCreateCommand(input):
let name = input.slice(1).trim();
if (!name) return;
try {
await neuralDB.upsert_tag({ name });
setInputVal(name);
} catch (e) {
shake();
console.log(e);
}
await neuralDB.upsert_tag({ name });
setInputVal(name);
return;
case Pattern.isSymbolCommand(input):
let symbol = input.slice(1).trim().toUpperCase();
switch (symbol) {
case Symb.ALL:
try {
let result: any = await neuralDB.query_tag_by_paging(1, 10000);
let list = result.list;
setTaglist(list);
setSelectedTag(list[0] || null);
} catch (e) {
shake();
console.log(e);
}
let result: any = await neuralDB.query_tag_by_paging(1, 10000);
let list = result.list;
setTaglist(list);
setSelectedTag(list[0] || null);
return;
default:
return;
}
default:
return;
}
};
});

const onChangeInput = useThrottle((input: string) => {
input = input.trim();
Expand All @@ -68,16 +60,11 @@ const App: FC = () => {
setSelectedTag(null);
return;
case Pattern.isSearchCommand(input):
try {
(async () => {
let result: any = await neuralDB.match_tag_by_name(input);
setTaglist(result);
setSelectedTag(result[0] || null);
})();
} catch (e) {
shake();
console.log(e);
}
CatchWrapper(async () => {
let result: any = await neuralDB.match_tag_by_name(input);
setTaglist(result);
setSelectedTag(result[0] || null);
})();
return;
default:
return;
Expand Down

0 comments on commit d4ef3fe

Please sign in to comment.