@talex-touch/utils 1.0.14 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base/index.ts +181 -181
- package/channel/index.ts +108 -99
- package/common/index.ts +2 -39
- package/common/storage/constants.ts +3 -0
- package/common/storage/entity/app-settings.ts +47 -0
- package/common/storage/entity/index.ts +1 -0
- package/common/storage/index.ts +3 -0
- package/common/utils.ts +160 -0
- package/core-box/README.md +218 -0
- package/core-box/index.ts +7 -0
- package/core-box/search.ts +536 -0
- package/core-box/types.ts +384 -0
- package/electron/download-manager.ts +118 -0
- package/{common → electron}/env-tool.ts +56 -56
- package/electron/touch-core.ts +167 -0
- package/electron/window.ts +71 -0
- package/eventbus/index.ts +86 -87
- package/index.ts +5 -0
- package/package.json +55 -30
- package/permission/index.ts +48 -48
- package/plugin/channel.ts +203 -193
- package/plugin/index.ts +216 -121
- package/plugin/log/logger-manager.ts +60 -0
- package/plugin/log/logger.ts +75 -0
- package/plugin/log/types.ts +27 -0
- package/plugin/preload.ts +39 -39
- package/plugin/sdk/common.ts +27 -27
- package/plugin/sdk/hooks/life-cycle.ts +95 -95
- package/plugin/sdk/index.ts +18 -13
- package/plugin/sdk/service/index.ts +29 -29
- package/plugin/sdk/types.ts +578 -0
- package/plugin/sdk/window/index.ts +40 -40
- package/renderer/index.ts +2 -0
- package/renderer/ref.ts +54 -54
- package/renderer/slots.ts +124 -0
- package/renderer/storage/app-settings.ts +34 -0
- package/renderer/storage/base-storage.ts +335 -0
- package/renderer/storage/index.ts +1 -0
- package/search/types.ts +726 -0
- package/service/index.ts +67 -67
- package/service/protocol/index.ts +77 -77
package/plugin/channel.ts
CHANGED
|
@@ -1,193 +1,203 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
1
|
+
// import { ipcRenderer } from 'electron';
|
|
2
|
+
// const { ipcRenderer, IpcMainEvent } = require("electron");
|
|
3
|
+
import { IpcRenderer, type IpcRendererEvent } from "electron";
|
|
4
|
+
import {
|
|
5
|
+
ChannelType,
|
|
6
|
+
DataCode,
|
|
7
|
+
ITouchClientChannel,
|
|
8
|
+
RawChannelSyncData,
|
|
9
|
+
RawStandardChannelData,
|
|
10
|
+
StandardChannelData,
|
|
11
|
+
} from "../channel";
|
|
12
|
+
|
|
13
|
+
let ipcRenderer: IpcRenderer
|
|
14
|
+
|
|
15
|
+
if (typeof window === 'undefined') {
|
|
16
|
+
ipcRenderer = require('electron').ipcRenderer
|
|
17
|
+
} else {
|
|
18
|
+
ipcRenderer = window.electron.ipcRenderer as unknown as IpcRenderer
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class TouchChannel implements ITouchClientChannel {
|
|
22
|
+
channelMap: Map<string, Function[]> = new Map();
|
|
23
|
+
|
|
24
|
+
pendingMap: Map<string, Function> = new Map();
|
|
25
|
+
|
|
26
|
+
plugin: string;
|
|
27
|
+
|
|
28
|
+
constructor(pluginName: string) {
|
|
29
|
+
this.plugin = pluginName;
|
|
30
|
+
|
|
31
|
+
ipcRenderer.on("@plugin-process-message", this.__handle_main.bind(this));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
__parse_raw_data(e: IpcRendererEvent | undefined, arg: any): RawStandardChannelData | null {
|
|
35
|
+
console.debug("Raw data: ", arg, e);
|
|
36
|
+
if (arg) {
|
|
37
|
+
const { name, header, code, data, sync } = arg;
|
|
38
|
+
|
|
39
|
+
if (header) {
|
|
40
|
+
return {
|
|
41
|
+
header: {
|
|
42
|
+
status: header.status || "request",
|
|
43
|
+
type: ChannelType.PLUGIN,
|
|
44
|
+
_originData: arg,
|
|
45
|
+
event: e
|
|
46
|
+
},
|
|
47
|
+
sync,
|
|
48
|
+
code,
|
|
49
|
+
data,
|
|
50
|
+
name: name as string,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.error(e, arg);
|
|
56
|
+
return null;
|
|
57
|
+
// throw new Error("Invalid message!");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
__handle_main(e: IpcRendererEvent, _arg: any): any {
|
|
61
|
+
const arg = JSON.parse(_arg)
|
|
62
|
+
const rawData = this.__parse_raw_data(e, arg);
|
|
63
|
+
if ( !rawData ) return
|
|
64
|
+
|
|
65
|
+
if ( rawData.header.status === 'reply' && rawData.sync ) {
|
|
66
|
+
const { id } = rawData.sync;
|
|
67
|
+
|
|
68
|
+
return this.pendingMap.get(id)?.(rawData);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// if ( rawData.plugin !== this.plugin ) return
|
|
72
|
+
|
|
73
|
+
this.channelMap.get(rawData.name)?.forEach((func) => {
|
|
74
|
+
const handInData: StandardChannelData = {
|
|
75
|
+
reply: (code: DataCode, data: any) => {
|
|
76
|
+
e.sender.send(
|
|
77
|
+
"@plugin-process-message",
|
|
78
|
+
this.__parse_sender(code, rawData, data, rawData.sync)
|
|
79
|
+
);
|
|
80
|
+
},
|
|
81
|
+
...rawData,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const res = func(handInData);
|
|
85
|
+
|
|
86
|
+
if (res && res instanceof Promise) return;
|
|
87
|
+
|
|
88
|
+
handInData.reply(DataCode.SUCCESS, res);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
__parse_sender(
|
|
93
|
+
code: DataCode,
|
|
94
|
+
rawData: RawStandardChannelData,
|
|
95
|
+
data: any,
|
|
96
|
+
sync?: RawChannelSyncData
|
|
97
|
+
): RawStandardChannelData {
|
|
98
|
+
return {
|
|
99
|
+
code,
|
|
100
|
+
data,
|
|
101
|
+
sync: !sync
|
|
102
|
+
? undefined
|
|
103
|
+
: {
|
|
104
|
+
timeStamp: new Date().getTime(),
|
|
105
|
+
// reply sync timeout should follow the request timeout, unless user set it.
|
|
106
|
+
timeout: sync.timeout,
|
|
107
|
+
id: sync.id,
|
|
108
|
+
},
|
|
109
|
+
name: rawData.name,
|
|
110
|
+
header: {
|
|
111
|
+
event: rawData.header.event,
|
|
112
|
+
status: "reply",
|
|
113
|
+
type: rawData.header.type,
|
|
114
|
+
_originData: rawData.header._originData,
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
regChannel(
|
|
120
|
+
eventName: string,
|
|
121
|
+
callback: Function
|
|
122
|
+
): () => void {
|
|
123
|
+
const listeners = this.channelMap.get(eventName) || [];
|
|
124
|
+
|
|
125
|
+
if (!listeners.includes(callback)) {
|
|
126
|
+
listeners.push(callback);
|
|
127
|
+
} else return () => void 0;
|
|
128
|
+
|
|
129
|
+
this.channelMap.set(eventName, listeners);
|
|
130
|
+
|
|
131
|
+
return () => {
|
|
132
|
+
const index = listeners.indexOf(callback);
|
|
133
|
+
|
|
134
|
+
if (index !== -1) {
|
|
135
|
+
listeners.splice(index, 1);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
send(eventName: string, arg: any): Promise<any> {
|
|
141
|
+
const uniqueId = `${new Date().getTime()}#${eventName}@${Math.random().toString(
|
|
142
|
+
12
|
|
143
|
+
)}`;
|
|
144
|
+
|
|
145
|
+
const data = {
|
|
146
|
+
code: DataCode.SUCCESS,
|
|
147
|
+
data: arg,
|
|
148
|
+
sync: {
|
|
149
|
+
timeStamp: new Date().getTime(),
|
|
150
|
+
timeout: 10000,
|
|
151
|
+
id: uniqueId,
|
|
152
|
+
},
|
|
153
|
+
name: eventName,
|
|
154
|
+
plugin: this.plugin,
|
|
155
|
+
header: {
|
|
156
|
+
status: "request",
|
|
157
|
+
type: ChannelType.PLUGIN,
|
|
158
|
+
},
|
|
159
|
+
} as RawStandardChannelData;
|
|
160
|
+
|
|
161
|
+
return new Promise((resolve) => {
|
|
162
|
+
|
|
163
|
+
ipcRenderer.send("@plugin-process-message", data);
|
|
164
|
+
|
|
165
|
+
this.pendingMap.set(uniqueId, (res: any) => {
|
|
166
|
+
this.pendingMap.delete(uniqueId);
|
|
167
|
+
|
|
168
|
+
resolve(res.data);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
sendSync(eventName: string, arg?: any): any {
|
|
174
|
+
const data = {
|
|
175
|
+
code: DataCode.SUCCESS,
|
|
176
|
+
data: arg,
|
|
177
|
+
name: eventName,
|
|
178
|
+
plugin: this.plugin,
|
|
179
|
+
header: {
|
|
180
|
+
status: "request",
|
|
181
|
+
type: ChannelType.PLUGIN,
|
|
182
|
+
},
|
|
183
|
+
} as RawStandardChannelData;
|
|
184
|
+
|
|
185
|
+
const res = this.__parse_raw_data(void 0, ipcRenderer.sendSync("@plugin-process-message", data))!
|
|
186
|
+
|
|
187
|
+
if ( res.header.status === 'reply' ) return res.data;
|
|
188
|
+
|
|
189
|
+
return res;
|
|
190
|
+
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
let touchChannel: ITouchClientChannel
|
|
195
|
+
|
|
196
|
+
export function genChannel() {
|
|
197
|
+
if (!touchChannel) {
|
|
198
|
+
// @ts-ignore
|
|
199
|
+
touchChannel = window.$channel = new TouchChannel(window.$plugin.name)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return touchChannel
|
|
203
|
+
}
|