@talex-touch/utils 1.0.14 → 1.0.15

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