返回 AiToEarn
receiveMsg.ts
根目录 / project / aitoearn-electron / src / icp / receiveMsg.ts
1 // 接收主进程发来的消息
2 import { SendChannelEnum } from '../../commont/UtilsEnum';
3 import { PublishProgressRes } from '../../electron/main/plat/pub/PubItemVideo';
4 import IpcRendererEvent = Electron.IpcRendererEvent;
5 import { AccountModel } from '../../electron/db/models/account';
6 import { PlatType } from '@@/AccountEnum';
7
8 // 绑定事件中间层方法
9 const bindEventCore = (
10 channel: SendChannelEnum,
11 listener: (event: IpcRendererEvent, ...args: any[]) => void,
12 ): (() => void) => {
13 const e = window.ipcRenderer.on(channel, listener);
14 // @ts-ignore
15 const events: any[] = e._events[channel];
16 if (!events) return () => {};
17 let f: any;
18 if (events instanceof Array) {
19 f = events[events.length === 0 ? 0 : events.length - 1];
20 } else {
21 f = events;
22 }
23 return () => {
24 if (f) {
25 window.ipcRenderer.off(channel, f);
26 }
27 };
28 };
29
30 // ----------------------- 应用 ---------------------------
31
32 export const onInteractionProgress = (callback: (...args: any[]) => void) => {
33 return bindEventCore(SendChannelEnum.InteractionProgress, (e, ...args) => {
34 callback(...args);
35 });
36 };
37
38 /**
39 * 账户登录完成的事件,可能为多个账户更新后的结果
40 * @param callback account=如果为多个账户更新的结果那么这个值是第一个,主要是为了兼容之前的单个账户的检测,accounts=如果是单个检测这个值为空,多个值检测这个值为更新后的账号结果
41 */
42 export const onAccountLoginFinish = (
43 callback: (account: AccountModel, accounts?: PlatType[]) => void,
44 ) => {
45 return bindEventCore(
46 SendChannelEnum.AccountLoginFinish,
47 (e, account: AccountModel, accounts?: PlatType[]) => {
48 callback(account, accounts);
49 },
50 );
51 };
52
53 // 视频审核完成
54 export const onVideoAuditFinish = (
55 callback: (_: { previewVideoLink: string; dataId: string }) => void,
56 ) => {
57 return bindEventCore(SendChannelEnum.VideoAuditFinish, (_, ...args) => {
58 // @ts-ignore
59 callback(...args);
60 });
61 };
62
63 // 视频发布进度
64 export const onVideoPublishProgress = (
65 callback: (progressData: PublishProgressRes, id: number) => void,
66 ) => {
67 return bindEventCore(
68 SendChannelEnum.VideoPublishProgress,
69 (_, progressData, id) => {
70 callback(progressData, id);
71 },
72 );
73 };
74
75 // 图文发布进度
76 export const onImgTextPublishProgress = (
77 callback: (progressData: PublishProgressRes) => void,
78 ) => {
79 return bindEventCore(
80 SendChannelEnum.ImgTextPublishProgress,
81 (_, progressData) => {
82 callback(progressData);
83 },
84 );
85 };
86
86 lines TYPESCRIPT